在 Spring MVC 3 中传递请求参数
我只想使用请求参数发送下拉列表的值。就我而言,
Kidscalcula_web/start.htm?klasid=myValueHere
我知道一种方法,但用它来做这件事听起来很不合理。如果我很无聊,我可能会编写一些 jQuery 来发布帖子并发送参数,例如。现在手动创建请求字符串听起来确实是一个非常糟糕的主意,因为 Spring 会处理这个问题。那么我怎样才能制作一个简单的表单,将我的下拉值发送到我的控制器呢?
只是我在任何地方都找不到如此琐碎的东西,你们中的一个人可能可以快速帮助我。我想控制器会像以下一样琐碎:
@RequestMapping(value = "post")
public String postIndex(@RequestParam("klasid") String klasid, HttpServletResponse response,
HttpServletRequest request) {
}
但我真的找不到任何关于如何制作 JSP 的示例向我发送该值。这可以通过
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
例如,您可以拥有一个带有名为
klasid
字段的命令类,而不是使用@RequestParam("klasid")
,Spring 会将所有这些绑定在一起:当您考虑到表单通常具有多个参数,并且使用
@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 calledklasid
, and Spring would bind the whole lot together: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.您甚至不需要标签库来发送此请求。您可以使用
method = "GET"
创建最简单的 HTML 表单(method
的默认值是多少):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 ofmethod
):