22

Is this possible?

@Controller
@RequestMapping("/login")
public class LoginController {

    @RequestMapping("/")
    public String loginRoot() {
        return "login";
    }

    @RequestMapping(value="/error", method=RequestMethod.GET)
    public String loginError() {
        return "login-error";
    }

}

I got a 404 error when accessing localhost:8080/projectname/login but not in localhost:8080/projectname/login/error.

Here's my web.xml project name

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<servlet>
    <description></description>
    <servlet-name>projectname</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>projectname</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

3 Answers 3

29

You don't need the / in the method's mapping. Just map it to "".

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

1 Comment

And this will work because "" will be relative to "/login" so still /login.
11

You don't need to "/" and you need to also add the request method.

@RequestMapping(method = RequestMethod.GET)
public String loginRoot() {
    return "login";
}

Consider using spring mvc tests to make the process of testing these scenarios easier:

https://spring.io/blog/2012/11/12/spring-framework-3-2-rc1-spring-mvc-test-framework

2 Comments

Just ran a quick test, in your case you can just have @RequestMapping and it will work. The request method is GET by default.
@BineshGunaratne The request method is not GET by default. If you omit the method attribute, all request methods will be matched.
6

Yes that is possible. The path in @RequestMapping on the method is relative to the path on the class annotation.

With your current setup, loginRoot() will handle requests to

localhost:8080/projectname/login/

Assuming you don't have anything else in your configuration preventing this.

7 Comments

that's what I believe too but Im getting a 404 error accessing localhost:8080/projectname/login and I dont know why, did I miss something?
Do you have a JSP named "login.jsp" in the same directory as your "error.jsp"?
@orvyl It works for me. Can we see your web.xml and servlet context configuration? Your mappings are probably different and preventing this.
@SotiriosDelimanolis What Spring version are you using? I just ran into a very similar issue with 4.0.0.RELEASE, and using an empty mapping string fixed it. Note that the question doesn't have the trailing slash on the URL, and yours does.
@chrylis I'm on 4.0.2. This behavior has existed since at least 3.1, iirc. I'm suggesting they change from using /login to using /login/ for the request to reach the handler. Again, this might be an issue with the rest of the configuration.
|

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.