1

I have a controller with the following methods:

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String register(Model model) {
        model.addAttribute("user", new User());
        return "register";
    }

    @RequestMapping(value = "/registration", method = RequestMethod.POST)
    public String registration(@ModelAttribute("user") User user, BindingResult bindingResult, RedirectAttributes redirectAttributes) {
        UserValidator validator = new UserValidator();
        validator.validate(user, bindingResult);

        if (bindingResult.hasErrors()) {
            redirectAttributes.addFlashAttribute(BindingResult.MODEL_KEY_PREFIX + "user", bindingResult);
            redirectAttributes.addFlashAttribute("user", user);

            return "redirect:/register";
        }
        ...
    }

My jsp:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<form:form method="POST" modelAttribute="user" action="/registration">
    <div>
        <label>First Name</label>
        <form:input path="firstName"/>
        <form:errors path="firstName"/>
    </div>
    <div>
        <label>Last Name</label>
        <form:input path="lastName"/>
        <form:errors path="lastName"/>
    </div>
    <div>
        <label>E-mail</label>
        <form:input path="email"/>
        <form:errors path="email"/>
    </div>
    <div>
        <label>Password</label>
        <form:password path="password"/>
        <form:errors path="password"/>
    </div>

    <input type="submit" value="Submit"/>
</form:form>

However, if there are errors they are not shown after the redirect because I create a new user every time the /register is called. Is there a way to fix this without storing the user in a session?

I can create another method just for this case so I can redirect to /register2 without losing the user:

@RequestMapping(value = "/register2", method = RequestMethod.GET)
public String register2() {
    return "register";
}

It worked like I wanted, but it's a very bad approach.

3
  • Why do a redirect for form-binding/validation errors? Typically you would only do a redirect on success to prevent a browser refresh from resubmitting acceptable data. Commented Jul 8, 2013 at 20:51
  • I don't want to show .../registration in the address bar if there are errors. Commented Jul 8, 2013 at 21:05
  • Can you POST to /register instead of /registration? You don't want to show registration in the address bar but you don't mind showing register2? Commented Jul 8, 2013 at 21:24

1 Answer 1

1

How about checking first if a model named "user" exist before creating one on "/register" ?

if(!model.containsAttribute("user")) model.addAttribute("user", new User());

Also if I'm not wrong you can simply use @ModelAttribute on parameter and it will serve similar purpose

@RequestMapping(value = "/register", method = RequestMethod.GET)
public String register(@ModelAttribute("user") User user) {
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, the first one worked just like I wanted! The second one didn't though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.