Spring MVC如何在我的控制器方法中获取GET HTTP Request的参数值? [英] Spring MVC How take the parameter value of a GET HTTP Request in my controller method?
问题描述
在此期间我正在研究Spring MVC展示示例(可从STS dasboard下载),我有一些关于 Request Mapping
示例的简单问题:
In this period I am studing the Spring MVC showcase example (downloadable from STS dasboard) and I have some simple question about the Request Mapping
examples:
1)在我的 home.jsp
页面中,我有这个链接:
1) In my home.jsp
page I have this link:
<li>
<a id="byParameter" class="textLink" href="<c:url value="/mapping/parameter?foo=bar" />">By path, method, and presence of parameter</a>
</li>
正如您在此链接中所看到的,我正在执行一个包含foo参数的HTTP GET请求值:bar。
As you can see by this link I am doing an HTTP GET Request having a "foo" parameter containing the value: "bar".
此HTTP请求由控制器类MappingController的以下方法处理:
This HTTP Request is handled by the following method of the controller class MappingController:
@RequestMapping(value="/mapping/parameter", method=RequestMethod.GET, params="foo")
public @ResponseBody String byParameter() {
return "Mapped by path + method + presence of query parameter! (MappingController)";
}
此方法管理HTTP请求(仅 GET
type),其参数名为foo
This method manage HTTP Request (only GET
type) that have a parameter named "foo"
如何获取此参数的值(bar)并将其放入变量中在我的参数方法的代码中?
How can I take the value ("bar") of this parameter and put it in a variable inside the code of my by Parameter method?
推荐答案
如文档,使用 @ RequestParam
注释:
public @ResponseBody String byParameter(@RequestParam("foo") String foo) {
return "Mapped by path + method + presence of query parameter! (MappingController) - foo = "
+ foo;
}
这篇关于Spring MVC如何在我的控制器方法中获取GET HTTP Request的参数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!