最近在弄SSH,结果action接收表单传值出现了错误:
三月 27, 2018 4:21:38 下午 com.opensymphony.xwork2.interceptor.ParametersInterceptor error
严重: Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'vApp.deptId' on 'class com.caixinhao.vehicleSystem.action.VehicleApplyAction: Error setting expression 'vApp.deptId' with v
alue '[Ljava.lang.String;@1f7925b4'
三月 27, 2018 4:21:38 下午 com.opensymphony.xwork2.interceptor.ParametersInterceptor error
严重: Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'vApp.driverId' on 'class com.caixinhao.vehicleSystem.action.VehicleApplyAction: Error setting expression 'vApp.driverId' wi
th value '[Ljava.lang.String;@41800801'
三月 27, 2018 4:21:38 下午 com.opensymphony.xwork2.interceptor.ParametersInterceptor error
严重: Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'vApp.empId' on 'class com.caixinhao.vehicleSystem.action.VehicleApplyAction: Error setting expression 'vApp.empId' with val
ue '[Ljava.lang.String;@526440c3'
三月 27, 2018 4:21:38 下午 com.opensymphony.xwork2.interceptor.ParametersInterceptor error
严重: Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'vApp.endTime' on 'class com.caixinhao.vehicleSystem.action.VehicleApplyAction: Error setting expression 'vApp.endTime' with
value '[Ljava.lang.String;@c057566'
三月 27, 2018 4:21:38 下午 com.opensymphony.xwork2.interceptor.ParametersInterceptor error
严重: Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'vApp.startTime' on 'class com.caixinhao.vehicleSystem.action.VehicleApplyAction: Error setting expression 'vApp.startTime'
with value '[Ljava.lang.String;@ef5784d'
三月 27, 2018 4:21:38 下午 com.opensymphony.xwork2.interceptor.ParametersInterceptor error
严重: Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'vApp.vehicleAppType' on 'class com.caixinhao.vehicleSystem.action.VehicleApplyAction: Error setting expression 'vApp.vehicl
eAppType' with value '[Ljava.lang.String;@4f74a992'
三月 27, 2018 4:21:38 下午 com.opensymphony.xwork2.interceptor.ParametersInterceptor error
严重: Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'vApp.vehicleId' on 'class com.caixinhao.vehicleSystem.action.VehicleApplyAction: Error setting expression 'vApp.vehicleId'
with value '[Ljava.lang.String;@3f668ae7'
三月 27, 2018 4:21:38 下午 com.opensymphony.xwork2.interceptor.ParametersInterceptor error
严重: Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'vApp.vehicleType' on 'class com.caixinhao.vehicleSystem.action.VehicleApplyAction: Error setting expression 'vApp.vehicleTy
pe' with value '[Ljava.lang.String;@186f7628'
首先我们确认struts2action接受表单传值的三种方式:
链接在此http://aslijiasheng.iteye.com/blog/1979691
struts2中的Action接收表单传递过来的参数有3种方法:
如,登陆表单login.jsp:
<form action="login" method="post" name="form1">
用户名:<s:textfield name="username"/><br/>
密 码:<s:password name="password"/><br/>
<s:submit value="提交"/>
</form>
1.在Action类中定义表单属性,两者属性名称必须一致。提供setter,getter方法。即可接收到表单传过来的参数.
这种接收参数的方法,方便简单,但是结构性不是很好,且当表单传递来的参数很多的时候,整个Action类中充斥着setter,getter方法,程序结构不是很美观。
2.把表单传递过来的参数封装成一个类,然后调用其中的属性.
如,把login.jsp页面要传来的参数进行封装
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
然后再Action方法中,定义该类的对象就可以了,如
public class loginAction extends ActionSupport{
private Users users;
public Users getUsers(){
return users;
}
public void setUsers(Users users){
this.users=users;
}
/*
传递过来的参数都封装在users中了,用getter方法取值就可以了
*/
}
通过这种方法传值,还必须在jsp页面做一下处理,login.jsp中from1的属性名应该改成这样:
登陆表单login.jsp:
<form action="login" method="post" name="form1">
用户名:<s:textfield name="users.username"/><br/>
密 码:<s:password name="users.password"/><br/>
<s:submit value="提交"/>
</form>
这种方法,在struts开发中是很常用的一种方法!
3.通过实现ModelDriven接口接收表单数据
首先Action类必须实现ModelDriven接口,同样把表单传来的数据封装起来,Action类中必须实例化该对象,并且要重写getModel()方法
public class loginAction extends ActionSupport implements ModelDriven<Users>{
private Users users =new Users();
public Users getModel(){
return users;
}
/*
表单传来的参数封装在users对象中
表单属性名不需要加上引用users对象,直接传参数名
*/
}
我用的是第二种方法,各种代码如下
JSP代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib prefix="s" uri="/struts-tags"%>
<table width="960" border="0" align="center"
background="${pageContext.request.contextPath}/images/bodybg.jpg">
<div aligin="left">
<s:form action="vehicleApply" >
申请车辆出库
<s:textfield name="vApp.empId" label="申请人ID"/>
<s:textfield name="vApp.driverId" label="驾驶员ID"/>
<s:textfield name="vApp.vehicleAppType" label="申请类型"/>
<s:textfield name="vApp.vehicleId" label="车辆ID"/>
<s:textfield name="vApp.vehicleType" label="车辆类型"/>
<s:textfield name="vApp.startTime" label="开始时间"/>
<s:textfield name="vApp.endTime" label="结束时间"/>
<s:textfield name="vApp.deptId" label="部门ID"/>
<tr><td colspan="2">
<s:submit value="提交" theme="simple"/><s:reset theme="simple" value="重填"/>
</td></tr>
</s:form>
</div>
action代码
public class VehicleApplyAction extends EmpBaseAction
{
private VehicleApply vApp;
public VehicleApply getvApp()
{
return this.vApp;
}
public void setvApp(VehicleApply vApp)
{
this.vapp = vApp;
}
public String execute()throws Exception
{
// 创建ActionContext实例
ActionContext ctx = ActionContext.getContext();
mgr.addVehicleApply(getvApp());
return SUCCESS;
}
}
结果还是出错,网上各种百度各种找,后来终于找到了======命名不规范
将vApp命名为vapp,
setvApp命名为setVapp,
马上就不报错了。
命名不规范,结果系统没有检测到setter方法,所有就会出现上面的错误。