Question

I looking for a way to find move by method in Spring mvc project.

For instance, I want to go getInfo method in CommonController.java when URL comes "localhost:8080/myproject/common?m=getInfo".

What I did always done is something like below.

@Controller
@RequestMapping("/common")
public class CommonController {
  @RequestMapping("/getInfo")
  public ModelAndView getInfo() throws Exception {
    System.out.println("Get Information from Common controller");
    return new ModelAndView("index", "model", new Object());
  }
}

In this way, I can access to "localhost:8080/common/getInfo". However, what I want to do is access as a parameter. It should be "localhost:8080/common?m=getInfo".

How can I make that? I saw several examples which use MultiActionController. Is the usage same with 3.X version also? Should I add controller to root-context.xml?(which is I really do not want to do :< Can I use just <context:component-scan> and other annotations?)

Thanks.

Was it helpful?

Solution

You could use @RequestParam annotation :

@Controller
@RequestMapping("/common")
public class CommonController {

   public ModelAndView getInfo(@RequestParam("m") String param) throws Exception {
   if("getInfo".equals(param)){
       //your code
   }
}

OR directly with @RequestMapping

@Controller
public class CommonController {
   @RequestMapping(value = "/common", params = "m=getInfo")
   public ModelAndView getInfo() throws Exception {
       //your code here
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top