在 Spring MVC 3 中传递请求参数

发布于 2024-10-01 06:58:39 字数 594 浏览 3 评论 0原文

我只想使用请求参数发送下拉列表的值。就我而言,

Kidscalcula_web/start.htm?klasid=myValueHere

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

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

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

但我真的找不到任何关于如何制作 JSP 的示例向我发送该值。这可以通过

taglib 实现吗?

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?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁绊已千年 2024-10-08 06:58:39

标签库通常与表单支持命令对象一起使用,而不是使用单独的 @RequestParam 参数绑定到控制器。这就是为什么您不会看到任何一起使用该组合的文档示例。

例如,您可以拥有一个带有名为 klasid 字段的命令类,而不是使用 @RequestParam("klasid"),Spring 会将所有这些绑定在一起:

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

当您考虑到表单通常具有多个参数,并且使用 @RequestParam 声明所有参数会很麻烦时,这是有道理的。

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

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

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:

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

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

把昨日还给我 2024-10-08 06:58:39

您甚至不需要标签库来发送此请求。您可以使用 method = "GET" 创建最简单的 HTML 表单(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>

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

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文