Passing multiple parameters from view to controller in spring mvc
jstl, spring, spring-mvc
Solution
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.
Problem
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.