requestsetAttribute和requestgetAttribute的问题

时间:2008-08-27 09:01:41   来源:论坛整理  作者:  编辑:chinaitzhe
请问,ACTION里request.setAttribute方法和JSP中的request.getAttribute方法之间只能传递字符串,而传递INT或其他类型都get不到是怎么回事,谢谢各位


注释:只能传递字符串举例
request.setAttribute( "name ", "jack ")这样可以通过request.getAttribute方法在JSP中得到
而request.setAttribute( "age ",18)这样传递int型或其他类型的参数在JSP中用request.getAttribute方法就得不到了
报的错误为:
ServletException during processing of content: ( java.lang.Integer) [60712] Caused by: Caused by: java.lang.ClassCastException java.lang.Integer
java.lang.ClassCastException: java.lang.Integer
java.lang.ClassCastException: java.lang.Integer
网友回复:可能是你的JDK版本不够高,换成5.0的应该没问题。

你可以这样试试。

request.setAttribute( "age ",18)改成request.setAttribute( "age ",new Integer(18))
网友回复:万分感谢,如果试成功了就给分:)
网友回复:似乎不是这个问题,改成new Integer(18)不行
JDK的话我再问问看,是什么版本的
网友回复:request.setAttribute( "age ",new Integer(18));

int i=((Integer)request.getAttribute( "age ")).intValue();

网友回复:JDK我是1.5.0_04的,应该不低了吧
还是谢谢你,继续期待
网友回复:setAttribute()函數第二個參數是個object,int是基本類型當然不可以。
网友回复:request.setAttribute( "age ",new Integer(18));

int i=((Integer)request.getAttribute( "age ")).intValue();
网友回复:Augustans你的方法也不行
我同事他们都是直接可以传其他类型的,包括Iterator
我不知道问题出在哪
网友回复:类型转换的问题,转一下就可以
网友回复:QWERT520,能说具体点吗?
类型我按我同事的方法转了
他可以传,我不可以传
网友回复:数据类型不符合,你就转换成你需要的类型!!!
网友回复:问题很邪门,希望大家帮忙解决,再次谢谢各位
网友回复:暈我代碼裏就這麽寫的阿,實在不行你就搞成String 阿,然後轉成int
网友回复:这个我也想过,是能解决问题
但我想知道问题出在哪
而且都用STRING的话也会导致不方便:)
网友回复:request.setAttribute( "age ",18);

int i=((Integer)request.getAttribute( "age ")).intValue();

网友回复:request.setAttribute( "age ",new Integer(18));
這麽寫肯定沒問題
网友回复:setAttribute和getAttribute只能传递对象,不能传递基本类型,只能把基本类型转换成封装类在进行传递。
网友回复:各位,谢谢了,本人自己失误,外加其他一些因素导致这些问题:)
给分!~~
网友回复:request.setAttribute(age,new Integer(18));
int i=(Integer)(request.getAttribute("age")).intValue();

request.setAttribute(Strig s,Object o)
request.getAttribute(String s)
两者只能传递对象,不能传递基本数据类型,所以需要转化成对应的封装类才能使用。
基本数据类型 封装类型
byte Byte
char Character
int Integer
short Short
float Float
double Double
long Long
boolean Boolean
关键字:问题,

相关文章

文章评论

共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面