4

I am trying to make a web app that allows user to login from landing page index.htm. this action is mapped with a LoginController that after successful login takes user back to same index.htm but as logged in user and greets user with welcome message.

index.htm also have another form named itemform, that allows user to add in item name as text. This action is controlled by itemController.

My problem is both my LoginController and itemController have same @RequestMapping and hence I get this error:

Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0' defined in ServletContext resource [/WEB-INF/tinga-servlet.xml]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Cannot map handler [loginController] to URL path [/index.htm]: There is already handler [com.tinga.LoginController@bf5555] mapped.

Cannot map handler [loginController] to URL path [/index.htm]: There is already handler [com.tinga.LoginController@bf5555] mapped.

How should I go about tackling this problem?

1
  • 1
    Your two controller method both mapped path "/index.htm" ? Commented Jul 23, 2012 at 6:29

4 Answers 4

2
@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'll need a servlet filter to intercept the requests and if you're not requesting the login.htm page, you'll have to check to make sure the user is logged in. If you, you allow the filterchain to proceed. If not, you issue a forward to /login.htm

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's the login page

    if (loggedIn || isLoginPage) {
        chain.doFilter(request, response);
    }
    else {
        request.getRequestDispatcher("/login.htm").forward(request, response);
    }
  }
}

And in the 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 it should give you the general idea of how to go about this.

Sign up to request clarification or add additional context in comments.

Comments

1

Request Mapping for all controllers should be unique in Spring MVC.

2 Comments

yes they do as both of LoginController and itemController return control to index.htm
this is a flaw in how you are doing your logins. What you really want is to map login controller to different paths, and use a servlet filter to check to see if the user is logged in. If not, you forward to the appropriate path for the login controller.
0

Maybe in your controllers with the same @RequestMapping you should define the Method (GET, POST...), like this way:

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

The controller with the GET method you use to render the form and bind the data (some object) to it. The controller with POST method you usually use to process the submission and validation of the form.

2 Comments

I may have not explained it correctly and may not trying to implement this correctly. The problem is in my jsp page (index.jsp) i have 2 forms (login form) with LoginController and item form with itemController. they worok fine independently (that is if i remove one form and leave the other) the only problem is when i have both of them on the same page (index.jsp) and hence for both of them i have @RequestMapping set to /index.htm. I tried your suggessted method but still does not work.
Great...maybe you should change some things in your code. For example, put the mapping name of LoginController and ItemController differently, cause in this way you see the different way they work. If I understand, you want to show the 'success' message in the same page as the form...Maybe in that case you should use some Ajax, returning a jsp page into the "success div" (let's say you have a <div> to say it was successful). Sorry if it don't work, but it's very difficult trying to solve something this way...hauhauahuaha
0

Add an hidden parameter in your forms to differentiate them, then differentiate them by adding the params attribute in 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"})

Comments

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.