spring/hibernate/struts2常见异常总结

  • Spring

①ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException

缺少aspectjweaver.jar,该jar包常用于spring aop中

 

②java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderL 

http://blessht.iteye.com/blog/1104450

 

③java.lang.NoSuchMethodException: $Proxy7.方法名()

spring aop中常见,aop的实现是代理,java领域代理有两种:jdk自带的针对接口的代码和cglib针对类的代码。spring aop默认使用jdk接口代理,某些时候访问的bean没有接口故出现以上问题,解决方法:在aop配置中修改代理类型:

 

<aop:config proxy-target-class="true">

 

④ClassNotFoundException javax/mail/MessagingException

spring配置javamailsenderimpl时出错,原因是缺少mail.jar这个包,将该jar包放入项目lib中即可

 

⑤:cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'aop:config'.这个是spring配置未引入aop命名规则引起的。下面这段是相对较全的命名规则。

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
   http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
   default-autowire="byName">

<beans>

 

⑥启动项目正常,访问Controller时报错

java.lang.NullPointerException

at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1003)

at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:792)

at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)

呵呵,这个问题在我搭建新环境时困扰我两小时!起初以为是exceptionResolver写错了,但注释掉也无济于事,然后各种注释不需要的东西还是报错。最后想到自己重新实现了org.springframework.web.servlet.DispatcherServlet,要不还原一下,使用spring默认的,结果一下就没错了。

然后我认真看自己的代码,发现问题了,看下面代码,initStrategies非常重要,开发人员可以在系统其中过程中添加各种初始化操作,但是千万别忘了调用super.initStrategies(context);这个方法是spring mvc自己做初始化必须使用的,我是因为把这句去掉了,添加了其它代码,所以导致莫名其妙的错误。

public class YdDispatcherServlet extends DispatcherServlet {
	private static final long serialVersionUID = -8834249973970757619L;
	
	/**
	 * 伴随servlet的init方法调用,执行数据的初始化操作
	 */
	@Override	
	protected void initStrategies(ApplicationContext context) {
		//super.initStrategies(context); 这句代码千万别注释
	}
}

 

 

 

  • Hibernate

①Unable to locate appropriate constructor on class

指定类找不到对应的构造方法,常出现于hql,确认hql中每个类型与Hibernate构造方法对应,并且构造方法的每个参数类型与hbm.xml文件对应

 

②org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ON near line 1

异常发生在HQL上面:select a.name,b.age from TableA a left join TableB b ON a.id=b.id。原来Hibernate不支持ON这种表达式,如果要建立两表关系就将ON替代为WHERE即可:

select a.name,b.age from TableA a left join TableB b where a.id=b.id

 

③java.lang.NullPointerException at org.hibernate.dialect.Dialect$3.getReturnType(Dialect.java:125)

异常发生在HQL上:select a.name,sum(b.age) from TableA a left join TableB b where a.id=b.id,异常发生原因跟下面第④个是一样的。

 

④org.hibernate.hql.ast.QuerySyntaxException: Path expected for join! 

异常发生在HQL上:select a.name,count(b.age) from TableA a left join TableB b where a.id=b.id。③和④的异常是同一原因:就是left join!原来在Hibernate如果两张表没有设置one-to-many这样的映射关系的话,就不能使用join。第③个是因为sum和join同时出现造成的错误,第④个是因为有left join造成的。修改方法是将left join用逗号","取代:select a.name,sum(b.age) from TableA a,TableB b where a.id=b.id

 

⑤Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1。我的代码如下,hibernate有一个好特性是持久化的时候通过set方法可以自动update数据库,但是运行这段代码时还是报上面的错。

User user = session.get(User.class,userId);
user.setName("xxx");//更新名称

 最后找原因,发现是User表在数据库中是自增的,而其实这条数据是通过UUID保存的主键。最后我到数据库将主键自增去掉就不报错了,很是奇怪!

 

⑥HibernateException: No Session found for current thread

通过sesstionFactory.currentSession获取不到Session对象,不同环境不同场景有不同原因。我是针对Spring3+Hibernate4整合时遇到的问题。

考虑以下两种解决方案:

方案一:在spring配置sessionFactory Bean中,加入对hibernate.current_session_context_class属性的注入,如下代码:

<property name="hibernateProperties">
  <props>
    ......
    <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
    ......		
  </props>
</property>

 

方案二:申明式事务配置的切面表达式不正确

如下表达式,我期望的事务范围是com.demo.任意层级.manager.下级类.任意方法(任意参数),但这里的**与*是一个意思,所以他真是的事务范围是com.demo.子级包.manager.下级类.任意方法(任意参数)。

<!-- 错误配置 -->
<aop:config >
    <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.demo.**.manager.*.*(..)))" /> 
</aop:config>

 解决方法是将**修改为..,两点才表示任意层级,如下代码是正确的。

<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.demo..manager.*.*(..)))" /> 

 

⑦ java.lang.IllegalArgumentException: node to traverse cannot be null!

执行HQL时报这种错误,表示HQL语法有问题,比如selec少一个t或者from写成form了。

 

⑧java.sql.SQLException: ORA-01722: 无效数字

问题出现在Oracle数据库环境下,看似很简单的字段类型都出错了:

 

select * from org_member,org_dept where deptPath LIKE memPath+'%';
原因是出在+号上:Oracle认为+号是数字操作,所以会将memPath varchar类型转换为number,这时就报错了。Oracle字符串拼接需要用||号。 改正后的SQL应该是这样的: 
select * from org_member where deptPath LIKE memPath || '%';

 

 

JSP

 

①Page directive: illegal to have multiple occurrences of contentType with different values (old: text/html; charset=utf-8, new: text/html; charset=UTF-8)

当前JSP有一个<%@ page>标签,并且引入了别的JSP

<%@ page contentType="text/html; charset=utf-8" isELIgnored="false"%>
<%@ include file="/WEB-INF/jsp/common/common.jsp"%>

 而别的JSP也有<%@ page>标签

<%@ page contentType="text/html; charset=UTF-8" isELIgnored="false"%>

 引起报错的原因是因为两个JSP的charset值不一样,一个是大写UTF-8一个是小写utf-8。改成一样即可。

Tomcat

①Setting property 'source' to 'org.eclipse.jst.jee.server:firstProject' did not find a matching property.

遇到的问题是在Eclipse下导入的firstProject这个dynamic web project无法被启动。

解决方法详见:http://blog.csdn.net/z69183787/article/details/19911935

作者说得很多,简单说,就是把Eclipse中配置的server删掉,重新创建一个就没问题了

猜你喜欢

转载自blessht.iteye.com/blog/1207098