在Struts 2中,可以使用<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 ”是下拉列表中第一项的值表达式。
要自动为下拉框选择默认值,请阅读以下文章: Struts 2中的自动选择下拉框值
Struts 2 <s:select>示例
一个完整的Struts 2示例,可通过<s:select>创建下拉框,并通过Java列表和OGNL列表填充选择选项 ,存储选择的值并将其显示在另一个页面中。
1.行动
动作类,用于生成并保留所选的下拉框选项。
SelectAction.java
package com.mkyong.common.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.mkyong.common.action.SelectAction" method="display">
<result name="none">pages/select.jsp</result>
</action>
<action name="resultAction" class="com.mkyong.common.action.SelectAction">
<result name="success">pages/result.jsp</result>
</action>
</package>
</struts>
5.演示
http:// localhost:8080 / Struts2Example / selectAction.action
http:// localhost:8080 / Struts2Example / resultAction.action