前端是input标签写的
从前端获取的值,后端是int类型,不知道怎么用request. 获取int类型的值呢?
下面这种写法是错误,报错的
int score = Integer.valueOf(request.getParameter("score"));
鉴于你没有报错信息,我猜想报错信息应该是转换异常。
你拿取参数是没有错的,但是需要做一下验证,不要直接转换
下面是我写的:
String score=request.getParameter("score");
if(score==null || score==''){
return "score为空";
}
int s=new Integer(score);
报错信息呢?
先判断非空,再转int。确认取到值了
不是那写法是错的,是你那样写是错的,要么是空,要么是不是数字格式,比如多了个空格啥的;
最简单的方式像楼上 直接写方法参数上,
String score=request.getParameter("score");
System.out.println("score="+score)
首先你是post请求还是get请求,然后在看如何取值,一般先要判断是否非空,然后再转int,
get请求如下:
String score = request.getParameter("score");
if(StringUtils.isEmpty(score)){
return "score为空!";
}
int sc = Integer.parseInt(score);
post请求 json对象传值
@RequestMapping(value = "/getUserInfo", method = RequestMethod.POST)
public @ResponseBody ResultData<String> getUserInfo(HttpServletRequest request) {
ResultData<String> data = new ResultData<String>();
try {
String paramStrin = Paramter.GetParamter(request);
JSONObject obj = JSONObject.parseObject(paramStrin);
Integer score= obj.getInteger("score");
} catch (Exception e) {
e.printStackTrace();
}
return data;
}