Struts2 <s:radio>單選按鈕例子
在Struts2,可以使用<s:radio>標簽來創建一個HTML單選按鈕。有很多方法來填充數據到單選按鈕,可以通過List, OGNL 或 Object。請查看下麵的例子就知道了。
Struts2 <s:radio> 示例
舉個例子來說明如何使用列表,OGNL和對象數據填充到Struts2中,使用<s:radio>標簽創建一個單選按鈕。
1. 動作 - Action
Action類來創建性彆和語言值單選按鈕列表。
RadioButtonAction.java
package com.gitbook.netmon.action; import java.util.ArrayList; import java.util.List; import com.gitbook.netmon.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.gitbook.netmon.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.gitbook.netmon.action" namespace="/" extends="struts-default"> <action name="radioButtonAction" class="com.gitbook.netmon.action.RadioButtonAction" method="display"> <result name="none">/pages/radiobutton.jsp</result> </action> <action name="resultAction" class="com.gitbook.netmon.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