在Spring MVC 3中传递请求参数 [英] Pass a request parameter in Spring MVC 3

查看:116
本文介绍了在Spring MVC 3中传递请求参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想通过请求参数发送我的下拉列表的值.就我而言

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

Kidscalcula_web/start.htm?klasid=myValueHere

我知道这样做的方法,但是将其用于此听起来很不合理.如果我很无聊,我可能会写一些jQuery来发布并发送参数,现在手动创建我的请求字符串听起来真的是一个非常糟糕的主意,因为Spring会解决这个问题.那么,如何制作一个简单的表单,将我的dropdownvalue发送到我的控制器?

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) {
}

但是我真的找不到任何有关如何制作JSP来向我发送该值的示例. <form> taglib是否有可能?

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 ?

推荐答案

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

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.

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

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) { /../ }

当您认为表单通常具有多个参数,并且使用@RequestParam声明所有表单时会很麻烦.

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

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

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.

这篇关于在Spring MVC 3中传递请求参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