首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

java类型异常java.lang.Integer cannot be cast to java.lang.String

2012-11-10 
java类型错误java.lang.Integer cannot be cast to java.lang.Stringspring framwork DAO的基类(下面代码

java类型错误java.lang.Integer cannot be cast to java.lang.String
spring framwork DAO的基类(下面代码忽略注解,那个应该没错)

Java code
public class HibernateCommonImpDAO<T extends Serializable, ID extends Serializable>extends HibernateDaoSupport {    [color=#FF0000]public ID save(T transientObject) {        return (ID) super.getSession().save(transientObject);[/color]    }}

这个返回类型是ID 我在这里很没搞懂这是个什么类型,接下来用到save方法的时候怎么定义类型。。。。

Service类的saveUser方法
Java code
public class LoginServiceImp implements LoginService    public String saveUser(LoginResource loginResource){        [color=#FF0000]return (String)loginResourceDAO.save(loginResource);[/color]           }}

LoginResource 是model实体
这里的类型就出问题啦。。。。
Java code
@RequestMapping(value="/regeditSave.do")    public String regeditSave(HttpServletRequest request,Model model) throws Exception {        HttpSession session = request.getSession();        try        {            LoginResource user = new LoginResource();            [color=#FF0000]loginService.saveUser(user);[/color]            session.setAttribute("sessionUser", user);                    }        catch(Exception e)        {            e.printStackTrace();        }                return "welcome";//跳转到欢迎界面        }

页面结果:页面跳转了,但添加不进数据库,session为空。
异常:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at com.sofmit.sd.login.service.LoginServiceImp.saveUser(LoginServiceImp.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy7.saveUser(Unknown Source)
at com.sofmit.sd.login.web.LoginController.regeditSave(LoginController.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:421)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:136)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:326)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:313)


at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:511)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:852)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:584)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1508)
at java.lang.Thread.run(Thread.java:619)
错误地方是知道了,但是怎么解决请大家教教我~~

[解决办法]
是你这个部错了
(String)loginResourceDAO.save(loginResource)
loginResourceDAO.save(loginResource) 返回值应该是int 类型 转换成string 时候 出错 你好好看看吧
[解决办法]
integer转换成String类型失败。 在需要integer转换成String的时候,后面加""就行了

热点排行