在 Spring MVC 3 中传递请求参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4139538/
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 04:11:51  来源:igfitidea点击:

Pass a request parameter in Spring MVC 3

springspring-mvchttprequest

提问by toomuchcs

I just want to send the value of my dropdownlist with a requestparameter. In my case being

我只想发送带有请求参数的下拉列表的值。在我的情况下

Kidscalcula_web/start.htm?klasid=myValueHere

I know a way of doing this but it sounds so irrational to use it for this. If I was bored I'd probably write some jQuery to do a post and send the parameter for instance.Now it really sounds like a very bad idea to manually make my requeststring, since Spring takes care of that. So how could I make a simple form that just sends my dropdownvalue to my controller?

我知道一种这样做的方法,但将它用于此听起来很不合理。如果我感到无聊,我可能会写一些 jQuery 来做一个帖子并发送参数,例如。现在手动创建我的请求字符串听起来真的是一个非常糟糕的主意,因为 Spring 会处理这些。那么我如何制作一个简单的表单,将我的下拉值发送到我的控制器?

It's just that I can't find something so trivial anywhere, and one of you can probably help me out quickly.I suppose the controller would be just as trivial as:

只是我在任何地方都找不到如此微不足道的东西,你们中的一个人可能可以快速帮助我。我想控制器会像以下一样微不足道:

    @RequestMapping(value = "post")
    public String postIndex(@RequestParam("klasid") String klasid, HttpServletResponse response,
            HttpServletRequest request) {
}

But I really can't find any examples on how to make a JSP to send me that value. Is this possible with the <form>taglib ?

但我真的找不到任何关于如何制作 JSP 来向我发送该值的示例。这可能与<form>taglib 吗?

回答by skaffman

The <form>taglib is generally used with form-backing command objects, rather than being bound to the controllers using individual @RequestParamarguments. This is why you won't see any documentation examples of that combination being used together.

<form>标签库通常与形背衬命令对象使用,而不是被结合到使用单独的控制器@RequestParam参数。这就是为什么您不会看到该组合被一起使用的任何文档示例。

For example, rather than having @RequestParam("klasid"), you'd have a command class with a field called klasid, and Spring would bind the whole lot together:

例如,不是有 ,而是@RequestParam("klasid")有一个带有名为 的字段的命令类klasid,Spring 会将所有内容绑定在一起:

@RequestMapping(value = "post")
public String postIndex(@ModelAttribute MyCommandClass command) { /../ }

This makes sense when you consider that forms typically have multiple parameters, and it'd get cumbersome to declare them all using @RequestParam.

当您考虑到表单通常具有多个参数时,这是有道理的,并且使用 @RequestParam.

Having said that, you can still do it - any form controls will generate request parameters that @RequestParamcan bind to, but if you choose to deviate from Spring MVC's form-backing command pattern, then it's quite awkward.

话虽如此,你仍然可以这样做——任何表单控件都会生成@RequestParam可以绑定到的请求参数 ,但是如果你选择偏离 Spring MVC 的表单支持命令模式,那么就相当尴尬了。

回答by axtavt

You don't even need a taglib to send this request. You can create a simpliest HTML form with method = "GET"(what is the default value of method):

你甚至不需要 taglib 来发送这个请求。您可以创建一个最简单的 HTML 表单method = "GET"(默认值是什么method):

<form action = "...">
    <select name = "klasid">
        <option value = "value1">Option 1</option>
        <option value = "value2">Option 2</option>
        <option value = "value3">Option 3</option>
    </select>
    <input type = "submit" />
</form>