如何在Spring MVC中显式获取post数据? [英] How to explicitly obtain post data in Spring MVC?

查看:160
本文介绍了如何在Spring MVC中显式获取post数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法获取帖子数据本身?我知道spring处理将数据绑定到java对象。但是,考虑到我要处理的两个字段,如何获取该数据?

Is there a way to obtain the post data itself? I know spring handles binding post data to java objects. But, given two fields that I want to process, how can I obtain that data?

例如,假设我的表单有两个字段:

For example, suppose my form had two fields:

 <input type="text" name="value1" id="value1"/>
 <input type="text" name="value2" id="value2"/>

我如何在控制器中检索这些值?

How would I go about retrieving those values in my controller?

推荐答案

如果您正在使用其中一个内置控制器实例,那么控制器方法的其中一个参数将是请求对象。您可以调用 request.getParameter(value1)来获取POST(或PUT)数据值。

If you are using one of the built-in controller instances, then one of the parameters to your controller method will be the Request object. You can call request.getParameter("value1") to get the POST (or PUT) data value.

如果您使用的是Spring MVC注释,则可以在方法的参数中添加带注释的参数:

If you are using Spring MVC annotations, you can add an annotated parameter to your method's parameters:

@RequestMapping(value = "/someUrl")
public String someMethod(@RequestParam("value1") String valueOne) {
 //do stuff with valueOne variable here
}

这篇关于如何在Spring MVC中显式获取post数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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