本文介绍了Spring MVC 如何在我的控制器方法中获取 GET HTTP 请求的参数值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此期间,我正在研究 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>

正如您通过此链接所看到的,我正在执行一个 HTTP GET 请求,其中包含一个包含值:bar"的foo"参数.

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)";
}

此方法管理具有名为foo"的参数的 HTTP 请求(仅 GET 类型)

This method manage HTTP Request (only GET type) that have a parameter named "foo"

如何获取此参数的值(bar")并将其放入我的 by Parameter 方法代码中的变量中?

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 注释:

As explained in the documentation, by using an @RequestParam annotation:

public @ResponseBody String byParameter(@RequestParam("foo") String foo) {
    return "Mapped by path + method + presence of query parameter! (MappingController) - foo = "
           + foo;
}

这篇关于Spring MVC 如何在我的控制器方法中获取 GET HTTP 请求的参数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 14:05