Struts 2无法加载拦截器文件

时间:2014-01-06 07:15:44

标签: java struts2 struts interceptor

我是struts2的新手,我在将自定义拦截器文件加载到我的演示Struts2应用程序时遇到问题。我已经测试并检查了所有的java,jsp和xml文件,一切似乎都没问题。在运行时我有以下错误,我不知道如何解决它。应用程序可以工作,但不会调用拦截器,也不会打印打印行语句。

WARNING: Unable to load config class com.ao.businessobjects.interceptor at interceptor -     file:/Users/faraichirawu/Documents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/AO/WEB-INF/classes/struts.xml:16:86 probably due to a missing jar, which might be fine if you never plan to use the LoginInterceptor interceptor
Jan 05, 2014 4:27:20 PM com.opensymphony.xwork2.config.providers.InterceptorBuilder error
SEVERE: Actual exception
Caught Exception while registering Interceptor class com.ao.businessobjects.interceptor - interceptor - file:/Users/faraichirawu/Documents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/AO/WEB-INF/classes/struts.xml:16:86
at com.opensymphony.xwork2.factory.DefaultInterceptorFactory.buildInterceptor(DefaultInterceptorFactory.java:64)
at com.opensymphony.xwork2.ObjectFactory.buildInterceptor(ObjectFactory.java:202)
at com.opensymphony.xwork2.config.providers.InterceptorBuilder.constructInterceptorReference(InterceptorBuilder.java:70)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.lookupInterceptorReference(XmlConfigurationProvider.java:1110)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.buildInterceptorList(XmlConfigurationProvider.java:626)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addAction(XmlConfigurationProvider.java:440)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:552)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:292)
at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:112)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:258)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:67)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:445)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:489)
at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:74)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:57)
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:281)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:262)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:107)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4775)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5452)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.ClassNotFoundException: com.ao.businessobjects.interceptor
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
at com.opensymphony.xwork2.util.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:152)
at com.opensymphony.xwork2.ObjectFactory.getClassInstance(ObjectFactory.java:128)
at com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:181)
at com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:171)
at com.opensymphony.xwork2.factory.DefaultInterceptorFactory.buildInterceptor(DefaultInterceptorFactory.java:42)
... 26 more

这是我的struts.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">  
<struts>

<!-- <constant name="struts.convention.result.path" value="/WEB-INF/" /> 
    <constant name="struts.convention.action.suffix" value="Controller" /> <constant 
    name="struts.convention.package.locators" value="default" /> <constant name="struts.convention.default.parent.package" 
    value="default" /> <constant name="struts.enable.DynamicMethodInvocation" 
    value="false" /> -->

<package name="default" extends="struts-default" namespace="/admin/accounts">
      <interceptors>
        <interceptor name="LoginInterceptor" class="com.ao.businessobjects.interceptor" />
        <interceptor-stack name="loginStack">
            <interceptor-ref name="LoginInterceptor" />
            <interceptor-ref name="defaultStack" />
        </interceptor-stack>
    </interceptors> 

    <global-results>
        <result name="login">/WEB-INF/login.jsp</result>
    </global-results>

    <action name="login">
        <result>/WEB-INF/login.jsp</result>
    </action>

    <action name="loginAction" class="com.ao.admin.administrator.accounts.LoginAction">
        <result name="success" type="chain">welcomeAction</result>
        <result name="input">/WEB-INF/login.jsp</result>
    </action>

    <action name="welcomeAction">
        <interceptor-ref name="loginStack" />
        <result name="success">/WEB-INF/welcome.jsp</result>
    </action>

    <action name="logoutAction" class="com.ao.admin.administrator.accounts.LogoutAction">
        <result name="success">/WEB-INF/login.jsp</result>
    </action>
</package>
</struts>   

已加载的jar lib文件为

commons-fileupload-1.3.jar
commons-io-2.2.jar
commons-lang-2.4.jar
commons-lang3-3.1.jar
commons-logging-1.1.3.jar
commons-logging-api-1.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.6.jar
struts2-config-browser-plugin-2.3.16.jar
struts2-core-2.3.16.jar
xwork-core-2.3.16.jar

这是自定义拦截器类

package com.ao.businessobjects.interceptor;


import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class LoginInterceptor implements Interceptor
{

private static final long serialVersionUID = 1L;

public String intercept(ActionInvocation invocation) throws Exception {

    String className = invocation.getAction().getClass().getName();
    long startTime = System.currentTimeMillis();
    System.out.println("Before calling action: " + className);

    String result = invocation.invoke();

    long endTime = System.currentTimeMillis();
    System.out.println("After calling action: " + className
            + " Time taken: " + (endTime - startTime) + " ms");

    return result;
}

public void destroy() {
    System.out.println("Destroying MyLoggingInterceptor...");
}
public void init() {
    System.out.println("Initializing MyLoggingInterceptor...");
}
}

1 个答案:

答案 0 :(得分:0)

class属性不完整

在你的struts.xml中替换

<interceptor name="LoginInterceptor" class="com.ao.businessobjects.interceptor" />

<interceptor name="LoginInterceptor" class="com.ao.businessobjects.interceptor.LoginInterceptor" />

“class”是拦截器的完整路径, “name”是将在struts.xml

中使用的逻辑名称