`

Struts2 OptionTransferSelect标签使用

    博客分类:
  • java
阅读更多

转自 出家如初,成佛有余

有时候需要在两个Select框中双向挪动数据,此种在Struts2叫OptionTransferSelect,效果如下:

optiontransferselect

1. Action层

TransferSelect.java

package com.mobilesoft.esales.webapp.action;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import java.util.Set;

import org.apache.log4j.Logger;

public class TransferSelect extends BaseAction {

private static final Logger logger = Logger.getLogger(DoubleListAction.class);

    String[] leftRoles;

    String[] rightRoles;

public String execute(){   

return SUCCESS;

    }

public String transferSelect(){

       ArrayList<Roles> leftList=new ArrayList<Roles> ();     

       Roles leftRoles1=new Roles();

       leftRoles1.setRoleId(1);

       leftRoles1.setRoleName("角色1");

       Roles leftRoles2=new Roles();

       leftRoles2.setRoleId(2);

       leftRoles2.setRoleName("角色2");

       Roles leftRoles3=new Roles();

       leftRoles3.setRoleId(3);

       leftRoles3.setRoleName("角色3");

       Roles leftRoles4=new Roles();

       leftRoles4.setRoleId(4);

       leftRoles4.setRoleName("角色4");

       leftList.add(leftRoles1);

       leftList.add(leftRoles2);

       leftList.add(leftRoles3);

       leftList.add(leftRoles4);

       ArrayList<Roles>  rightList=new ArrayList();     

       Roles rightRoles1=new Roles();

       rightRoles1.setRoleId(1);

       rightRoles1.setRoleName("角色5");

       Roles rightRoles2=new Roles();

       rightRoles2.setRoleId(2);

       rightRoles2.setRoleName("角色6");

       Roles rightRoles3=new Roles();

       rightRoles3.setRoleId(3);

       rightRoles3.setRoleName("角色7");

       Roles rightRoles4=new Roles();

       rightRoles4.setRoleId(4);

       rightRoles4.setRoleName("角色8");

       rightList.add(rightRoles1);

       rightList.add(rightRoles2);

       rightList.add(rightRoles3);

       rightList.add(rightRoles4);    

       getRequest().setAttribute("rightList", rightList);

       getRequest().setAttribute("leftList", leftList);

return SUCCESS;

    }

public String transferSelectPost(){

       Map map=getRequest().getParameterMap();

       Set set=map.entrySet();

       Iterator iterator=set.iterator();

while(iterator.hasNext()){

           Map.Entry mapEntry=(Map.Entry)iterator.next();

if(mapEntry.getValue() instanceof String[]){

              String[] selectValues=(String[])mapEntry.getValue();

for(int i=0;i<selectValues.length;i++){

logger.fatal("For map test,The key is: "+mapEntry.getKey()+",value is :"+selectValues[i]); 

              }

           }

       }

for(int i=0;i<leftRoles.length;i++){

logger.fatal("For Array test,The leftRoles roleName is: "+leftRoles[i]); 

       }

for(int i=0;i<rightRoles.length;i++){

logger.fatal("For Array test,The rightRoles roleName is: "+rightRoles[i]);  

       }     

       getRequest().setAttribute("map", map);

return SUCCESS;

    }

public void setLeftRoles(String[] leftRoles) {

this.leftRoles = leftRoles;

    }

public void setRightRoles(String[] rightRoles) {

this.rightRoles = rightRoles;

    }  

}

Roles.java

package com.mobilesoft.esales.webapp.action;

public class Roles {
    private Integer roleId;
    private String roleName;
    public Roles(){
    }
    public Integer getRoleId() {
        return roleId;
    }
    public void setRoleId(Integer roleId) {
        this.roleId = roleId;
    }
    public String getRoleName() {
        return roleName;
    }
    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }
}

2. Web层

transferselect.jsp

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

<%@ page language="java" errorPage="/error.jsp" pageEncoding="GBK" contentType="text/html;charset=GBK" %>

<html>

<head>

<title>Struts 2 Cool Tags - OptionTransferSelect</title>

<s:head theme="ajax"/>

</head>

<body>

<h2>用户角色授权</h2>

<s:form action="transferSelectPost" name="form1" >

<s:optiontransferselect

label="测试-Action数据"

name="leftRoles"

            leftTitle="用户已授权角色"

            list="#request.leftList"

            listKey="roleId"

            listValue="roleName"

multiple="true"

            headerKey="headerKey"

            headerValue="--- 请选择用户角色 ---"

            emptyOption="false"

            allowUpDownOnLeft="false"

            cssStyle="width:200px;height:300px;"

            rightTitle="用户未授权的角色"

            doubleList="#request.rightList"

            doubleListKey="roleId"

            doubleListValue="roleName"

            doubleName="rightRoles"

            doubleHeaderKey="doubleHeaderKey"

            doubleHeaderValue="--- 请选择用户角色 ---"

            doubleEmptyOption="false"

            doubleMultiple="true"

            allowUpDownOnRight="false"

            doubleCssStyle="width:200px;height:300px;"

/>

<s:submit align="left"/>

</s:form>

</body>

</html>

transferselectpost.jsp

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

<%@ page language="java" errorPage="/error.jsp" pageEncoding="GBK" contentType="text/html;charset=GBK" %>

<html>

<head>

<title>Struts 2 Cool Tags - OptionTransferSelect</title>

<s:head />

</head>

<body>

<h2>选择结果</h2>

<s:form action="transferSelect" name="form1" >

<s:iterator value="#request.map" status="mystatus">

<tr>

<td>

<s:property value="key" />:<s:property value="value" />,             

</td>

</tr>

</s:iterator>

<s:submit align="left"/>

</s:form>

</body>

</html>

3. struts.xml

<action name="transferSelect" method="transferSelect"

<result name="success">test/transferselect.jsp</result>

</action>

<action name="transferSelectPost" method="transferSelectPost"

<result name="success">test/transferselectpost.jsp</result>

</action>

 

jsp中的<s:optiontransferselect />标签已正常显示使用
<s:optiontransferselect tooltip="用户权限" label="设置权限"
name="mhPopedom.leftList_1"
leftTitle="用户未授权限"
list="leftList_1"
listKey="popeId"
listValue="popeName"
headerKey="headerKey"
multiple="true"
emptyOption="false"
allowUpDownOnLeft="false"
cssStyle="width:200px;height:300px;"
cssClass="input2"

addToLeftLabel="向左移动"
addToRightLabel="向右移动"
addAllToLeftLabel="全部左移"
selectAllLabel="全部选择"
addAllToRightLabel="全部右移"

doubleName="rightPopedom"
rightTitle="用户已授权限"
doubleList="rightList_1"
doubleListKey="popeId"
doubleListValue="popeName"
doubleHeaderKey="doubleHeaderKey"
doubleMultiple="true"
doubleEmptyOption="false"
allowUpDownOnRight="false"
doubleCssClass="input2"
doubleCssStyle="width:200px;height:300px;">
</s:optiontransferselect>
但不论怎样在action中都不能获得rightPopedom的值,报空指针异常。
请各位帮忙,在线急等!!!!
flybida 2009-07-28
后台action里面添加属性(setXXX和getXXX方法):
private String[] leftList;
private String[] rightList;

在前台页面上,当提交表单时,onclick事件调用下面的js:
<script type="text/javascript">
  function myself(){
  var right=document.forms[0].rightList;
  for(i=1; i <right.length; i++)
    right[i].selected = true;
  }
</script>
提交后,就能在后台得到rightList里面的值了。
如果没有这段Js代码,后台是得不到值的,这是Struts2 <s:optiontransferselect/>本身的bug.
 
分享到:
评论

相关推荐

    struts2 optiontransferselect标签的用法

    struts2 optiontransferselect标签,其中包含一个jsp页面,可以将此jsp页面放入您的项目中,即可看到效果!

    struts2 optiontransferselect标签使用中的问题

    NULL 博文链接:https://hanxin0311.iteye.com/blog/443327

    Struts2 UI标签代码示例

    Struts2 UI标签示例(包含tabbedPanel标签,autocompleter标签,tree和treenode,optiontransferselect标签等)

    struts2标签大全详解

    struts2标签大全详解,optiontransferselect标签属性 ,Struts2常用的Ajax标签

    struts2 标签 s:optiontransferselect左右下拉列表的使用。

    NULL 博文链接:https://zhouhaitao.iteye.com/blog/1179393

    struts2 标签库 帮助文档

    optiontransferselect标签:创建两个选项以及转移下拉列表项,该标签生成两个下拉列表框,同时生成相应的按钮,这些按钮可以控制选项在两个下拉列表之间移动、排序。 radio标签:单选框。 optgroup标签:生成一个...

    JQuery实现select互换数据和上下移动

    功能类似struts2 optiontransferselect 标签的jquery组件 非常好用的一个JQuery组件

    用struts2 实现注册

    用struts2实现用户的注册,涉及到的知识点包括用户名和密码的校验(正则表达式验证);国际化;radio,checkboxlist,optiontransferselect等标签的使用,文件的上传等。

    深入浅出Struts2(附源码)

    5.18 optiontransferselect标签 87 5.19 doubleselect标签 90 5.20 主题 92 5.21 小结 94 第6章通用标签 95 6.1 property标签 95 6.2 a标签 97 6.3 action标签 97 6.4 param标签 98 6.5 bean标签 98 6.6 ...

    JSP_struts2标签大全

    JSP_struts2标签大全 1.a 3 2.action 3 3. actionerror 4 4. actionmessage 5 5. append 5 6. bean 7 7.checkbox 7 8.checkboxlist 8 9. combobox 9 10. conponent 9 11. date 11 12. datetimepicker 12 13. debug ...

    Struts2+API+标签全解+网页说明

    optiontransferselect标签:创建两个选项以及转移下拉列表项,该标签生成两个下拉列表框,同时生成相应的按钮,这些按钮可以控制选项在两个下拉列表之间移动、排序。 radio标签:单选框。 optgroup标签:生成一个...

    struts2相关资料

    这里包含了struts2标签的介绍,介绍了Struts2中OGNLvalueStack和stackContext,radio List的预选问题,文件的上传与下载,防止表单重复提交optiontransferselect字符串拼接,struts2_Path_路径问题

    Struts2标签 UI标志又可以分为表单UI和非表单UI两部分

    不过,Struts 2.0加了几个我们经常在项目中用到的控件如:datepicker、doubleselect、timepicker、optiontransferselect等。因为这些标志很多都经常用到,而且参数也很多,要在一篇文章详细说明并非易事。 下面主要...

    深入浅出Struts 2 .pdf(原书扫描版) part 1

    5.18 optiontransferselect标签 87 5.19 doubleselect标签 90 5.20 主题 92 5.21 小结 94 第6章 通用标签 95 6.1 property标签 95 6.2 a标签 97 6.3 action标签 97 6.4 param标签 98 6.5 bean标签 98 6.6 date标签 ...

    低清版 大型门户网站是这样炼成的.pdf

    3.4.10 optiontransferselect标签详解 144 3.4.11 password标签详解 146 3.4.12 radio标签详解 147 3.4.13 reset与submit标签详解 148 3.4.14 textarea标签详解 149 3.4.15 textfield标签详解 149 3.4.16 ...

Global site tag (gtag.js) - Google Analytics