Reputation: 2775
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>
Upvotes: 22
Views: 26863
Reputation: 279910
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.
Upvotes: 6
Reputation: 123
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
Upvotes: 11
Reputation: 77177
You don't need the /
in the method's mapping. Just map it to ""
.
Upvotes: 29