struts2拦截器配置

wxz2832840 2011-03-18
自定义了一个struts2拦截器,然后配置在SSH2中,报错如下:
严重: Exception starting filter struts2.2.1
Unable to load configuration. - interceptor-ref - file:/D:/software/apache-tomcat-6.0.20-t/apache-tomcat-6.0.20/webapps/recommend/WEB-INF/classes/struts.xml:36:48
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:428)
at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:69)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:51)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:397)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3800)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4450)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:722)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:583)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: Unable to load configuration. - interceptor-ref - file:/D:/software/apache-tomcat-6.0.20-t/apache-tomcat-6.0.20/webapps/recommend/WEB-INF/classes/struts.xml:36:48
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:69)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:371)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:415)
... 20 more
Caused by: Unable to find interceptor class referenced by ref-name myInterceptor - interceptor-ref - file:/D:/software/apache-tomcat-6.0.20-t/apache-tomcat-6.0.20/webapps/recommend/WEB-INF/classes/struts.xml:36:48
at com.opensymphony.xwork2.config.providers.InterceptorBuilder.constructInterceptorReference(InterceptorBuilder.java:63)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.lookupInterceptorReference(XmlConfigurationProvider.java:998)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.buildInterceptorList(XmlConfigurationProvider.java:550)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addAction(XmlConfigurationProvider.java:381)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:479)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:275)
at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:111)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:204)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:66)
... 22 more
2011-3-18 11:24:13 org.apache.catalina.core.StandardContext start
严重: Error filterStart
2011-3-18 11:24:13 org.apache.catalina.core.StandardContext start
严重: Context [/recommend] startup failed due to previous errors
2011-3-18 11:24:13 org.apache.catalina.core.ApplicationContext log
信息: Closing Spring root WebApplicationContext
哪位大侠请给指点一下,到底是什么错误...
wxz2832840 2011-03-18
补充拦截器类和配置文件如下:
(1)拦截器类:

package com.test.recommend.interceptor;

import java.util.Map;
import com.test.recommend.domain.User;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class AuthenticationInterceptor extends AbstractInterceptor {

private static final long serialVersionUID = 1L;

@Override
public String intercept(ActionInvocation invocation) throws Exception {
Map<String,Object> sessionValues=invocation.getInvocationContext().getSession();
//从session获取user
User user=(User)sessionValues.get("user");
//如果user为空,跳转到登录页面
if(user==null){
return "login";
}
//否则,正常运行
return invocation.invoke();
}
}

(2)拦截器在spring中的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="authenticationInterceptor" class="com.test.recommend.interceptor.AuthenticationInterceptor"></bean>
</beans>

(3)拦截器在struts2配置文件struts.xml中配置:

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.objectFactory" value="spring" />
<include file="struts-default.xml"></include>
        <package name="struts2" extends="struts-default"> 
<interceptors>
<interceptor name="myInterceptor" class="com.test.recommend.interceptor.AuthenticationInterceptor">
</interceptor>
</interceptors>
<global-results>
<result name="login" type="redirect">/WEB-INF/jsp/login.jsp</result>
</global-results>
        </package>
</struts>

配置大体上就是这样,但是启动服务器时总是报上面的错误
wxz2832840 2011-03-18
问题已解决:出现该错误的原因是拦截器的声明和引用没有在同一个package下,所以使用拦截器的时候一定要注意拦截器的声明和引用在同一个package下。
Global site tag (gtag.js) - Google Analytics