1

I'm just checking out Spring MVC framework, so I'm new to it, pls forgive the basic question. But I'm trying to figure out how the view resolver works.

I created a basic controller method to add a user like this:

 @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addUser(@ModelAttribute("user") User user, BindingResult result) {
        logger.entry(user, result);
        userRepository.save(user);

        String returnString="redirect:members/confirmation";
        logger.exit(returnString);
        return returnString;
    }

I want it to redirect to the members/confirmation.jsp page under the WEB-INF/pages folder. It's just a simple JSP that says "thanks for registering". The redirect is so it won't allow form double posting. (This is just a basic test app)

I configured the internalviewresolver in the mvc-dispatcher-servlet.xml configuration file:

 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
        <property name="order" value="0"/>
    </bean>

But it's not resolving to the confirmation view, and just 404's.

What am I missing here?

Thanks!

Rich

Richard G
  • 5,243
  • 11
  • 53
  • 95
  • Also if I take out the redirect prefix, it works fine. – Richard G Jul 05 '14 at 07:00
  • You're confising redirect and forward. redirect means sending a response to the browser, asking it to send back a request to another location. What you want is a forward. Don't use the redirect: prefix. – JB Nizet Jul 05 '14 at 07:01
  • redirect is the recommended method to stop duplicate form submission issues right? http://stackoverflow.com/questions/2324931/duplicate-form-submission-in-spring – Richard G Jul 05 '14 at 07:50
  • The POST-REDIRECT-GET pattern is indeed a good idea to implement. But then you must redirect to the URL of a resource accessible from the outside, and not to a view. The path after the `redirect:` prefix must be the path to one of your controller methods, accessible using a GET request: once you've created a user, you redirect to the URL allowing to list all the users for example, or to the URL allowing to view the created user details. – JB Nizet Jul 05 '14 at 08:02
  • ok cool, is there a way to neatly resolve the path to the jsp through convention rather than writing a controller method? So if I request "/users/confirmation" on a GET it will automatically resolve to "/users/confirmation.jsp". (I've been working with Rails which is pretty good at these type of conventions) – Richard G Jul 05 '14 at 08:14
  • I don't understand what you're asking. Why would you not want to go through a controller? Spring MVC is named MVC for a good reason. Where would the view get the model it needs to display if the request doesn't go through a controller? – JB Nizet Jul 05 '14 at 08:17

1 Answers1

2

Returning "redirect:members/confirmation" does a client-side redirect to members/confirmation (to this relative URL). Your application does not respond to HTTP requests to members/confirmation at the moment. Create a controller for this URI with your target JSP page as the view:

@RequestMapping(value = "/members/confirmation", method = RequestMethod.GET)
public String confirmation() {

     return "members/confirmation";
}

It's the only right way to do it.

Alexey
  • 2,542
  • 4
  • 31
  • 53