shippi shippi - 7 months ago 38
Java Question

Spring mvc: best practice to navigate between two jsps

I am using spring mvc + spring security to build a simple web application. I implemented the login/logout (spring mvc) but i want to provide also a registration jsp accessible directly from the login.jsp. Basically i just need a simple link from the login.jsp to the regsiter.jsp without passing any parameters or whatever. I just want to ask you what is the best practice to achieve this?

Is there any way to navigate directly between the two jsps without routing the request down to the controller? (or this is not really a spring mvc way?) To be honest my only "problem" is just to have a separate method in the register controller which does nothing just routes the request to the register jsp. I mean:


public class RegisterController {

@RequestMapping(value="/view_register.htm", method = RequestMethod.POST)
/** Navigates to the register page */
public String navigateToRegistration(ModelMap model) {
return "register";

@RequestMapping(value="/register.htm", method = RequestMethod.POST)
/** Handles request from the registration page and registers the user */
public String registerUser(ModelMap model) {

// hard stuff to register the user
return "welcome";



<td>Don't have an account yet.</td>
<td> <a href="<c:url value='view_register.htm' />">Register here</a>


<form name='registration' action="<c:url value='register.htm' />"

WEB-INF/web.xml - routing each request to spring mvc



<beans xmlns=""

<import resource="classpath:/applicationContext.xml" />

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


Is my implementation correct? This is the mvc way? I just want to know whether i understand this well and not doing any antipattern stuff. The thing what confuses me is really this additional small method in the regsitration controller which does nothing just navigates from the login to register. Is there at least any naming convention for this kind of methods? Would you recommend rather a ForwardController which could act as a Manager/Dispatcher and would just manage this kind of navigation requests?


Your structure looks correct: One exception is that the /view_register.htm should be a GET, not a POST. Spring MVC uses a front controller pattern, and so every request does go through a controller. When the only thing a request does is to return a view, you can configure your mappings using the ParameterizableViewController instead of coding a controller, but I tend to code a method like you have when there are multiple related requests within a controller. As your app evolves, you might find there is some session setup you will add, and this just keeps all of the registration requests in the same class.

Don't forget to configure security to allow anonymous access to your registration requests.