This might be an obvious question. But am not able to figure it out so far.
In my Spring application, I make a GET request to the following url
http://www.example.com/firstpage
This request goes to the front controller where I have a request mapping as below:
@RequestMapping(value = "/firstpage")
public String handlerMethod(HttpServletRequest request, HttpSession session)
throws CustomException {
...
return "secondpage";
}
This “secondpage” corresponds to secondpage.jsp and its contents are correctly displayed.
But the problem is the browser URL still displays
http://www.example.com/firstpage
Why is this happening? Any suggestions as to how to make the browser URL change?
Also does Spring have any default support for encoding URL ?
One of the beauties of Spring MVC is that the view is totally separate from the controller. So your controller maps to the URL path “/firstpage” and in response can render any view. It may be a JSP, JSON, PDF, whatever type of view. Your view resolver configuration determines which view is used.
In your case, you are returning “secondpage” which simply tells spring to look for the view named secondpage, in accordance with your view resolver configuration which probably looks in WEB-INF for secondpage.jsp or something like that. It’s still just a view.
If you would like it to do something else, you can return “redirect:secondpage” which will tell the browser to actually redirect to “/secondpage” which I believe will change the url in the address bar, but it will also want to go to a controller mapped to “/secondpage” or will need a view mapped without a controller.