Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC: RequestMapping both class and method

Tags:

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> 
like image 803
Orvyl Avatar asked Mar 28 '14 01:03

Orvyl


People also ask

Which methods uses RequestMapping with a class?

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.

Which of these can be the return type of a method annotated with @RequestMapping in Spring?

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.

What will happen if handler methods @RequestMapping?

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.

Is @RequestMapping required?

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.


2 Answers

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

like image 59
chrylis -cautiouslyoptimistic- Avatar answered Oct 18 '22 12:10

chrylis -cautiouslyoptimistic-


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

like image 44
Binz Avatar answered Oct 18 '22 12:10

Binz