关于request.getAttribute()获取值的强转问题

首先,用request.getAttribute()获取的值是Object类型,所以可以强转为String类型,但是不能直接强转为int类型,否则会报错,所以在无数次的百度后,发现有一下两种方式可以参考:

1.Integer.parseInt(String.valueOf(request.getAttribute("attrname") != null ? request.getAttribute("attrname") : "0").trim())

2.

String  attrValue = (String)request.getAttribute( "attrname" );
  int  result =  0  ;
  if (attrValue !=  null  && ! "" .equals(attrValue)){
     result =  new  Integer(attrValue);
     //或者result = Integer.parseInt(attrValue);
  }

你可能感兴趣的:(java基础)