Spring Multiple MVCs with the same @RequestMapping

I am trying to create a web application that allows the user to log in from the index.htm landing page. this action is compared with LoginController, which after a successful login returns the user back to the same index.htm , but as a registered user, he welcomes the user with a welcome message.

index.htm also has another form called itemform, which allows the user to add the name of the item as text. This action is controlled by itemController.

My problem is that my LoginController and itemController have the same @RequestMapping and therefore I get this error:

An error occurred while creating the bean with the name "org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping # 0" defined in the ServletContext resource [/WEB-INF/tinga-servlet.xml]: The bean was not initialized; The nested exception is java.lang.IllegalStateException: the [loginController] handler could not be matched with the URL [/index.htm]: the [ com.tinga.LoginController@bf5555 ] handler has already been processed.

Unable to map [loginController] handler to URL [/index.htm]: [ com.tinga.LoginController@bf5555 ] handler is displayed.

How do I solve this problem?

+4
source share
4 answers
 @RequestMapping(value="/login.htm") public ModelAndView login(HttpServletRequest request, HttpServletResponse response) { // show login page if no parameters given // process login if parameters are given } @RequestMapping(value="/index.htm") public ModelAndView index(HttpServletRequest request, HttpServletResponse response) { // show the index page } 

Finally, you need a servlet filter to intercept requests, and if you are not requesting the login.htm page, you will need to verify that the user is logged on. If you allow filterchain to continue. If not, you forward /login.htm forward

 public class LoginFilter implements Filter { public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpServletRequest = (HttpServletRequest)request; boolean loggedIn = ...; // determine if the user is logged in. boolean isLoginPage = ...; // use path to see if it the login page if (loggedIn || isLoginPage) { chain.doFilter(request, response); } else { request.getRequestDispatcher("/login.htm").forward(request, response); } } } 

And in web.xml

Example from my deployment descriptor:

 <filter> <filter-name>LoginFilter</filter-name> <filter-class>LoginFilter</filter-class> </filter> <filter-mapping> <filter-name>LoginFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping> 

This is all from memory, but this should give you a general idea of ​​how to do this.

+2
source

Request matching for all controllers must be unique in Spring MVC.

0
source

Perhaps in your controllers with the same @RequestMapping you should define a method (GET, POST ...), for example, as follows:

 @RequestMapping(value="/index.htm", method = RequestMethod.GET) @RequestMapping(value="/index.htm", method = RequestMethod.POST) 

The controller with the GET method that you use to visualize the form and bind data (some object) to it. A controller with the POST method that you usually use to process the presentation and validate the form

0
source

Add hidden parameters to your forms to distinguish them, and then separate them by adding the params attribute to the annotation of your post methods.

 <form:hidden name="hiddenAction" value="login" /> <form:hidden name="hiddenAction" value="item" /> @RequestMapping(method = RequestMethod.POST, params = {"hiddenAction=login"}) @RequestMapping(method = RequestMethod.POST, params = {"hiddenAction=item"}) 
0
source

All Articles