Struts2.2.3.1,Method "setStudent" failed for object com.city.action.UpdateAction
在自学struts2 就自己写了个小的管理程序
在对信息进行修改的时候出错了,请大家帮帮忙
struts.xml
<?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> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="false" /> <package name="default" namespace="/" extends="struts-default"> <default-action-ref name="index" /> <action name="index" class="com.city.action.StudentAction"> <result name="success">/index.jsp</result> <result name="update">/update.jsp</result> <result name="add">/add.jsp</result> <result name="goback" type="redirectAction"> <param name="actionName">back</param> <param name="namespace">/</param> </result> </action> <action name="back" class="com.city.action.StudentAction"> <result name="success"> /index.jsp </result> </action> <action name="add" class="com.city.action.AddAction"> <result name="goback" type="redirectAction"> <param name="actionName">back</param> <param name="namespace">/</param> </result> </action> <action name="update" class="com.city.action.UpdateAction"> <result name="goback" type="redirectAction"> <param name="actionName">back</param> <param name="namespace">/</param> </result> <result name="input"> /error.jsp </result> </action> </package> <!-- Add packages here --></struts>
package com.city.action;import java.util.ArrayList;import java.util.List;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.apache.struts2.ServletActionContext;import org.apache.struts2.interceptor.RequestAware;import org.apache.struts2.interceptor.ServletRequestAware;import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.ActionSupport;import com.city.model.*;import com.city.server.StudentServer;public class StudentAction extends ActionSupport{ List<Student> list = new ArrayList<Student>(); HttpServletRequest request=ServletActionContext.getRequest(); private String id; private Student student = new Student(); public HttpServletRequest getRequest() { return request; } public void setRequest(HttpServletRequest request) { this.request = request; } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } public List<Student> getList() { return list; } public void setList(List<Student> list) { this.list = list; }// public void setServletRequest(HttpServletRequest request){// this.request = request;// } public String execute(){ list = new StudentServer().checkStuList(); return SUCCESS; } public String gotoUpdate(){ this.student = new StudentServer().checkStuById(Integer.parseInt(id)); return "update"; } public String gotoAdd(){ return "add"; } public String deleteStuById(){ new StudentServer().deleteStu(Integer.parseInt(id)); return "goback"; } public void setId(String id) { this.id = id; } public String getId() { return id; }}
package com.city.action;import com.city.model.Student;import com.city.server.StudentServer;import com.opensymphony.xwork2.ActionSupport;public class UpdateAction extends ActionSupport { Student student = new Student(); public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } public String updateSuccess(){ new StudentServer().updateStu(student); return "goback"; } public String updateCancle(){ return "goback"; } }<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><%@taglib uri="/struts-tags" prefix="s" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'update.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> This is my update page. <br> <s:form action="update!updateSuccess" method="post"> <table> <tr> <td> 姓名: </td> <td> <s:hidden name="student" theme="simple" value="<s:property value='student.id'/>" ></s:hidden> <input name="student.name" value="<s:property value="student.name"/>" ></input> </td> </tr> <tr> <td> 学号: </td> <td> <input name="student.stu_no" value="<s:property value="student.stu_no"/>" ></input> </td> </tr> <tr> <td> 年龄: </td> <td> <input name="student.age" value="<s:property value="student.age"/>" ></input> </td> </tr> <tr> <td> <s:submit value="提交"></s:submit> </td> <td> </td> </tr> </table> </s:form> <table> <tr><td><a href="update!updateCancle">返回</a></td></tr> </table> <br/> <hr/> <s:debug></s:debug> </body></html>
at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1285)
at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:1474)
at ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:85)
at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:162)
at com.opensymphony.xwork2.ognl.accessor.ObjectAccessor.setProperty(ObjectAccessor.java:27)
at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2245)
java.lang.NoSuchMethodException: com.city.action.UpdateAction.setStudent([Ljava.lang.String;)
。。。。
我在网上查了下,有的人说是页面有重复信息,是不是我在StudentAction里面定义了student对象 在UpdateAction里面也定义了student对象 然后都放在了udapte.jsp页面造成了冲突,但是如果这样写的话,那我UpdateAction里面怎样获得update.jsp提交的值呢???求帮助
[解决办法]
java.lang.NoSuchMethodException: com.city.action.UpdateAction.setStudent([Ljava.lang.String;)
他是找不到setStudent方法,是不是要定义的,查一下
<action name="index" class="com.city.action.StudentAction">
<result name="success">/index.jsp</result>
<result name="update">/update.jsp</result>
<result name="add">/add.jsp</result>
<result name="goback" type="redirectAction">
<param name="actionName">back</param>
<param name="namespace">/</param>
</result>
</action>
[解决办法]
java.lang.NoSuchMethodException: com.city.action.UpdateAction.setStudent([Ljava.lang.String;)
UpdateAction中没找到setStudent方法,如果没有提供这个方法,传参数的时候肯定会报错。。。。
[解决办法]
<s:hidden name="student" theme="simple" value="<s:property value='student.id'/>" ></s:hidden>
改为
<s:hidden name="student.id" theme="simple" value="<s:property value='student.id'/>" ></s:hidden>
[解决办法]
9楼正解,应该是value="<s:property value='student.id'没有取到值,所以就报错“”不能转换为int,可以把student.id的值直接打印出来看看。
[解决办法]
,<s:hidden name="student.id" theme="simple" value="<s:property value='student.id'/>" ></s:hidden>
差不多就是这里出错了
public class UpdateAction extends ActionSupport {
Student student = new Student();
struts2 需要你去new 嘛?
private Student student;
[解决办法]
问题出在:
<s:hidden name="student.id" theme="simple" value="<s:property value='student.id'/>" ></s:hidden>
[解决办法]
断点 看下变量有没有值