在Struts2,可以使用<s:radio>標籤來建立一個HTML無線電鈕。有很多方法來填充資料到無線電鈕,可以通過List, OGNL 或 Object。請檢視下面的例子就知道了。
舉個例子來說明如何使用列表,OGNL和物件資料填充到Struts2中,使用<s:radio>標籤建立一個無線電鈕。
Action類來建立性別和語言值無線電鈕列表。
RadioButtonAction.java
package com.tw511.common.action; import java.util.ArrayList; import java.util.List; import com.tw511.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.tw511.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; } }
結果頁面是使用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>
<?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.tw511.common.action" namespace="/" extends="struts-default"> <action name="radioButtonAction" class="com.tw511.common.action.RadioButtonAction" method="display"> <result name="none">/pages/radiobutton.jsp</result> </action> <action name="resultAction" class="com.tw511.common.action.RadioButtonAction"> <result name="success">/pages/result.jsp</result> </action> </package> </struts>
http://localhost:8080/struts2radio/radioButtonAction.action
http://localhost:8080/struts2radio/resultAction.action