Struts2 <s:doubleselect>示例
在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代碼...
好了,<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.yiibai.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" %>
Struts2 示例
<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" />
result.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
Struts2 示例
Test #1 : ,
Test #2 : ,
Test #2 : ,
3. struts.xml
5. 示例
http://localhost:8080/strut2sdoubleselect/doubleSelectAction.action
http://localhost:8080/strut2sdoubleselect/resultAction.action
參考
- Struts 2 關聯選擇框文檔
- Struts 2 <s:select> 示例