您的位置:首页 > 其它

SSH整合容易出现的错误

2010-04-07 17:23 323 查看
1.地址栏中:http://localhost:8088/sshtest/user.do?act=register

报错:HTTP Status 404 - Servlet action is not available

原因是:struts-config.xml配置文件中的请求处理器类出错了(找不到类路径)
<controller processorClass="com.xj.requestprocessor.MyProcessor"></controller>
解决办法:
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor">
</controller>

2.报错 :javax.servlet.ServletException: Cannot find ActionMappings or ActionFormBeans collection
原因: 工程里面的struts包.有损坏或某些struts包没有存在.
解决办法:重新创建一个工程,并且导入struts,然后将导入的struts包copy到目前的工厂的
WEB-INf/lib目录,重新部署,重新启动服务,就ok了

3.报错: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的父类.

4.报错:java.lang.NoSuchMethodError org.objectweb.asm.ClassVisitor.visit
(IILjavalangString;LjavalangString;[LjavalangString;LjavalangString;)V.txt

原因: 由于某些Spring和Hibernate的版本问题,其中导入包的时候,某些jar包发生了冲突.

解决办法: 删除工程下面WEB-INF/lib/asm-2.2.3.jar,然后在MyEclipse中刷新工程,
再重新部署工程,然后重启 Tomcat.

5. 报错 : Servlet Action is not available

(1). 将struts-config.xml文件中的
<plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml" />
</plug-in> 去掉

(2).然后在web.xml文件中加上这段代码就可以了.

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

6.报错:'sessionFactory' or 'hibernateTemplate' is required

原因: 在dao操作类中需要注入HibernateTemplate来创建sessionFactory.
或者直接注入sessionFactory.

错误的写法:
<bean id="depsdao" class="com.xj.dao.impl.DepsDao"
lazy-init="true">
</bean>

<bean id="userdao" class="com.xj.dao.impl.UsersDao"
lazy-init="true">
</bean>

解决办法:

要将这2个对象中的某一个注入到dao中.
第一种方法:
<bean id="depsdao" class="com.xj.dao.impl.DepsDao"
lazy-init="true">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
第二种方法:
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<bean id="depsdao" class="com.xj.dao.impl.DepsDao"
lazy-init="true">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate" />
</property>
</bean>

7.报错: java.lang.IllegalStateException: No WebApplicationContext found:
no ContextLoaderListener registered?

原因: web.xml文件中的配置写错了
解决办法:在web.xml中加上
<context-param>
<param-name> contextConfigLocation </param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
</param-value>
</context-param>
<servlet>
<servlet-name> context </servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup> 1 </load-on-startup>
</servlet>

8.报错:No bean named 'sessionFactory' is defined
错误可能原因1:
在web.xml中加载applicationContext.xml文件的时候没有加载成功,看你的路径是否 正确,
这个配置错误,服务器启动的时候不会报错
错误可能原因2:
没有删除asm-2.2.3.jar文件.一定要直接从磁盘目录删除.然后刷新工程,重新部署 重启服务.

9.报错:sessionFactory或者hibernateTemplate找不到
有的时候,我们明明就在dao中注入了sessionFactory或hibernate,但是还是总是报错,说
sessionFactory或者hibernateTemplate找不到

下面这种情况会引发这种情况:
在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容器.

解决办法:
在reset方法中:
ServletContext context = servlet.getServletContext();
ApplicationContext app = (ApplicationContext) context.getAttribute
(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

IDeptService ds = (IDeptService) app.getBean("deptservice");
this.depList = ds.selectAllDeps();
注:servlet是struts框架中间的一个对象,保存了web容器的很多信息

那么之前是怎样设置进去的呢?
// context.setAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,启动的时候创建的那个spring器);

10.报错: 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

原因:很明显又是一个代理时候,强转错误:

ServletContext context = servlet.getServletContext();
ApplicationContext app = (ApplicationContext) context.getAttribute
 (WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
IDeptService ds = (DeptService) app.getBean("deptservice");
this.depList = ds.selectAllDeps();
就是这段示例代码中的 IDeptService ds = (DeptService) app.getBean ("deptservice");出错了
不能直接强转成接口的实现类,一定要转成接口.
IDeptService ds = (IDeptService) app.getBean("deptservice");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: