1
votes

I am trying to read the values entered from the form to another jsp page using spring mvc but without using annotations. And while doing so i am getting this error: Neither BindingResult nor plain target object for bean name 'user' available as request attribute My Controller Class code is as follows:

public class UserFormController extends SimpleFormController {


    public UserFormController()
    {
        setCommandClass(User.class);

    }

     protected ModelAndView onSubmit(Object command) throws Exception {

        User user = (User) command;

        return new ModelAndView("userDetails", "user", user);

    }

}

My POJO code is:

public class User {
    String name;
    String gender;
    String[] subject;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public String[] getSubject() {
        return subject;
    }
    public void setSubject(String[] subject) {
        this.subject = subject;
    }
}

The Form's code is:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form:form method="POST" commandName="user" action="/abc.htm">
<table>
<tr>
     <form:input path="name" /> 
</tr>
<tr>
    <form:radiobutton label="M" value="male" path="gender"/>
    <form:radiobutton label="F" value="female" path="gender"/>
</tr>
<tr>
    <form:checkbox label="maths" value="maths" path="subject"/>
    <form:checkbox label="english" value="english" path="subject"/>
</tr>
</table>
<input type="submit" value="submit">
</form:form>
</body>
</html>

My welcome page is redirect.jsp from where i am redirecting to redirection.jsp and using web.xml i am sending the control to the servlet named "disp". The code for "disp-servlet.xml" is:

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


<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>

    <bean name="/redirection.htm" 
            class="org.springframework.web.servlet.mvc.ParameterizableViewController">
        <property name="viewName" value="entry" />
   </bean>


<bean name="/abc.htm" class="UserFormController"/>
</beans>

Through 'ParameterizableViewController' i am trying to redirect the request to entry.jsp within WEB-INF/jsp folder. And then i am trying to display the details on "userDetails.jsp":

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
user name:${user.name} 
</tr>
<tr>
user gender:${user.gender}
</tr>
<tr>
user subjects:${user.subjects[0]},${user.subjects[1]}
</tr>
</table>
</body>
</html> 

The error that i am getting is:

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute
org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:175)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:195)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:161)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:148)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:139)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:123)
org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:409)
org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:140)
org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:91)
org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:78)
org.apache.jsp.WEB_002dINF.jsp.entry_jsp._jspx_meth_form_005finput_005f0(entry_jsp.java:184)
org.apache.jsp.WEB_002dINF.jsp.entry_jsp._jspx_meth_form_005fform_005f0(entry_jsp.java:125)
org.apache.jsp.WEB_002dINF.jsp.entry_jsp._jspService(entry_jsp.java:84)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:239)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1060)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:798)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
1

1 Answers

1
votes

According to the stacktrace, the error occurs in the rendering of a view named entry.jsp. I assume it is the form that you show above.

The tag <form:form method="POST" commandName="user" action="/abc.htm"> searches a request attribute of name user (that's normal), and finds none (so the error).

A form in normally processed like this :

  • browser issues a GET request to the application
  • application produces and sends an HTML page containing the form
  • browser issues a POST request with form data
  • application processes the form data and redirects to another URL

Your error occurs in phase 2 : application produces an HTML page, because at that moment you have no request attribute.

What you should do :

  • forget the deprecated (since 3.0) SimpleFormController and use annotated controllers which are definitively simpler and more powerful - you'll find many example around
  • if you cannot (Spring 2.5 or java 1.4 but this is very old and unmaintened), use the UserFormController to prepare the view, by implementing a showForm method or at least by setting formView to entry.jsp and implementing a referenceData method returning a map { "user" : new User() }

Edit

An annotated controller could look like :

@Controller
public class UserFormController {

    @RequestMapping("/redirection")
    public String show(Model model) {
        model.addAttribute("user", new User());
        return "entry";
    }

    @RequestMapping("/abc")
    protected String submit(@ModelAttribute("user") User user) {
        return "userDetails";
    }
}