(13)Spring2.0与Hibernate3.0的错误解决
严重: action: null
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'sessionfactory' defined in ServletContext resource [/WEB-INF/beans-config.xml]: Initialization of bean failed; nested exception is java.lang.SecurityException:
class "org.apache.commons.collections.SequencedHashMap"'s signer information does not match signer information of other classes in the same package
java.lang.SecurityException: class "org.apache.commons.collections.SequencedHashMap"s signer information does not match signer information of other classes in the same package
at java.lang.ClassLoader.checkCerts(ClassLoader.java:775)
at java.lang.ClassLoader.preDefineClass(ClassLoader.java:487)
at java.lang.ClassLoader.defineClass(ClassLoader.java:614)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124) at
org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1817)
at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:872).........
(18)错误: java.lang.NullPointerException
StandardWrapperValve[action]: Servlet.service() for servlet action threw exception
javax.servlet.jsp.JspException: Cannot retrieve mapping for action /settlementTypeManage
或者: type Status report
message Servlet action is not available
description The requested resource (Servlet action is not available) is not available.
原因: 发现 dao 实例、 manage 实例等需要注入的东西没有被注入
解决:这个时候,你应该查看日志文件;默认是应用服务器的 log 文件,比如 Tomcat 就是 [Tomcat 安装目录 ]/logs ;你会发现提示你: 可能是:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sf' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is org.hibernate.HibernateException: could not configure from URL: file:src/hibernate.cfg.xml
org.hibernate.HibernateException: could not configure from URL: file:src/hibernate.cfg.xml
……………………….
Caused by: java.io.FileNotFoundException: src\hibernate.cfg.xml
可能是:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is org.hibernate.MappingException: Resource: com/mcc/coupon/model/UserRole.hbm.xml not found
org.hibernate.MappingException: Resource: com/mcc/coupon/model/UserRole.hbm.xml not found
然后你就知道原因是因为配置文件的解析出了错误,这个通过 Web 页面是看不出来的。
更多的是持久化映射文件出的错误;导致了没有被解析;当然你需要的功能就无法使用了。
(19) 错误:StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
java.lang.ClassNotFoundException: org.apache.struts.taglib.bean.CookieTei
界面错误具体描述:
org.apache.jasper.JasperException: Failed to load or instantiate TagExtraInfo class: org.apache.struts.taglib.bean.CookieTei
(23)问题:用Tomcat5.5.12,发现原来很好用的系统不能用了,反复测试发现页面中不能包含 taglib,否则会出现以下提示:
HTTP Status 500 -type Exception report
Message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: /index.jsp(1,1) Unable to read TLD "META-INF/tlds/struts-bean.tld" from JAR file "file:*****/WEB-INF/lib/struts.jar":
(25) HTTP Status 404 - Servlet action is not available
type Status report
message Servlet action is not available
description The requested resource (Servlet action is not available) is not available.
(26): javax.servlet.ServletException: Error creating bean with name '/login' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'UserDAO' while setting bean property 'userDAO'; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'UserDAO' is defined
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:523)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
com.siyue.struts.filter.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:167)
这主要是applicationContext.xml文件中没有声明UserDao这个Bean,加入以下就可以解决。
<bean name="UserDAO" class="com.siyue.hibernate.UserDAO" singleton="false">
</bean>
但是,这就出现了下面的问题:
javax.servlet.ServletException: Error creating bean with name '/login' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'UserDAO' while setting bean property 'userDAO'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UserDAO' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: sessionFactory or hibernateTemplate is required
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:523)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
com.siyue.struts.filter.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:167)
这是因为声明中没有注入SessionFactory所致,将上面声明的UserDao改成这样就解决了:
<bean name="UserDAO" class="com.siyue.hibernate.UserDAO" singleton="false">
<property name="sessionFactory" ref ="sessionFactory" />
</property>
</bean>
(27)org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save():
(29) 出现错误:
Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class 'com.microsoft.sqlserver.jdbc.SQLServerDriver' for connect URL '
jdbc:sqlserver://localhost;DatabaseName=exam
.............
Caused by: java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:264)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:773)
... 38 more
(31) org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'useraction' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'user_server' of bean class [com.bids.action.UserAction]: Bean property 'user_server' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'user_server' of bean class [com.bids.action.UserAction]: Bean property 'user_server' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:793)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:645)
.........
2009-1-21 12:09:18 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
原因是"user_server""user_pojo"没有get和set方法。
(32) 地址栏中:http://localhost:8088/sshtest/user.do?act=register
报错:HTTP Status 404 - Servlet action is not available
(34) 报错:java.lang.Long cannot be cast to java.lang.Integer
Long 无法转化成Integer类型. 这个异常 经常出现在hinbernate分页查询的时候.
例如:
注: super.pageQuery(hql,null,null,null);调用了一个父类的一个封装方法.查询时候使用
List list = this.getHibernateTemplate().executeFind(new HibernateCallback(){});
/**
* 查询所有用户记录总数
*/
public Integer getUsersCount() {
String hql = "select count(*) from Users";
List list = super.pageQuery(hql, null, null, null);
return (Integer) list.get(0);
}
原因:
这里在Hibernate2.0之前版本list.get(0)返回的是Integer类型.
但是在Hibernate3.0以后版本list.get(0)返回的是Long类型.
所以在这里不可以由Long型强转成Integer类型.
解决办法:
public Integer getUsersCount() {
String hql = "select count(*) from Users";
List list = super.pageQuery(hql, null, null, null);
Number num = (Number) list.get(0);
return num.intValue();
}
注:java.lang.Number是Integer,Long的父类.
(35) 报错 : Servlet Action is not available 因为Spring配置文件的位置放在WEB-INF下,要在WEB.XML文件中作为Servlet步署,而不能用插件方式连接
下面这种情况会引发这种情况:
在struts中的formbean中的reset方法,往往我们用来初始化界面的一些显示值.
如一个注册,可能在这里先查询数据库,找到所有的部门信息,然后绑定在界面的下拉框中.所以可能会在reset方法中调用业务层方法.(也有可能请求页面之前,先经过一个servlet或jsp,在servlet里面调用业务层方法)
如果你在formbean中是这样写会报上面的错误:
ApplicationContext ap = new FileSystemXmlApplicationContext(
new String[] {
"E:\\MyWorkPlace\\sshtest\\WebRoot\\WEB-INF\\applicationContext.xml",
"E:\\MyWorkPlace\\sshtest\\WebRoot\\WEB-INF\\operatorbeans.xml",
"E:\\MyWorkPlace\\sshtest\\WebRoot\\WEB-INF\\actionbeans.xml" });
IUserService us = (IUserService) ap.getBean("userservice");
然后用us 直接调用方法.
原因: 在这里ApplicationContext ap = new FileSystemXmlApplicationContext(
new String[] {});
启动的实际上是另外一个spring容器,而不是刚刚启动服务的时候创建的那个spring容器.
(40) 报错: org.apache.jasper.JasperException: $Proxy3 cannot be cast to
com.xj.service.impl.RoleService
java.lang.ClassCastException: $Proxy3 cannot be cast to com.xj.service.impl.RoleService
(43) org.springframework.dao.InvalidDataAccessResourceUsageException:
could not execute query; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
方案:sql语句有错误
(44) 严重: Exception sending context initialized event to listener instance of class com.sun.faces.config.ConfigureListener
javax.faces.FacesException: Can't parse configuration
file:jar:file:/E:/工具/apache-tomcat-6.0.9/apache-tomcat-6.0.9/webapps/jsf02/
WEB-INF/lib/jsf-impl.jar!/com/sun/faces/standard-html-renderkit.xml
方案:把tomcat的安装文件安装到英文目录下
(45) spring MVC+hibernate
Exception in thread "main" org.springframework.dao.InvalidDataAccessResourceUsageException: Could not execute JDBC batch update; nested exception is
org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
Caused by: org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [org.springframework.transaction.interceptor.TransactionProxyFactoryBean] for bean with name 'userService' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: problem with class file or dependent class; nested exception is java.lang.NoClassDefFoundError: org/springframework/aop/framework/AbstractSingletonProxyFactoryBean Caused by:
java.lang.NoClassDefFoundError: org/springframework/aop/framework/AbstractSingletonProxyFactoryBean
方案:添加 spring 2.0 aop libraries
(46)org.apache.jasper.JasperException: Unable to compile class for JSP:
或org.apache.jasper.JasperException: Unable to compile class for JSP
方案:jsp网页中的java代码有问题
或:jsp页面中的java代码括号不对称!
(47)错误:Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'CompanyController' defined in class path resource [com/company/test/company-config.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'methodNameResolver' of bean class [com.company.action.CompanyController]: Bean property 'methodNameResolver' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'methodNameResolver' of bean class [com.company.action.CompanyController]: Bean property 'methodNameResolver' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
控制器没继承MultiActionController
(48)提示:Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'turDiffService' defined in class path resource [com/expert/test/expert-config.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'turDifficultDao' of bean class [com.expert.operation.impl.TurDiffSerImpl]: Bean property 'turDifficultDao' is not writable or has an invalid setter method. Did you mean 'truDifficultDao'?
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'turDifficultDao' of bean class [com.expert.operation.impl.TurDiffSerImpl]: Bean property 'turDifficultDao' is not writable or has an invalid setter method. Did you mean 'truDifficultDao'?
(49)错误:Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'urlMapping' defined in class path resource [com/expert/test/expert-config.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named '' is defined
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named '' is defined
(50)org.springframework.orm.hibernate3.HibernateQueryException: Instruction is not mapped
[from Instruction]; nested exception is org.hibernate.hql.ast.QuerySyntaxException:
Instruction is not mapped [from Instruction]
Caused by: org.hibernate.hql.ast.QuerySyntaxException: Instruction is not mapped [from
Instruction]
dom4j.jar:
dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的。dom4j是一个非常非常优秀的Java XML API,具有性能优异、功能强大和极端易用使用的特点,同时它也是一个开放源代码的软件,可以在SourceForge上找到它。在IBM developerWorks上面可以找到一篇文章,对主流的Java XML API进行的性能、功能和易用性的评测,dom4j无论在那个方面都是非常出色的。我早在将近两年之前就开始使用dom4j,直到现在。如今你可以看到越来越多的Java软件都在使用dom4j来读写XML,特别值得一提的是连Sun的JAXM也在用dom4j。这是必须使用的jar包,Hibernate用它来读写配置文件。