Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing multiple parameters from view to controller in spring mvc

I want to pass parameter from my jsp page to controller. I found a way to do so as- on click of-

<a href="ReqElement/reqTypeList.html?menuTab=CRM Setup">CRM Setup</a>

and in controller class-

@RequestMapping("/reqTypeList")
    public ModelAndView reqTypes(@ModelAttribute("reqElementSetup") ReqElementBean reqElBean, BindingResult result, Map<String, Object> map,HttpSession session,@RequestParam("id") String menu) {

But if i want to pass multiple parameters then i have to add more @RequestParam as -

@RequestMapping("/reqTypeList")
public ModelAndView reqTypes(@ModelAttribute("reqElementSetup") ReqElementBean reqElBean, BindingResult result, Map<String, Object> map,HttpSession session,@RequestParam("id") String id,@RequestParam("roleId") String roleid,@RequestParam("funcId") String funcid) {

So my question is - Is there any other convenient way to do so? Because in such above way i.e. in case of more and more parameters the size of method parameter will get increased. I am new for Spring. Please help.

like image 577
Viks Avatar asked Jan 06 '14 12:01

Viks


People also ask

How pass data from view to controller in spring?

Spring MVC exposes a utility class called ModelMap which implicitly extends a LinkedHashMap. In order to pass data from controller to JSP, all you have to do is add a ModelMap argument to your controller method and then populate it inside your method body using the generic addAttribute() method.

How do I use multiple RequestParam?

Similarly, if the request has more than one query string parameter, we can use @RequestParam annotation individually on the respective method arguments. Our controller reads id and name parameters from the request. Executing the respective GET request, we see that both request parameters are mapped correctly.

How do I pass multiple parameters in spring boot?

@RequestMapping(value="mno/{objectKey}", method = RequestMethod. GET, consumes="application/json") public List<Book> getBook4(@RequestParam ObjectKey objectKey) { ... } @RequestMapping(value="ghi/{objectKey}",method = RequestMethod. GET) public List<Book> getBook2(@PathVariable ObjectKey objectKey) { ... }


2 Answers

I don't know what you expect from an other convenient way to do so. This is the most convenient way. You specify exactly the parameters you want and those are the ones Spring gives you.

This is the right way to do it.


This is your problem statement

Because in such above way i.e. in case of more and more parameters the size of method parameter will get increased.

First, Spring MVC was written to make your life easier and, among other reasons, to remove as many dependencies as possible to the Servlet API.

Second, there is absolutely nothing wrong with having a lot of method parameters. You're not even calling the method yourself, Spring is and it has all the tools it needs to invoke it with the correct arguments.

Finally, the whole point of @RequestParam is so that you don't use HttpServletRequest#getParameter(String). A method like this

@RequestMapping
public String someMethod(@RequestParam String param1, @RequestParam String param2) {
    // use the request parameters
}

is the equivalent of

@RequestMapping
public String someMethod(HttpServletRequest request) {
    String param1 = request.getParameter("param1");
    String param2 = request.getParameter("param2");
    if (param1 == null) {
        throw new // some bad request exception
    }
    if (param2 == null) {
        throw new // some bad request exception
    }
    // use the request parameters
}

I hope you see how you have much more boilerplate, convoluted code to write. This becomes even worse if you need to add default values for missing request parameters. With Spring MVC

@RequestMapping
public String someMethod(@RequestParam(defaultValue = "some value") String param1)
    // use the request parameters
}

Without it

@RequestMapping
public String someMethod(HttpServletRequest request)
    String param1 = request.getParameter("param1");
    if (param1 == null) {
        param1 = "someValue";
    }
    // use the request parameters
}

If your API requires more request parameters, go ahead and add them, but make your life simple and use @RequestParam where appropriate.

like image 103
Sotirios Delimanolis Avatar answered Oct 20 '22 01:10

Sotirios Delimanolis


As Sotirios says, the @RequestParam annotation is already very convenient.

If you have a lot of request parameters, you could always specify that Spring pass the whole HttpServletRequest from which you can access the parameters you need.

@RequestMapping("/reqTypeList")
public ModelAndView reqTypes(@ModelAttribute("reqElementSetup") ReqElementBean reqElBean, BindingResult result, Map<String, Object> map, HttpServletRequest request) {

    String id = request.getParameter("id");
    String roleid = request.getParameter("roleid");
    String funcid = request.getParameter("funcid");
    ...
}
like image 35
Will Keeling Avatar answered Oct 20 '22 01:10

Will Keeling