Spring 3 MVC - Form Failure Causes Exception When Reloading JSP

Posted by jboyd on Stack Overflow See other posts from Stack Overflow or by jboyd
Published on 2010-06-09T17:20:19Z Indexed on 2010/06/09 17:22 UTC
Read the original article Hit count: 252

Filed under:
|
|
|
|

Using Spring 3 MVC, please bear with the long code example, it's quite simple, but I want to make sure all relevant information is posted.

Basically here is the use case:

There is a registration page, a user can login, OR fill out a registration form. The login form is a simple HTML form, the registration form is a more complicated, Spring bound form that uses a RegistrationFormData bean. Here is the relevant code:

UserController.java

...

@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(Model model) {
    model.addAttribute("registrationInfo", new ProfileAdminFormData());
    return "login";
}

...

@RequestMapping(value = "/login.do", method = RequestMethod.POST)
public String doLogin(
        @RequestParam(value = "userName") String userName,
        @RequestParam(value = "password") String password,
        Model model) {

    logger.info("login.do : userName=" + userName + ", password=" + password);
    try {
        getUser().login(userName, password);
    } catch (UserNotFoundException ex) {
        logger.error(ex);
        model.addAttribute("loginError", ex.getWebViewableErrorMessage());
        return "login";
    }
    return "redirect:/";
}

... 

@RequestMapping(value = "/register.do")
public String register(
        @ModelAttribute(value = "registrationInfo") ProfileAdminFormData profileAdminFormData,
        BindingResult result,
        Model model) {

    //todo: redirect
    if (new RegistrationValidator(profileAdminFormData, result).validate()) {
        try {
            User().register(profileAdminFormData);
            return "index";
        } catch (UserException ex) {
            logger.error(ex);
            model.addAttribute("registrationErrorMessage", ex.getWebViewableErrorMessage());
            return "login";
        }
    }
    return "login";
}

and the JSP:

...
<form:form commandName="registrationInfo" action="register.do">
...

So the problem here is that when login fails I get an exception because there is no bean "registrationInfo" in the model attributes. What I need is that regardless of the path through this controller that the "registrationInfo" bean is not null, that way if login fails, as opposed to registration, that bean is still in the model.

As you can see I create the registrationInfo object explicitly in my controller in the method bound to "/login", which is what I thought was going to be kind of a setup method" Something doesn't feel right about the "/login" method which sets up the page, but I needed to that in order to get the page to render at all without throwing an exception because there is no "registrationInfo" model attribute, as needed by the form in the JSP

© Stack Overflow or respective owner

Related posts about java

Related posts about web