Struts2 <s:select>下拉框例子
這裡創建一個Web工程:strut2selectdropdown,來演示在多個複選框如何設置的默認值,整個項目的結構如下圖所示:
在Struts2,可以使用 <s:select> 標簽來創建一個HTML下拉框。
<s:select label="What's your favor search engine" headerKey="-1" headerValue="Select Search Engines" list="searchEngine" name="yourSearchEngine" />
產生下麵的HTML代碼...
<td class="tdLabel"> <label for="resultAction_yourSearchEngine" class="label"> What's your favor search engine: </label> </td> <td> <select name="yourSearchEngine" id="resultAction_yourSearchEngine"> <option value="-1">Select Search Engines</option> <option value="google.com">google.com</option> <option value="bing.com">bing.com</option> <option value="yahoo.com">yahoo.com</option> <option value="baidu.com">baidu.com</option> </select> </td>
其語法不需要太多解釋,隻不過是“headerKey”和“headerValue”。“headerKey”是一個在下拉列表中項用來表示鍵,而“headerValue”是在下拉列表中的項目表示值。
要自動選擇下拉框中的默認值,看看這篇文章:自動選擇Struts2下拉框的值
Struts 2 <s:select> 示例
一個通過<s:select>創建下拉框完整的Struts2的例子,和填充通過Java列表,OGNL列表中選擇選項,保存所選的值,並在另一頁顯示。
1. 動作 - Action
Action類來生成並按住選定的下拉框中選項。
SelectAction.java
package com.gitbook.netmon.action; import java.util.ArrayList; import java.util.List; import com.opensymphony.xwork2.ActionSupport; public class SelectAction extends ActionSupport{ private List<String> searchEngine; private String yourSearchEngine; private String yourMonth; public String getYourMonth() { return yourMonth; } public void setYourMonth(String yourMonth) { this.yourMonth = yourMonth; } public List<String> getSearchEngine() { return searchEngine; } public void setSearchEngine(List<String> searchEngine) { this.searchEngine = searchEngine; } public String getYourSearchEngine() { return yourSearchEngine; } public void setYourSearchEngine(String yourSearchEngine) { this.yourSearchEngine = yourSearchEngine; } public String getDefaultSearchEngine() { return "yahoo.com"; } public SelectAction(){ searchEngine = new ArrayList<String>(); searchEngine.add("google.com"); searchEngine.add("bing.com"); searchEngine.add("yahoo.com"); searchEngine.add("baidu.com"); } public String execute() { return SUCCESS; } public String display() { return NONE; } }
2. 結果頁麵
渲染下拉框中通過“<s:select>”標簽,並填充通過Java列表,OGNL列表中的選擇選項
select.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h1>Struts 2 drop down box example</h1> <s:form action="resultAction" namespace="/"> <h2> <s:select label="What's your favor search engine" headerKey="-1" headerValue="Select Search Engines" list="searchEngine" name="yourSearchEngine" value="defaultSearchEngine" /> </h2> <h2> <s:select label="Select a month" headerKey="-1" headerValue="Select Month" list="#{'1':'Jan', '2':'Feb', '3':'Mar', '4':'Apr'}" name="yourMonth" value="2" /> </h2> <s:submit value="submit" name="submit" /> </s:form> </body> </html>
result.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <body> <h1>Struts 2 drop down box example</h1> <h2> Favor search engine : <s:property value="yourSearchEngine"/> </h2> <h2> Selected month : <s:property value="yourMonth"/> </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="default" namespace="/" extends="struts-default"> <action name="selectAction" class="com.gitbook.netmon.action.SelectAction" method="display"> <result name="none">/pages/select.jsp</result> </action> <action name="resultAction" class="com.gitbook.netmon.action.SelectAction"> <result name="success">/pages/result.jsp</result> </action> </package> </struts>
5. 運行實例
http://localhost:8080/strut2selectdropdown/selectAction.action
http://localhost:8080/strut2selectdropdown/resultAction.action
參考