Spring - 基于注释的控制器 - 基于查询字符串的请求映射 [英] Spring - Annotation Based Controller - RequestMapping based on query string

查看:12
本文介绍了Spring - 基于注释的控制器 - 基于查询字符串的请求映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Spring基于注解的控制器中,是否可以使用@RequestMapping将不同的查询字符串映射到不同的方法上?

In Spring annotation-based controller, is it possible to map different query strings using @RequestMapping to different methods?

例如

@RequestMapping("/test.html?day=monday")
public void writeMonday() {
}


@RequestMapping("/test.html?day=tuesday")
public void writeTuesday() {
}

推荐答案

是的,你可以使用 params 元素:

Yes, you can use the params element:

@RequestMapping("/test.html", params = "day=monday")
public void writeMonday() {
}

@RequestMapping("/test.html", params = "day=tuesday")
public void writeTuesday() {
}

您甚至可以根据参数的存在不存在进行映射:

You can even map based on the presence or absence of a param:

@RequestMapping("/test.html", params = "day")
public void writeSomeDay() {
}

@RequestMapping("/test.html", params = "!day")
public void writeNoDay() {
}

这篇关于Spring - 基于注释的控制器 - 基于查询字符串的请求映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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