我想Integer
在 JSP 中将值作为属性传递。但是当我尝试
int i = Integer.parseInt(request.getAttribute("count"));
返回一个错误。你能告诉我一种将Integer
数字作为属性存储在 JSP 中的方法吗?
我收到一个铸造错误parseInt()
,说不适合处理对象。
request.getAttribute
返回一个对象。你需要把它变成String
这样:
Integer.parseInt((String)request.getAttribute("count"));
要访问 JSP 中的请求,请使用request
小写字母 r。它还需要在 scriptlet 中,但不建议使用 scriptlet,因为您可以轻松使用 JSP EL。
<%
int i=Integer.parseInt((String)request.getAttribute("count"));
%>
如果您在页面上显示此值,则可以轻松使用表达式语言:
${count}
request.getAttribute() - 返回一个对象。
所以这个对象必须按如下方式进行类型转换
int i = (Integer.parseInt)(String.valueOf(request.getAttribute("count")));
我有我的工作示例..看看
字符串余额 = (String.valueOf(session.getAttribute("CostOfTotalTicket")));
int i = Integer.parseInt(Balance);
我在会话中存储的CostOfTotalTicket变量是字符串类型的
尝试这个
<%Object object = request.getAttribute("count");
int val =Integer.parseInt(object.toString());%>
它对我有用