BindingResult和bean名称'student'的普通目标对象都不能作为请求属性使用

时间:2013-05-12 16:36:09

标签: spring spring-mvc

我在检索jsp studentHome.jsp时遇到以下异常:

org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/jsp/studentHome.jsp at line 16

13:         <table>
14:             <tr>
15:                 <td>Name</td>
16:                 <td><form:input path="name"/></td>
17:             </tr>
18:             <tr>
19:                 <td>Age</td>
Neither BindingResult nor plain target object for bean name 'student' available as request attribute

studentHome.jsp:

<form:form method="post" action="somepage" commandName="student">
    <table>
        <tr>
            <td>Name</td>
            <td><form:input path="name"/></td>
        </tr>
        <tr>
            <td>Age</td>
            <!--Notice, this is normal html tag, will not be bound to an object -->
            <td><form:input path="age"/></td>
        </tr>
        <tr>
            <td>Id</td>
            <td><form:input path="id"/></td>
        </tr>
        <tr>
            <td>Address</td>
            <!--Notice, this is normal html tag, will not be bound to an object -->
            <td><form:input path="address"/></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="Send to Student Controller"/>
            </td>
        </tr>
    </table>
</form:form>

Student.java:

package springapp1.domain;
import java.io.Serializable;
@SuppressWarnings("serial")
public class Student implements Serializable{

private String name;
private int id;
private String age;
private String address;

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getAge() {
    return age;
}
public void setAge(String age) {
    this.age = age;
}
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}

public String toString() {

    StringBuffer details= new StringBuffer();
    details.append("Name: "+this.name);
    details.append("Age: "+this.age);
    details.append("Id: "+this.id);
    details.append("Address: "+this.address);
    return details.toString();
}

}

StudentController.java:

public class StudentController implements Controller {

protected final Log logger = LogFactory.getLog(getClass());

@Override
public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response)
throws Exception {
    // TODO Auto-generated method stub
    logger.info("returning studentView");
    Map<String, Object> myModel = new HashMap<String, Object>();
    myModel.put("students", "students");
    return new ModelAndView("studentHome", "model", myModel);
}

@ModelAttribute("student")
 public Student getStudentObject() {
  return new Student();
 }

@RequestMapping("/somepage")
public String someAction(@ModelAttribute("student") Student student, Map<String, Object> map,
                                HttpServletRequest request, BindingResult bindingResult) {

        /*logger.info("Name=" + request.getParameter("name") + " age=" + request.getParameter("age"));*/
       /* do some process and send back the data */
        map.put("student", student);
        return "studentsuccess";
   }

}

我想显示这个jsp; studentsuccess.jsp:

<%@ include file="/WEB-INF/jsp/include.jsp" %>
<html>
<head>
<title><fmt:message key="studentTitle"/></title>
</head>
<body>
<h1><fmt:message key="studentHeading"/></h1>
<h3>Students Success Page</h3>
</body>
</html>

servlet xml文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<!-- the application context definition for the springapp1 DispatcherServlet -->
<bean id="viewResolver"        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>

<bean name="/student.htm" class="springapp1.web.StudentController">
</bean>

我浏览过很多类似错误的stackoverflow帖子。但是,似乎我无法得到如何解决这个问题。问题似乎是在StudentController.java文件中。请帮忙!

1 个答案:

答案 0 :(得分:-1)

您不应手动实施handleRequest方法。

  

此方法 - 以及此接口 - 最好不要直接由自定义控制器实现,因为此包提供的抽象控制器已经为Web应用程序中的典型用例提供了许多功能。

因此,请尝试将其删除并替换为普通请求处理程序,您将Student对象放入Model

另外,替换

public class StudentController implements Controller {

通过

@Controller
public class StudentController {