0

My main problem here is returning a string with a pathvariable value from one controller to another.

See here:

@RequestMapping(value = "/profile/{location}")
public ModelAndView profile(@PathVariable("location") String location) throws Exception {
    return new ModelAndView("profile", "*", *);
}

@RequestMapping(value = "/records", method = "RequestMethod.POST") 
public String inRecords(@Valid User user, BindingResult result) {
    if(result.hasErrors()) {
        return "profile/system";  
    }
    else {
        .....
        return "somewhere";
    }
}

My problem here is the return "profile/system" going to WEB-INF/views/profile/system.jsp. Am I doing anything wrong with @PathVariable or with the return statement itself?

Any suggestions?

5
  • Look into redirect attributes. Commented Feb 19, 2014 at 3:53
  • tried it just now, but errors still won't show. Commented Feb 19, 2014 at 4:00
  • The errors don't just show up on their own. It depends how you use them. Commented Feb 19, 2014 at 4:01
  • I have a <c:if> for errors to show up when there are any, and since it redirected to the profile/system page, there should be. But the <c:if> result shows that there were no errors? Am I making sense? Pardon me, and thank you. Commented Feb 19, 2014 at 4:08
  • I have no idea how you are using either BindingResult or RedirectAttributes, so I can't help you. Commented Feb 19, 2014 at 4:09

1 Answer 1

1

Why you dont try something like this.

@RequestMapping(value = "/records", method = "RequestMethod.POST") 
public void inRecords(@Valid User user, HttpServletResponse response) {
    if(result.hasErrors()) {
        response.sendRedirect("/YourApp/profile/system")
    }

I think ModelAndView is taking the returned String and try to run ViewResolver that try to get the jso, avoid that calling or redirecting the request directly to the needed endpoint.

Or If you want to keep modelAndView use this

return new ModelAndView("redirect:/profile/system");
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you :) ! I figured it out just a while ago, even though its bad practice, I just copied the contents of ModelAndView profile under the result.hasErrors() and used return new ModelAndView("profile", "*", *).

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.