Question

Hi I am facing an issue and looked all over internet but still not able to find out the root cause. I am posting my code snippet please help me out as I am new to spring 3. I am using modelAttribute in form and what I want that in controller all the values from request should be backed in the object so that I can perform validation and other business logic. I know there is mistake only in my controller.

1) index.jsp

<form:form action="login" method="POST" modelAttribute="login">
        <table>
            <tr><td>User Id:</td><td><form:input path="userId"/></td></tr>
            <tr><td>Password:</td><td><form:password path="userPassword"/></td></tr>
            <tr><td></td><td><input type="submit" value="Login"/></td></tr>
        </table>
    </form:form>

2) Controller

@RequestMapping(value="/login/", method=RequestMethod.POST)
public String login(@ModelAttribute("login") @Valid Login login,BindingResult result)
    {

        System.out.println("We have entered into controller class");
        if(result.hasErrors())
        {
            System.out.println("Errors:"+result.getFieldError("userReject"));
                return "redirect:/login";
                }
                else
                {
        return "home";}
    }
}

3) JBoss Log

04:35:29,067 ERROR [org.springframework.web.servlet.tags.form.InputTag] (http--0.0.0.0-8090-1) Neither BindingResult nor plain target object for bean name 'login' available as request attribute: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'login' available as request attribute
        at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141) [spring-webmvc-3.0.5.Release.jar:3.0.5.RELEASE]
Était-ce utile?

La solution

The problem is not in the method you posted, which handles the login form submission. It's in the method used to display the form. The form needs to populate its fields from a bean named "login", and you didn't place any bean named "login" in the model, i.e. in a request attribute.

Side note: a login form should never use GET. It should use POST. You really don't want the password to appear in the browser address bar. And you don't want it to appear in the browser history, the server and proxy logs, etc.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top