Www.helper3.com
热门关键字: php net javascript java
Struts2基础 专题
专题目录
您的位置:java > Struts2基础专题 > Struts2 <s:doubleselect>示例
Struts2 <s:doubleselect>示例
作者:--    发布时间:2019-11-20 10:15:59

在struts2,<s:doubleselect>标签用于创建两个html下拉框,第一个下拉列表中选择后,第二个下拉列表将根据第一个选择发生相应的变化。一个非常典型的例子是“国家”,“国家”下拉选项,不同的“状态”列表是根据所选择的“国家”。

这里创建一个web工程:strut2sdoubleselect,来演示在多个复选框如何设置的默认值,整个项目的结构如下图所示:

 <s:doubleselect> 标签示例

<s:doubleselect label="fruits (ognl) " 
name="fruit1" list="{'fruit','meat'}" 
doublename="fruit2" 
doublelist="top == 'fruit' ? {'apple', 'orange','banana'} : {'chicken', 'pig'}" />

产生下面的html代码...

<tr> 
<td class="tdlabel">
  <label for="resultaction_fruit1" class="label">
     fruits (ognl) :
  </label>
</td> 
<td> 
<select name="fruit1" id="resultaction_fruit1" 
    onchange="resultaction_fruit1redirect(this.options.selectedindex)"> 
    <option value="fruit">fruit</option> 
    <option value="meat">meat</option> 
</select> 
<br /> 
<select name="fruit2" id="resultaction_fruit2"> 
</select> 
<script type="text/javascript"> 
    var resultaction_fruit1group = new array(2 + 0);
    for (i = 0; i < (2 + 0); i++)
    resultaction_fruit1group[i] = new array();
 
    resultaction_fruit1group[0][0] = new option("apple", "apple");
 
    resultaction_fruit1group[0][1] = new option("orange", "orange");
 
    resultaction_fruit1group[0][2] = new option("banana", "banana");
 
    resultaction_fruit1group[1][0] = new option("chicken", "chicken");
 
    resultaction_fruit1group[1][1] = new option("pig", "pig");
 
 
    var resultaction_fruit1temp = document.resultaction.resultaction_fruit2;
    resultaction_fruit1redirect(0);
    function resultaction_fruit1redirect(x) {
    	var selected = false;
        for (m = resultaction_fruit1temp.options.length - 1; m >= 0; m--) {
            resultaction_fruit1temp.options[m] = null;
        }
 
        for (i = 0; i < resultaction_fruit1group[x].length; i++) {
            resultaction_fruit1temp.options[i] = 
	     new option(
              resultaction_fruit1group[x][i].text, resultaction_fruit1group[x][i].value
             );
         }
 
        if ((resultaction_fruit1temp.options.length > 0) && (! selected)) {
           	resultaction_fruit1temp.options[0].selected = true;
        }
    }
</script>   
</td> 
</tr>

好了,<s:doubleselect>标签是真正产生大量的代码。它会创建两个下拉列表, 很多javascript代码背后做魔术(第一选择,第二个变化),并且还附上了“onchange()”行为,以第一个下拉框。

看起来比较复杂,但简单!
这只是两个<s:select>标记结合在一个标签,看得很清楚了。
<s:doubleselect
name="" list="" 
doublename="" doublelist="" />

name”和“list”是指第一个下拉列表。“doublename”和“doublelist”是指所述第二下拉列表。数据群和“<s:select>”标签是一样的。 

struts2 <s:doubleselect> 示例

在一个完整的例子,例如<s:doubleselect>标签,表示使用ognl或java列出数据来填充到下拉列表中。

1. action

action类来生成两个下拉列表。
doubleselectaction.java

package com.h3.common.action;

import java.util.arraylist;
import java.util.arrays;
import java.util.hashmap;
import java.util.map;
import java.util.set;

import com.opensymphony.xwork2.actionsupport;

public class doubleselectaction extends actionsupport{

	private string fruit1;
	private string fruit2;
	
