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

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

问题描述

有没有办法自己获取post数据?我知道 spring 处理将 post 数据绑定到 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 对象.您可以调用 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 中显式获取发布数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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