SpringMVC中的@RequestMapping

来源:互联网 发布:windows xp主题下载 编辑:程序博客网 时间:2024/06/05 19:00

关于@RequestMapping注释中的参数:
params参数:用来处理带有指定参数的请求。
可以用它来请求同一个父路径却下的不同方法。

@Controller@RequestMapping("/getRequest.do")public class TestController {    @RequestMapping(params = "method=text1")    public String getRequest01(HttpServletRequest request, HttpServletResponse response) {        return "sucess";    }    @RequestMapping(params = "method=text2")    public String getRequest02(HttpServletRequest request, HttpServletResponse response) {        return "fail";    }}

我们可以看到上面这段代码有两个不同的方法,getRequest01()与getRequest02()。这两个方法都有相同的父路径:/getRequest。他们都使用了@RequestMapping注释。在注释中,params参数的属性不同。

在我们发送请求的时候,url中的特定参数值会决定访问哪个方法。

url: getRequest.do?method=text1
这个就会访问getRequest01这个方法。

url: getRequest.do?method=text2
这个就会访问getRequest02这个方法。

produces 参数:
我们可以看一下源码中的注释

The producible media types of the mapped request, narrowing the primary mapping.     * <p>The format is a single media type or a sequence of media types,     * with a request only mapped if the {@code Accept} matches one of these media types.     * Examples:     * <pre class="code">     * produces = "text/plain"     * produces = {"text/plain", "application/*"}     * </pre>     * Expressions can be negated by using the "!" operator, as in "!text/plain", which matches     * all requests with a {@code Accept} other than "text/plain".     * <p><b>Supported at the type level as well as at the method level!</b>     * When used at the type level, all method-level mappings override     * this produces restriction.     * @see org.springframework.http.MediaType
  • 只有请求头的accept中的类型和produces的值相匹配,才会进行映射。produces = {"text/plain", "application/*"}
  • 也可以指定通过!来指定不映射的类型。produces="!text/plain"
  • 当我们在类上使用这个属性的时候,类中方法的映射都会被覆盖

未完待续…