session转换类型的问题
int pageMax=3;
int pageIndex=1;
MesBiz mes=new MesBizImpl();
String str=(String)session.getAttribute("indexes");
(报错的语句:java.lang.Integer cannot be cast to java.lang.String)
if(str!=null){
pageIndex=Integer.parseInt(str);
}
刚进页面的时候正常运行str为null
当提交后再跳转回来 session.getAttribute("indexes"); 为2!(已经确定为2了)
但把它转换为String时错误!
下面是完整代码:
Main.jsp页面
<%
int pageMax=3;
int pageIndex=1;
MesBiz mes=new MesBizImpl();
String str=(String)session.getAttribute("indexes");
if(str!=null){
pageIndex=Integer.parseInt(str);
}
List<Message> list=mes.showMes(pageMax,pageIndex);
request.setAttribute("Messages",list);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
<h3></h3>
<c:set var="mesage" value="${requestScope.Messages}" scope="request"></c:set>
<c:forEach var="mes" items="${requestScope.mesage}" varStatus="status">
<div>
<div style>作者:${mes.author}</div>
<div>文章:${mes.mesText}</div>
</div>
</c:forEach>
<div>
<ul>
<a href="turnPage.jsp?turn=1">上一页</a>
<a href="turnPage.jsp?turn=<%=(pageIndex+1)%>">下一页</a>
</ul>
</div>
<hr/>
</body>
</html>
turnPage.jsp页面
<html>
<head>
<title>处理分页</title>
</head>
<body>
<%
request.setCharacterEncoding("GBK");
String index=request.getParameter("turn");
if(index!=null){
int pageIndex=Integer.valueOf((String)request.getParameter("turn"));
session.setAttribute("indexes",pageIndex);
response.sendRedirect("Main.jsp");
}else{
out.print("出错");
}
%>
</body>
</html>
------解决方案--------------------
session.getAttribute("indexes")你最好写为:session.getAttribute("indexes").toString().trim();这样试试