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>
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
Orvyl
  • 2,635
  • 6
  • 31
  • 47

3 Answers3

29

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

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
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

Binz
  • 123
  • 5
  • Just ran a quick test, in your case you can just have @RequestMapping and it will work. The request method is GET by default. – Binz Mar 28 '14 at 01:58
  • 2
    @BineshGunaratne The request method is not GET by default. If you omit the `method` attribute, all request methods will be matched. – Sotirios Delimanolis Mar 28 '14 at 02:01
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.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • 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? – Orvyl Mar 28 '14 at 01:39
  • Do you have a JSP named "login.jsp" in the same directory as your "error.jsp"? – nickdos Mar 28 '14 at 01:41
  • @orvyl It works for me. Can we see your web.xml and servlet context configuration? Your mappings are probably different and preventing this. – Sotirios Delimanolis Mar 28 '14 at 01:41
  • @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 -cautiouslyoptimistic- Mar 28 '14 at 01:42
  • @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. – Sotirios Delimanolis Mar 28 '14 at 01:44
  • Ow! Im sorry, adding `/` at the end of the url fixed the error. So, when someone want to have a url without `/` at the last of url, set `@RequestMapping("")` instead of `@RequestMapping("/")` – Orvyl Mar 28 '14 at 01:46
  • @orvyl That's it. You actually don't need to add a value at all, just `@RequestMapping`, with any other attributes. – Sotirios Delimanolis Mar 28 '14 at 01:48