Java 如何检查是否设置了get参数?

Java 如何检查是否设置了get参数?,java,spring,controller,Java,Spring,Controller,是否可以在一个控制器处理程序中区分这两个请求URI: http://my-uri/ http://my-uri/?with_empty_param HttpServletRequest对象有一个映射参数名称及其值的ParameterMap对象 通过这个映射,我们可以检查servlet请求中是否传递了参数 // Check if with_empty_param parameter exists if (request.getParameterMap().containsKey("wi

是否可以在一个控制器处理程序中区分这两个请求URI:

http://my-uri/
http://my-uri/?with_empty_param 

HttpServletRequest对象有一个映射参数名称及其值的
ParameterMap
对象

通过这个映射,我们可以检查servlet请求中是否传递了参数

// Check if with_empty_param parameter exists
    if (request.getParameterMap().containsKey("with_empty_param ")) {
        String with_empty_param = request.getParameter("with_empty_param ");
    }
如果要使用Spring,可以执行以下操作:

@RequestMapping(value = {"/init"}, method = RequestMethod.GET)
public String methodName(
@RequestParam Map<String,String> allParams, ModelMap model) {
 if (allParams.containsKey("with_empty_param ")) {
  ...
 }
@RequestMapping(value={/init”},method=RequestMethod.GET)
公共字符串方法名(
@RequestParam映射所有参数,ModelMap模型){
if(allParams.containsKey(“with_empty_param”)){
...
}

HttpServletRequest对象有一个映射参数名称及其值的
ParameterMap
对象

通过这个映射,我们可以检查servlet请求中是否传递了参数

// Check if with_empty_param parameter exists
    if (request.getParameterMap().containsKey("with_empty_param ")) {
        String with_empty_param = request.getParameter("with_empty_param ");
    }
如果要使用Spring,可以执行以下操作:

@RequestMapping(value = {"/init"}, method = RequestMethod.GET)
public String methodName(
@RequestParam Map<String,String> allParams, ModelMap model) {
 if (allParams.containsKey("with_empty_param ")) {
  ...
 }
@RequestMapping(value={/init”},method=RequestMethod.GET)
公共字符串方法名(
@RequestParam映射所有参数,ModelMap模型){
if(allParams.containsKey(“with_empty_param”)){
...
}
注释有一个参数,您可以使用它

@RequestMapping
public void method1() {}
检查一下参数

@RequestMapping(params={"param"})
public void method2() {}
您还可以使用
来取消检查,因此如果该参数不存在

@RequestMapping(params={"!param"})
public void method3() {}
注释有一个参数,您可以使用它

@RequestMapping
public void method1() {}
检查一下参数

@RequestMapping(params={"param"})
public void method2() {}
您还可以使用
来取消检查,因此如果该参数不存在

@RequestMapping(params={"!param"})
public void method3() {}

使用
@RequestMapping
params
参数。它的值在两种情况下都为空吗?不确定您对该注释的意思…如果它不存在(无值或不存在)没有值…使用
@RequestMapping
params
参数。它的值在两种情况下都为空吗?不确定您对该注释的意思…如果它不存在(没有值或不存在)没有价值…听起来很简单,如果没有其他方法,我会这样做。听起来很简单,如果没有其他方法,我会这样做。