Announcement Announcement Module
Collapse
No announcement yet.
Spring 3 MVC redirect: is giving me a 404 when redirecting to another controller Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring 3 MVC redirect: is giving me a 404 when redirecting to another controller

    My view resolver:

    Code:
    <bean id="htmlView" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views" />
        <property name="suffix" value=".jsp" />
    </bean>
    My request mapping method in my controller:

    Code:
    @RequestMapping(value="/account/register", method=RequestMethod.POST)
    public String registerHandler(@Valid RegisterForm registerForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return "/account/register";
        }
        return "redirect:/account/profile";
    }
    If there are errors then it returns just fine back to the account registration page. After reading some of the official docs, it appears that redirect will try to resolve the view to the current servlet context. Since "account/profile" resides in another controller, it doesn't seem to know what to do with it. I've tried every URL combination of redirect I can think of, and none of them will redirect correctly.

  • #2
    copy here the complete error stack trace

    about /account/register

    Spring could assume that in /WEB-INF/views should exits perhaps

    /account/register.jsp (register.jsp within account)
    or
    /account/register.jsp all the bold part like a name

    the error stack trace should show more details

    Comment


    • #3
      The redirect is a client side redirect so it should match the full EXTERNAL url of the path you want to redirect to. This might include the servlet path and servlet extension which it is mapped to...

      Comment


      • #4
        Originally posted by hal10001 View Post
        My view resolver:

        Code:
        <bean id="htmlView" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views" />
            <property name="suffix" value=".jsp" />
        </bean>
        My request mapping method in my controller:

        Code:
        @RequestMapping(value="/account/register", method=RequestMethod.POST)
        public String registerHandler(@Valid RegisterForm registerForm, BindingResult bindingResult) {
            if (bindingResult.hasErrors()) {
                return "/account/register";
            }
            return "redirect:/account/profile";
        }
        If there are errors then it returns just fine back to the account registration page. After reading some of the official docs, it appears that redirect will try to resolve the view to the current servlet context. Since "account/profile" resides in another controller, it doesn't seem to know what to do with it. I've tried every URL combination of redirect I can think of, and none of them will redirect correctly.
        It turns out the problem was that the controller I was redirecting to was not actually configured correctly. So even though it was properly redirecting, the controller it redirected to could not find the right JSP view to show.

        Comment

        Working...
        X