0

I’m just new to Spring, stuck up with some error past two days. Would like somebody to help and solve the problem. Also I googled the error but could not understand the root cause of the error. I've not studied about the annotations in spring yet, so could not understand most of the examples online.

Error occured in the program:

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'login' available as request attribute

Here, is the code of the application:

data.jsp:(This is a view page to accept the data from the user)

<form:form commandName="login" method="POST" name="login">
Username:<form:input path="name"/> 
<font color="red"><form:errors path="name"/></font><br/><br/>
<input type="submit" value="Login"/>
</form:form>

LoginFormController.java:(Controller page to process the request)

package Controller;
import model.Student;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class LoginFormController extends SimpleFormController {

    public LoginFormController() {
        //Initialize controller properties here or 
        //in the Web Application Context

        setCommandClass(Student.class);
        setCommandName("login");
        setSuccessView("success");
        setFormView("data");
     }
     @Override
     protected ModelAndView onSubmit(Object command) throws Exception {
         Student s1= (Student)command;
         ModelAndView mv = new ModelAndView(getSuccessView());
         mv.addObject("message", "Hello! "+s1.getName());
         return mv;
     }

}

Student.java:(java model class)

package model;

public class Student {
    String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Student() {
    }
    public Student(String name) {
        this.name = name;
    }
}

dispatcher-servlet.xml:(xml file description)

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">        
        <props>
            <prop key="index.htm">indexController</prop>
            <prop key="data.htm">dataController</prop>
            <prop key="/login.htm">loginDataController</prop>
        </props>           
    </property>
</bean>
<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />
<bean name="dataController"
      class="org.springframework.web.servlet.mvc.ParameterizableViewController"
      p:viewName="data" />    
<bean name="indexController"
      class="org.springframework.web.servlet.mvc.ParameterizableViewController"
      p:viewName="index" />    
<bean name="Student" class="model.Student"/> 
<bean id="loginDataController" class="Controller.LoginFormController"/>

Can anyone provide help finding a solution?

4

1 回答 1

0
<form:form commandName="login" method="POST" name="login">

spring 的表单标签的 commandName 属性是暴露表单对象的模型属性的名称。默认为“命令”。
资料来源:Spring Form TLD 文档

现在回到你的情况:
“登录”应该作为模型属性(请求属性)可用,你没有这样做,因此你的错误。

所以修改你的 onSubmit方法为

@Override
     protected ModelAndView onSubmit(Object command) throws Exception {
         Student s1= (Student)command;
         ModelAndView mv = new ModelAndView(getSuccessView());
         mv.addObject("login",s1);
         mv.addObject("message", "Hello! "+s1.getName());
         return mv;
     }

我还建议您通过Spring 表单上的Java Dzone 教程

于 2013-07-23T19:42:53.327 回答