Passing multiple parameters from view to controller in spring mvc

I want to pass a parameter from my jsp page to the controller. I found a way to do this, on click -

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

and in the 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 a few parameters, then I need to add another @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 this? Because in this way, in this way, that is, in the case of an increasing number of parameters, the size of the method parameter will be increased. I am new to Spring. Please, help.

+4
source share
3 answers

, . . , , , Spring.

.


, , .

-, Spring MVC , , , API .

-, , . , Spring , , .

, @RequestParam , HttpServletRequest#getParameter(String).

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

@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
}

, , , . , . Spring MVC

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

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

API , , @RequestParam, .

+14

, @RequestParam .

, , Spring HttpServletRequest, .

@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");
    ...
}
+2

request.getParameter Spring. , , , , Spring, .

If you have many parameters, enter a DTO class with all these parameters, and then you can write a controller method, as shown below.

@RequestMapping("/test")

public String test(DTO dto)
{
    // your DTO has all the fields of your request parameters given that the name of the 
    //request parameters and the variables of the dto have been kept same.
}
0
source

All Articles