首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Struts2.0 表单顶用到select标签提交时出错

2012-10-26 
Struts2.0 表单中用到select标签提交时出错在表单用中到了select标签,页面初始化时一切正常,select也正常

Struts2.0 表单中用到select标签提交时出错
在表单用中到了select标签,页面初始化时一切正常,select也正常初始化
可是当我提交时就出现如下错误:
严重: Servlet.service() for servlet jsp threw exception
tag 'select', field 'list', name 'functions.typeId': The requested list key '%{types}' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]

在应用中我用到两个Action,两个jsp页面

一个FunctionsAction,一个FunctionstypeAction
一个add_function.jsp,一个add_functions_success.jsp

具休流程是我从FunctionstypeAction中获取一个functionstypes的List
用来初始化add_function.jsp中的select标签,
然后把表单提交给FunctionsAction中的insert方法
add_function.jsp初始化是正常的,
但是点提交到FunctionsAction的时候页面空白
我看了一下源文件,页面只执行了一部分,
代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>首页</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /></head><body style="text-align:center;"><form id="insertFunction" onsubmit="return true;" action="insertFunction.action" method="post"><table name="code">package com.mp.job.baseinfo.action;import java.util.List;import com.mp.job.baseinfo.service.IFunctionsService;import com.mp.job.baseinfo.vo.Functions;import com.mp.job.common.service.JobActionSupport;import com.mp.job.exception.BaseException;import com.mp.job.util.Pager;@SuppressWarnings("serial")public class FunctionsAction extends JobActionSupport {private IFunctionsService functionsService;private Functions functions;private List functionsRecords;@SuppressWarnings("unused")private Object[][] params;private Pager pager;         <!-- getter和setter这里省略! -->public String insert()throws BaseException{try{functionsService.save(functions);}catch(Exception e){e.toString();addActionMessage("插入数据时出现异常!");return INPUT;}return SUCCESS;}<!-- 其他的方法省略 -->}


FunctionstypeAction:
package com.mp.job.baseinfo.action;import java.util.List;import com.mp.job.baseinfo.service.IFunctionstypeService;import com.mp.job.baseinfo.vo.Functionstype;import com.mp.job.common.service.JobActionSupport;import com.mp.job.exception.BaseException;import com.mp.job.util.Pager;@SuppressWarnings("serial")public class FunctionstypeAction extends JobActionSupport {private IFunctionstypeService functionstypeService;private Functionstype functionstype;private List functionstypes;private List functionstypeRecords;@SuppressWarnings("unused")private Object[][] params;private Pager pager;<!-- getter和setter这里省略! -->@SuppressWarnings("unchecked")public String getAllFunctionstypes()throws BaseException{functionstypes = functionstypeService.findAll();return null;}<!-- 其他方法这里省略! -->public List getFunctionstypes() {return functionstypes;}public void setFunctionstypes(List functionstypes) {this.functionstypes = functionstypes;}}


struts.xml  --->>>其中用到的Action我都在Spring中配置过了
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><!-- ================================================================== --><!--                   名称相关functionsAction                                --><!-- ================================================================== --><package name="functions" namespace="/admin/BaseInfo"extends="struts-default"><action name="insertFunction" namespace="/admin/BaseInfo"extends="struts-default"><action name="getAllFunctionstypes" name="code"><%@page language="java" pageEncoding="utf-8"%><%@taglib prefix="s" uri="/struts-tags"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>首页</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /></head><body style="text-align:center;"><s:actionmessage /><s:action name="getAllFunctionstypes" id="getAllFunctionstypes" /><s:set name="types" value="#getAllFunctionstypes.functionstypes"/><s:form action="insertFunction.action" method="post"><s:select list="%{types}"  listKey="id"  listValue="name"  name="functions.typeId"  label="类型"/><s:textfield name="functions.name" label="名称"/><s:submit value="添加"/></s:form><hr/></body></html>


add_function_success.jsp: --->>>这个页面很简单,个人认为错误不会出在这里,呵呵
<%@page language="java" pageEncoding="utf-8"%><%@taglib prefix="s" uri="/struts-tags"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>首页</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /></head><body style="text-align:center;"><table width="350px" height="150px" align="center" bgcolor="#ffff00"><tr><td style="color:#00ff00;"><b>成功添加数据!</b><br/><br/><a href="add_function.jsp">继续添加</a>????<a href="listAllFunctions.action">查看全部</a></td></tr></table></body></html>


我现在的感觉是List是不是只能从同一个Action中调出,也只能提交到同一个Action????<%@page language="java" pageEncoding="utf-8"%> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>首页</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> </head> <body style="text-align:center;"> <s:actionmessage /> <s:action name="getAllFunctionstypes" id="getAllFunctionstypes" /> <s:form action="insertFunction.action" method="post"> <s:select list="#getAllFunctionstypes.functionstypes" listKey="id" listValue="name" name="functions.typeId" label="职能类型"/> <s:textfield name="functions.name" label="职能名称"/> <s:submit value="添加"/> </s:form> <hr/> </body> </html>

另外说一句:list是可以从其它action中取的,我曾经用过这种办法;但提交只能到同一个action<s:action name="getAllFunctionstypes" id="getAllFunctionstypes" /> 里面加了不合适的拦截器,特别象验证的拦截器,这样你在form提交的时候 却没有验证成功造成的
我在webwork中刚开始也遇到同样的问题 //public String getAllFunctionstypes()throws BaseException{ // functionstypes = functionstypeService.findAll(); // return null; //} <!-- 其他方法这里省略! --> public List getFunctionstypes() { return functionstypeService.findAll(); } //public void setFunctionstypes(List functionstypes) { // this.functionstypes = functionstypes; //}

热点排行