最近做项目,需要用到struts2中的标签,
自己便研究了一些.研究的成功..则贴出来与大家分享:
第一步搭建struts2框架。 然后建立Action.
这里取名叫做StrutsTagAction。
下面是StrutsTagAction的具体代码:
package pack.java.file.upload.action; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import com.opensymphony.xwork2.ActionSupport; public class StrutsTagAction extends ActionSupport{ private static final long serialVersionUID = 3862594026022793012L; private Map<String, Object> userGroupMap; private Map<String, Object> alreadExistsUserGroupMap; private String javaBook; private String[] leftSelectValue; private String[] rightSelectValue; //getter和settter方式省略; /** * 显示用户组信息; * @return */ public String displayUserGroupList(){ Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> userGroup = new HashMap<String, Object>(); userGroup.put("100", "开发员组"); userGroup.put("200", "代码测试组"); userGroup.put("300", "管理员组"); userGroup.put("400", "需求分析组"); userGroup.put("500", "架构设计组"); Map<String, Object> alreadyExistxUserGroup = new HashMap<String, Object>(); alreadyExistxUserGroup.put("300", "管理员组"); alreadyExistxUserGroup.put("400", "需求分析组"); alreadyExistxUserGroup.put("500", "架构设计组"); this.alreadExistsUserGroupMap = alreadyExistxUserGroup; //判定所有权限中,是否存在相同的;如果存在,则删除; for(Iterator<String> iterator1 = userGroup.keySet().iterator();iterator1.hasNext();){ String userGroupKey = iterator1.next(); for(Iterator<String> iterator2 = alreadyExistxUserGroup.keySet().iterator();iterator2.hasNext();){ String alreadyExistsKey = iterator2.next(); if(userGroupKey.equals(alreadyExistsKey)){ map.put(userGroupKey,userGroup.get(userGroupKey)); } } } //循环已有的map对象: for(Iterator<String> iterator1 = map.keySet().iterator();iterator1.hasNext();){ //移除已经存在的对象; userGroup.remove(iterator1.next()); } this.userGroupMap = userGroup; return "userGroupList"; } /** * 获取选择复选框的值; * @return */ public String getDoubleSelectValue(){ //获取选择的值; System.out.println("左侧下拉框的值:"); if(leftSelectValue!=null && leftSelectValue.length>0){ for(int i = 0;i<this.leftSelectValue.length;i++){ System.out.print(this.leftSelectValue[i]+","); } } System.out.println(""); System.out.println("右侧下拉框的值:"); if(rightSelectValue!=null && rightSelectValue.length>0){ for(int i = 0;i<this.rightSelectValue.length;i++){ System.out.print(this.rightSelectValue[i]+","); } } System.out.println(""); //重新加载显示; displayUserGroupList(); return "userGroupList"; } }
然后,建立一个jsp页面.
具体代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <s:head theme="ajax"/> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script type="text/javascript"> //在js中获取值; function getDoubleSelectListValue(){ //获取左边的下拉框的值; var leftNode = document.getElementById("left"); for(var i = 0;i<leftNode.length;i=i+1){ alert("左侧下拉框的值:->>"+leftNode[i].value+","+leftNode[i].text);//id } //获取右边的下拉框的值; var rightNode = document.getElementById("right"); for(var i = 0;i<rightNode.length;i=i+1){ alert("右侧下拉框的值:->>"+rightNode[i].value+","+rightNode[i].text); } } </script> </head> <body> <s:form method="post" name="myform" action="strutsTagAction!getDoubleSelectValue.action"> <!-- Struts2中的左右选择框; list - 指左侧下拉框的集合,可以是List集合,或者是Map集合 doubleList - 指右侧下拉框的集合, leftTitle - 指左侧标题 rightTitle - 指右侧标题 listKey - 指左侧的list中的key, listValue - 指左侧的list中的value, --> <s:optiontransferselect id="left" name="leftSelectValue" list="#request.userGroupMap" listKey="key" listValue="value" doubleId="right" doubleName="rightSelectValue" rightTitle="已选择" leftTitle="未选择" doubleList="#request.alreadExistsUserGroupMap" doubleListKey="key" doubleListValue="value" /> <br/> <s:submit value="提交已经选择的值" theme="simple"></s:submit> <input type="button" value="在Javascript中获取值" onclick="getDoubleSelectListValue()"/><br/><br/> <br/><br/> <!-- 日期选择框 --> <s:datetimepicker name="order.data" label="购买日期" toggleType="explode" value="today"/> <!-- 时间选择框; --> <s:datetimepicker name="start" label="选择出发时间" type="time" value="12:00"/> </s:form> </body> </html>
测试结果:
当点击提交按钮到后台输出的值:
左侧下拉框的值:
200,100,
右侧下拉框的值:
500,400,300,