Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you have two URL routes mapped to the same handler method in Spring MVC (3.0)?

I have a a userPanel method mapped to the /user/panel URL route:

@RequestMapping(value = "/user/panel", method = RequestMethod.GET)
public final String userPanel(HttpServletRequest request, ModelMap model)

However, I would also like the userPanel method to handle the route /panel without creating a separate method such as this:

@RequestMapping(value = "/panel", method = RequestMethod.GET)
public final String panel(HttpServletRequest request, ModelMap model)

Is there a way to have the userPanel method handle both routes to avoid duplication?

like image 364
Justin Long Avatar asked Oct 12 '10 22:10

Justin Long


People also ask

How does Spring MVC handle multiple users?

Based on url matching it calls respective method. all my methods are singleton. Now when two users are opening app at same time spring is able to run them parallelly and give results to them.

What is URL mapping in Spring MVC?

Tags:spring mvc | url mapping. In Spring MVC application, the SimpleUrlHandlerMapping is the most flexible handler mapping class, which allow developer to specify the mapping of URL pattern and handlers explicitly. The SimpleUrlHandlerMapping can be declared in two ways.

What is difference between @GetMapping and @RequestMapping?

@RequestMapping is used at the class level while @GetMapping is used to connect the methods. This is also an important Spring MVC interview question to knowing how and when to use both RequestMapping and GetMapping is crucial for Java developers.

What is true about @RequestMapping annotation in Spring MVC?

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.


1 Answers

@RequestMapping can take multiple paths:

@RequestMapping(value = {"/user/panel", "/panel"}, method = RequestMethod.GET)
public final String userPanel(HttpServletRequest request, ModelMap model)
like image 127
skaffman Avatar answered Sep 28 '22 14:09

skaffman