Struts2 <s:radio>单选按钮例子
精华
小牛编辑
145浏览
2023-03-14
在Struts2,可以使用<s:radio>标签来创建一个HTML单选按钮。有很多方法来填充数据到单选按钮,可以通过List, OGNL 或 Object。请查看下面的例子就知道了。
Struts2 <s:radio> 示例
举个例子来说明如何使用列表,OGNL和对象数据填充到Struts2中,使用<s:radio>标签创建一个单选按钮。
1. 动作 - Action
Action类来创建性别和语言值单选按钮列表。
RadioButtonAction.java
package com.yiibai.common.action; import java.util.ArrayList; import java.util.List; import com.yiibai.common.model.Language; import com.opensymphony.xwork2.ActionSupport; public class RadioButtonAction extends ActionSupport{ private List<String> genders; private List<Language> languages ; private String yourGender; private String yourAnswer; private String yourLanguage; private static final String MALE = "male"; private static final String FEMALE = "female"; private static final String UNKNOWN = "unknown"; public RadioButtonAction(){ genders = new ArrayList<String>(); genders.add(MALE); genders.add(FEMALE); genders.add(UNKNOWN); languages = new ArrayList<Language>(); languages.add( new Language("EN", "English") ); languages.add( new Language("FR", "France") ); languages.add( new Language("CN_ZH", "Chinese") ); languages.add( new Language("DE", "German") ); } //return default gender value public String getDefaultGenderValue(){ return UNKNOWN; } //return default language value public String getDefaultLanguageValue(){ return "CN_ZH"; } public String execute() { return SUCCESS; } public String display() { return NONE; } //getter ad setter methods }
Language.java
package com.yiibai.common.model; public class Language{ private String languageCode; private String languageDisplay; public String getLanguageCode() { return languageCode; } public void setLanguageCode(String languageCode) { this.languageCode = languageCode; } public String getLanguageDisplay() { return languageDisplay; } public void setLanguageDisplay(String languageDisplay) { this.languageDisplay = languageDisplay; } public Language(String languageCode, String languageDisplay) { this.languageCode = languageCode; this.languageDisplay = languageDisplay; } }
2. 结果页面
结果页面是使用Struts2 “s:radio” 通过List, OGNL 和 object来创建的三个单选按钮。
radiobutton.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h1>Struts 2 radio button example</h1> <s:form action="resultAction" namespace="/"> <h2> <s:radio label="Gender" name="yourGender" list="genders" value="defaultGenderValue" /> <s:radio label="Gender" name="yourLanguage" list="languages" listKey="languageCode" listValue="languageDisplay" value="defaultLanguageValue" /> <s:radio label="Answer" name="yourAnswer" list="#{'1':'Yes','2':'No'}" value="2" /> </h2> <s:submit value="submit" name="submit" /> </s:form> </body> </html>a
result.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <body> <h1>Struts 2 radio button example</h1> <h2> Your Selected Gender : <s:property value="yourGender"/> </h2> <h2> Your Selected Language : <s:property value="yourLanguage"/> </h2> <h2> Your Selected Answer : <s:property value="yourAnswer"/> </h2> </body> </html>
3. struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="com.yiibai.common.action" namespace="/" extends="struts-default"> <action name="radioButtonAction" class="com.yiibai.common.action.RadioButtonAction" method="display"> <result name="none">/pages/radiobutton.jsp</result> </action> <action name="resultAction" class="com.yiibai.common.action.RadioButtonAction"> <result name="success">/pages/result.jsp</result> </action> </package> </struts>
5. 示例
http://localhost:8080/struts2radio/radioButtonAction.action
http://localhost:8080/struts2radio/resultAction.action
要了解如何预选值,读这篇文章 –
在Struts2的单选按钮预选值
参数
- http://struts.apache.org/2.0.11.2/docs/radio.html
- http://struts.apache.org/2.1.8.1/docs/struts-2-form-tags.html