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>
The @RequestMapping annotation can be applied to class-level and/or method-level in a controller. The class-level annotation maps a specific request path or pattern onto a controller. You can then apply additional method-level annotations to make mappings more specific to handler methods.
A Callable can be returned when the application wants to produce the return value asynchronously in a thread managed by Spring MVC. A DeferredResult can be returned when the application wants to produce the return value from a thread of its own choosing.
In the code snippet above, the method element of the @RequestMapping annotations indicates the HTTP method type of the HTTP request. All the handler methods will handle requests coming to the same URL ( /home), but will depend on the HTTP method being used.
A @RequestMapping on the class level is not required. Without it, all paths are simply absolute, and not relative. This means if you specify the classlevel annotations, the url shall be relative, so for register it shall be /user/register(URL to Handler mapping) and likewise.
You don't need the /
in the method's mapping. Just map it to ""
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With