出家如初,成佛有余

Struts2 OptionTransferSelect标签使用

Posted in Uncategorized by chuanliang on 2008/04/02

 

有时候需要在两个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” class=”com.mobilesoft.esales.webapp.action.TransferSelect”>

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

</action>

<action name=”transferSelectPost” method=”transferSelectPost” class=”com.mobilesoft.esales.webapp.action.TransferSelect”>

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

</action>

 

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: