在Controller函数里面,用@RequestParam传递字符串参数,则必须在做Http请求时,把对应的参数名写上,即使是不写参数值,如下所示:
http://localhost:8080/site/boxmanage/activate?imei&webid
这里面传递了两个参数imei和webid,并且没有为参数设置取值。
这样空的参数,用@RequestParam来获取,获取到的参数取值为"",而不是null。
@RequestMapping(value = "/boxmanage/activate", method = RequestMethod.POST)
@ResponseBody
public JSONObject activate(
@RequestParam(value = "imei") String imei,
@RequestParam(value = "webid") String webid) throws Exception
在程序中做一个简单测试:
if (webid==null) {
System.out.println("111");
} else if (webid.equals("")) {
System.out.println("222");
}
输出结果:
然后,用HttpServletRequest request,然后获取request中的参数也是一样的效果。有一点需要注意,用request.getParameter("para_name")这种方式,返回的结果是字符串类型,实际中需要根据需要进行强制类型转换。