Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC @RequestMapping ... using method name as action value?

Say I have this:

@RequestMapping(value="/hello")
public ModelAndView hello(Model model){

    System.out.println("HelloWorldAction.sayHello");
    return null;      
}   

Is it possible to skip the value="hello" part, and just have the @RequestMapping annotation and have spring use the method name as the value, similar to this:

@RequestMapping
public ModelAndView hello(Model model){

    System.out.println("HelloWorldAction.sayHello");
    return null;      
}

Thanks!

===================EDIT=====================

Tried this but not working:

@Controller
@RequestMapping(value="admin", method=RequestMethod.GET)
public class AdminController {

    @RequestMapping
    public ResponseEntity<String> hello() { 
      System.out.println("hellooooooo");
    }


}
like image 212
mjs Avatar asked Mar 15 '12 11:03

mjs


People also ask

Can we use @RequestMapping at method level?

The @RequestMapping annotation can be applied to class-level and/or method-level in a controller.

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.

Which of the following is the method where RequestMapping is used with RequestParam?

The @RequestParam annotation is used with @RequestMapping to bind a web request parameter to the parameter of the handler method.

What is the use of @RequestMapping in Spring MVC?

RequestMapping annotation is used to map web requests onto specific handler classes and/or handler methods. @RequestMapping can be applied to the controller class as well as methods.


2 Answers

Try to add "/*" on the request mapping value of the class

@Controller
@RequestMapping(value="admin/*")
public class AdminController {

    @RequestMapping
    public ResponseEntity<String> hello() { 
      System.out.println("hellooooooo");
    }
}

You can go the page http://localhost:8080/website/admin/hello

like image 182
BasicCoder Avatar answered Nov 11 '22 06:11

BasicCoder


It should work if you move the RequestMethod on your specific method:

@Controller
@RequestMapping(value="admin")
public class AdminController {

    @RequestMapping(method=RequestMethod.GET)
    public ResponseEntity<String> hello() { 
      System.out.println("hellooooooo");
    }
}

and access it through http://hostname:port/admin/hello

Have a look here: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping

Good luck

like image 36
bugske Avatar answered Nov 11 '22 06:11

bugske