当前位置: 首页 > 教程 > Struts2 >

Struts2 <s:select>下拉框例子

精华
小牛编辑
139浏览
2023-03-14

这里创建一个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.yiibai.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.yiibai.common.action.SelectAction" method="display">
	<result name="none">/pages/select.jsp</result>
   </action>
		
   <action name="resultAction" class="com.yiibai.common.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

参考

  1. Struts 2 select 文档


下载代码 –  http://pan.baidu.com/s/1eQGzqHO