spring 在spring mvc中将多个参数从视图传递给控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20949777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:26:36  来源:igfitidea点击:

Passing multiple parameters from view to controller in spring mvc

springspring-mvcjstl

提问by Viks

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

我想将参数从我的 jsp 页面传递到控制器。我找到了一种方法——点击——

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

但是如果我想传递多个参数,那么我必须添加更多 @RequestParam 作为 -

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

所以我的问题是 - 还有其他方便的方法吗?因为在上述方式中,即在参数越来越多的情况下,方法参数的大小将增加。我是春天的新人。请帮忙。

回答by Sotirios Delimanolis

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.

我不知道您对其他方便的方法有何期望。这是最方便的方式。您可以准确指定所需的参数,而这些参数正是 Spring 给您的。

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.

首先,编写 Spring MVC 是为了让您的生活更轻松,除其他原因外,还为了尽可能多地删除对 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.

其次,拥有大量方法参数绝对没有错。您甚至不需要自己调用该方法,Spring 是,并且它拥有使用正确参数调用它所需的所有工具。

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

最后,重点@RequestParam是让您不要使用HttpServletRequest#getParameter(String). 这样的方法

@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

我希望你看到你如何编写更多样板、复杂的代码。如果您需要为缺少的请求参数添加默认值,情况会变得更糟。使用 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 @RequestParamwhere appropriate.

如果您的 API 需要更多请求参数,请继续添加它们,但让您的生活变得简单并@RequestParam在适当的地方使用。

回答by Will Keeling

As Sotirios says, the @RequestParamannotation is already very convenient.

正如Sotirios所说,@RequestParam注解已经很方便了。

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

如果你有很多请求参数,你总是可以指定 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");
    ...
}

回答by Mebin

Using request.getParameteris not the Spring way. This is a very basic problem which was obviously thought of when Spring was made, otherwise whats the point of having a framework like this.

使用request.getParameter不是 Spring 的方式。这是一个很基础的问题,很明显在做Spring的时候就想到了,否则有这样的框架有什么意义。

If you have many parameters make a DTO class with all those parameters and then you can write the controller method as below.

如果您有许多参数,则使用所有这些参数创建一个 DTO 类,然后您可以编写如下控制器方法。

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