	private string server1;
	private string server2;
	
	private string language1;
	private string language2;
	
	map languagemap;

	public string getfruit1() {
		return fruit1;
	}

	public void setfruit1(string fruit1) {
		this.fruit1 = fruit1;
	}

	public string getfruit2() {
		return fruit2;
	}

	public void setfruit2(string fruit2) {
		this.fruit2 = fruit2;
	}

	public string getserver1() {
		return server1;
	}

	public void setserver1(string server1) {
		this.server1 = server1;
	}

	public string getserver2() {
		return server2;
	}

	public void setserver2(string server2) {
		this.server2 = server2;
	}

	public string getlanguage1() {
		return language1;
	}

	public void setlanguage1(string language1) {
		this.language1 = language1;
	}

	public string getlanguage2() {
		return language2;
	}

	public void setlanguage2(string language2) {
		this.language2 = language2;
	}

	public map getlanguagemap() {
		return languagemap;
	}

	public void setlanguagemap(map languagemap) {
		this.languagemap = languagemap;
	}

	public doubleselectaction(){
	  languagemap =new hashmap();		
		
          languagemap.put("java", 
            new arraylist<string>(arrays.aslist("spring", "hibernate", "struts 2")));
          languagemap.put(".net", new arraylist<string>(arrays.aslist("vb.net", "c#")));
          languagemap.put("javascript", new arraylist<string>(arrays.aslist("jquery")));
	}

	public string execute() {
		return success;
	}
	
	public string display() {
		return none;
	}
	
}

2. 结果页面

渲染两个下拉下拉框,通过“<s:doubleselect>”标签,并通过java列表,ognl列表填充

doubleselect.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>

<body>
<h1>struts2 <s:doubleselect> 示例</h1>

<s:form action="resultaction" namespace="/">

<s:doubleselect label="fruits (ognl) " 
name="fruit1" list="{'fruit','meat'}" 
doublename="fruit2" 
doublelist="top == 'fruit' ? {'apple', 'orange','banana'} : {'chicken', 'pig'}" />

<s:set name="serverlist" 
     value="#{
         'appserver': {'apache', 'tomcat', 'jboss'},
         'database': {'oracle', 'mysql'}
         }" />
<s:doubleselect label="server (ognl) " 
name="server1" list="#serverlist.keyset()" 
doublename="server2" doublelist="#serverlist[top]" />

<s:doubleselect label="language (java list) " 
name="language1" list="languagemap.keyset()" 
doublename="language2" doublelist="languagemap.get(top)" />

<s:submit value="submit" name="submit" />
	
</s:form>

</body>
</html>

result.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>

<body>
<h1>struts2 <s:doubleselect> 示例</h1>

<h2>
  test #1 : <s:property value="fruit1"/> , <s:property value="fruit2"/>
</h2> 

<h2>
  test #2 : <s:property value="server1"/> , <s:property value="server2"/>
</h2> 

<h2>
  test #2 : <s:property value="language1"/> , <s:property value="language2"/>
</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="doubleselectaction" 
         class="com.h3.common.action.doubleselectaction" method="display">
	<result name="none">/pages/doubleselect.jsp</result>
   </action>
		
   <action name="resultaction" class="com.h3.common.action.doubleselectaction">
	<result name="success">/pages/result.jsp</result>
   </action>
  </package>
	
</struts>

5. 示例

http://localhost:8080/strut2sdoubleselect/doubleselectaction.action

http://localhost:8080/strut2sdoubleselect/resultaction.action

参考

  1. struts 2 关联选择框文档
  2. struts 2 <s:select> 示例


代码下载:http://pan.baidu.com/s/1hqm8zje
所有评论

loading......

滚动到顶部 滚动到底部
合作联系(微信):
www.helper3.com
关于本站:
宗旨
网站备案号:
粤ICP备17073857号-1

声明:
本站部分内容来自网络,如您发现本站内容侵害到您的利益,请联系本站管理员处理。