Struts2 <s:optiontransferselect>示例
在Struts2中,選項選擇組件是兩個「updownselect」選擇部件在左,右側對齊,在它們中間,包含按鈕來移動自己的選擇選項。通過<s:optiontransferselect>標籤這可以創建。
這裏創建一個Web工程:strut2updownselect,來演示在多個複選框如何設置的默認值,整個項目的結構如下圖所示:
<s:optiontransferselect
label="Lucky Numbers"
name="leftNumber"
list="{'1 - One ', '2 - Two', '3 - Three', '4 - Four', '5 - Five'}"
doubleName="rightNumber"
doubleList="{'10 - Ten','20 - Twenty','30 - Thirty','40 - Forty','50 - Fifty'}"
/>
「name」和「list」是指向左選擇組件;而「doubleName」和「doubleList」是指在正確的選擇組。
產生下面的HTML,兩個「updownselect」組件,按鈕和JavaScript來自己動(默認的XHTML主題)之間的選擇選項。
Struts2 <s:optiontransferselect> 示例
一個完整的全面的 <s:optiontransferselect> 標籤例子,表明使用OGNL和Java列出來填充數據到「選項中選擇轉移」的組件。
1. 動作類
Action類來生成並存儲左右選擇選項。
OptionTransferSelectAction.java
package com.yiibai.common.action;
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
public class OptionTransferSelectAction extends ActionSupport{
private List<String> leftAntivirusList = new ArrayList<String>();
private List<String> rightAntivirusList = new ArrayList<String>();
private String leftAntivirus;
private String rightAntivirus;
private String leftNumber;
private String rightNumber;
public OptionTransferSelectAction(){
leftAntivirusList.add("Norton 360 Version 4.0");
leftAntivirusList.add("McAfee Total Protection 2010");
leftAntivirusList.add("Trend Micro IS Pro 2010");
leftAntivirusList.add("BitDefender Total Security 2010");
rightAntivirusList.add("Norton Internet Security 2010");
rightAntivirusList.add("Kaspersky Internet Security 2010");
rightAntivirusList.add("McAfee Internet Security 2010");
rightAntivirusList.add("AVG Internet Security 2010");
rightAntivirusList.add("Trend Micro Internet Security 2010");
rightAntivirusList.add("F-Secure Internet Security 2010");
}
public String getLeftNumber() {
return leftNumber;
}
public void setLeftNumber(String leftNumber) {
this.leftNumber = leftNumber;
}
public String getRightNumber() {
return rightNumber;
}
public void setRightNumber(String rightNumber) {
this.rightNumber = rightNumber;
}
public List<String> getLeftAntivirusList() {
return leftAntivirusList;
}
public void setLeftAntivirusList(List<String> leftAntivirusList) {
this.leftAntivirusList = leftAntivirusList;
}
public List<String> getRightAntivirusList() {
return rightAntivirusList;
}
public void setRightAntivirusList(List<String> rightAntivirusList) {
this.rightAntivirusList = rightAntivirusList;
}
public String getLeftAntivirus() {
return leftAntivirus;
}
public void setLeftAntivirus(String leftAntivirus) {
this.leftAntivirus = leftAntivirus;
}
public String getRightAntivirus() {
return rightAntivirus;
}
public void setRightAntivirus(String rightAntivirus) {
this.rightAntivirus = rightAntivirus;
}
public String execute() throws Exception{
return SUCCESS;
}
public String display() {
return NONE;
}
}
2. 結果頁面
通過「<s:optiontransferselect>」選項轉移選擇組件標籤渲染,並通過Java和OGNL列表產生左側和右側選擇選項。
optiontransferselect.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
Struts 2 optiontransferselect 示例
<s:form action="resultAction" namespace="/" method="POST" >
<s:optiontransferselect
label="Lucky Numbers"
name="leftNumber"
list="{'1 - One ', '2 - Two', '3 - Three', '4 - Four', '5 - Five'}"
doubleName="rightNumber"
doubleList="{'10 - Ten','20 - Twenty','30 - Thirty','40 - Forty','50 - Fifty'}"
/>
<s:optiontransferselect
label="Favourite Antivirus"
name="leftAntivirus"
leftTitle="Left Antivirus Title"
rightTitle="Right Antivirus Title"
list="leftAntivirusList"
multiple="true"
headerKey="-1"
headerValue="--- Please Select ---"
doubleList="rightAntivirusList"
doubleName="rightAntivirus"
doubleHeaderKey="-1"
doubleHeaderValue="--- Please Select ---"
/>
<s:submit value="submit" name="submit" />
result.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
Struts 2 optiontransferselect example
Left AntiVirus :
Right AntiVirus :
Left Numbers :
Right Numbers :
3. struts.xml
4. 實例
http://localhost:8080/struts2optiontransferselect/optionTransferSelectAction.action
參考
- Struts 2 updownselect 文檔
- Struts 2 updownselect 示例
- Struts 2 doubleselect 示例
代碼下載: http://pan.baidu.com/s/1qW5p8lu