在 Jsp中 如何将 int型 转换 Object型?

来源:百度知道 编辑:UC知道 时间:2024/06/30 05:18:31
本想将 int型的id 赋给session变量的。如下:
session.setAttribute("id",userbean.getId());//userbean.getId()得到的是一个整型的值。

提示如下错误:The method setAttribute(String, Object) in the type HttpSession is not applicable for the arguments (String, int)

换成 session.setAttribute("id",(Object)userbean.getId());
错误如下:Cannot cast from int to Object

如何解啊?

int 型不能转换成Object对象.你可以改成session.setAttribute("id",(Object)new Ingeter(userbean.getId()));
需要把int这种基本数据类型的包装成对象才能转换.还可以使用Integer.valueOf()方法转换.

你确定userbean.getId获得了值吗?int的超类应该就是object,所以给INT型应该是没有问题的,不需要类型转换.不行就.tostring()