Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 求助:OptionTransferSelect标签的使用
babycat_feifei





发贴: 1
积分: 0
于 2008-08-13 19:12 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
有时候需要在两个Select框中双向挪动数据,此种在Struts2叫OptionTransferSelect,我在网上看到一段代码,自己实践了一下,发现了好多问题,源代码如下:

1. Action层
BaseAction.java

package com.mobilesoft.esales.webapp.action;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public abstract class BaseAction extends ActionSupport {

public HttpServletRequest getRequest(){
return ServletActionContext.getRequest();
}

public HttpServletResponse getResponse(){
return ServletActionContext.getResponse();
}

public HttpSession getSession(){
return getRequest().getSession();
}

public ServletContext getServletContext(){
return ServletActionContext.getServletContext();
}

public String getRealyPath(String path){
return getServletContext().getRealPath(path);
}
}

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>

用户角色授权

<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>

选择结果

<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>
发布时报如下错误:
2008-8-11 15:40:27 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet jsp threw exception
tag 'optiontransferselect', field 'list', name 'leftRoles': The requested list key '#request.leftList' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]
at org.apache.struts2.components.Component.fieldError(Component.java:231)
at org.apache.struts2.components.Component.findValue(Component.java:293)
at org.apache.struts2.components.ListUIBean.evaluateExtraParams(ListUIBean.java:79)
at org.apache.struts2.components.DoubleListUIBean.evaluateExtraParams(DoubleListUIBean.java:95)
at org.apache.struts2.components.OptionTransferSelect.evaluateExtraParams(OptionTransferSelect.java:145)
at org.apache.struts2.components.UIBean.evaluateParams(UIBean.java:780)
at org.apache.struts2.components.UIBean.end(UIBean.java:481)
at org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:43)
at org.apache.jsp.MyListStruts_jsp._jspx_meth_s_005foptiontransferselect_005f0(MyListStruts_jsp.java:220)
at org.apache.jsp.MyListStruts_jsp._jspx_meth_s_005fform_005f0(MyListStruts_jsp.java:141)
at org.apache.jsp.MyListStruts_jsp._jspService(MyListStruts_jsp.java:83)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:413)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
应该是#request.leftList用法不对,但是不知道怎么更正,恳请各位高手帮助!!!




话题树型展开
人气 标题 作者 字数 发贴时间
9144 求助:OptionTransferSelect标签的使用 babycat_feifei 9303 2008-08-13 19:12

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923