Struts2:java.lang.NoSuchFieldException: resourceEntries at java.lang.Class.getDeclaredField(Class.java:1901)
今天在做Struts2的测试用例时候,程序能正常跳转,但是在Console却报了一个错误,如下:
1 java.lang.NoSuchFieldException: resourceEntries 2 at java.lang.Class.getDeclaredField(Class.java:1901) 3 at com.opensymphony.xwork2.util.LocalizedTextUtil.clearMap(LocalizedTextUtil.java:877) 4 at com.opensymphony.xwork2.util.LocalizedTextUtil.clearTomcatCache(LocalizedTextUtil.java:857) 5 at com.opensymphony.xwork2.util.LocalizedTextUtil.reloadBundles(LocalizedTextUtil.java:841) 6 at com.opensymphony.xwork2.util.LocalizedTextUtil.getMessage(LocalizedTextUtil.java:723) 7 at com.opensymphony.xwork2.util.LocalizedTextUtil.findMessage(LocalizedTextUtil.java:781) 8 at com.opensymphony.xwork2.util.LocalizedTextUtil.findText(LocalizedTextUtil.java:447) 9 at com.opensymphony.xwork2.util.LocalizedTextUtil.findText(LocalizedTextUtil.java:374) 10 at com.opensymphony.xwork2.TextProviderSupport.getText(TextProviderSupport.java:202) 11 at com.opensymphony.xwork2.TextProviderSupport.getText(TextProviderSupport.java:117) 12 at com.opensymphony.xwork2.ActionSupport.getText(ActionSupport.java:100) 13 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 14 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 15 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 16 at java.lang.reflect.Method.invoke(Method.java:601) 17 at ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:871) 18 at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1273) 19 at ognl.ObjectMethodAccessor.callMethod(ObjectMethodAccessor.java:68) 20 at com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethodWithDebugInfo(XWorkMethodAccessor.java:117) 21 at com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethod(XWorkMethodAccessor.java:101) 22 at ognl.OgnlRuntime.callMethod(OgnlRuntime.java:1349) 23 at ognl.OgnlRuntime.callMethod(OgnlRuntime.java:1325) 24 at com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor.callMethod(CompoundRootAccessor.java:232) 25 at ognl.OgnlRuntime.callMethod(OgnlRuntime.java:1349) 26 at ognl.ASTMethod.getValueBody(ASTMethod.java:90) 27 at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212) 28 at ognl.SimpleNode.getValue(SimpleNode.java:258) 29 at ognl.Ognl.getValue(Ognl.java:494) 30 at com.opensymphony.xwork2.ognl.OgnlUtil.getValue(OgnlUtil.java:217) 31 at com.opensymphony.xwork2.ognl.OgnlValueStack.getValue(OgnlValueStack.java:342) 32 at com.opensymphony.xwork2.ognl.OgnlValueStack.tryFindValue(OgnlValueStack.java:331) 33 at com.opensymphony.xwork2.ognl.OgnlValueStack.tryFindValueWhenExpressionIsNotNull(OgnlValueStack.java:307) 34 at com.opensymphony.xwork2.ognl.OgnlValueStack.findValue(OgnlValueStack.java:293) 35 at org.apache.struts2.components.Property.start(Property.java:162) 36 at org.apache.struts2.views.jsp.ComponentTagSupport.doStartTag(ComponentTagSupport.java:53) 37 at org.apache.jsp.welcome_jsp._jspx_meth_s_005fproperty_005f0(welcome_jsp.java:227) 38 at org.apache.jsp.welcome_jsp._jspService(welcome_jsp.java:129) 39 at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 40 at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 41 at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438) 42 at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396) 43 at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340) 44 at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 45 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) 46 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 47 at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 48 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) 49 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 50 at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:721) 51 at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:466) 52 at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:391) 53 at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:318) 54 at org.apache.struts2.dispatcher.ServletDispatcherResult.doExecute(ServletDispatcherResult.java:159) 55 at org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186) 56 at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:373) 57 at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:277) 58 at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176) 59 at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 60 at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 61 at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263) 62 at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68) 63 at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 64 at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 65 at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133) 66 at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 67 at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207) 68 at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 69 at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 70 at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207) 71 at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 72 at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 73 at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190) 74 at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 75 at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75) 76 at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 77 at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94) 78 at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 79 at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243) 80 at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 81 at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100) 82 at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 83 at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141) 84 at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 85 at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267) 86 at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 87 at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:142) 88 at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 89 at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:166) 90 at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) 91 at com.opensymphony.xwork2.DefaultActionInvocation.invok 92 e(DefaultActionInvocation.java:248) 93 at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176) 94 at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 95 at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164) 96 at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 97 at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190) 98 at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 99 at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187) 100 at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248) 101 at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52) 102 at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:485) 103 at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) 104 at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91) 105 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) 106 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 107 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) 108 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) 109 at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 110 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) 111 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) 112 at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617) 113 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) 114 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518) 115 at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091) 116 at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668) 117 at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2503) 118 at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2492) 119 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 120 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 121 at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 122 at java.lang.Thread.run(Thread.java:722)
刚开始,百思不得其解,因为在我的form中没有用到resourceEntries,于是根据代码查看struts2源码,在struts2源码中找到了resourceEntries。
1 private static void clearTomcatCache() 2 { 3 ClassLoader loader = getCurrentThreadContextClassLoader(); 4 5 Class cl = loader.getClass(); 6 try 7 { 8 if ("org.apache.catalina.loader.WebappClassLoader".equals(cl.getName())) { 9 clearMap(cl, loader, "resourceEntries"); 10 } 11 else if (LOG.isDebugEnabled()) 12 LOG.debug("class loader " + cl.getName() + " is not tomcat loader.", new String[0]); 13 } 14 catch (NoSuchFieldException nsfe) 15 { 16 if ("org.apache.catalina.loader.WebappClassLoaderBase".equals(cl.getSuperclass().getName())) { 17 if (LOG.isDebugEnabled()) 18 LOG.debug("Base class #0 doesn't contain '#1' field, trying with parent!", nsfe, new String[] { cl.getName(), "resourceEntries" }); 19 try 20 { 21 clearMap(cl.getSuperclass(), loader, "resourceEntries"); 22 } catch (Exception e) { 23 if (LOG.isWarnEnabled()) 24 LOG.warn("Couldn't clear tomcat cache using #0", e, new String[] { cl.getSuperclass().getName() }); 25 } 26 } 27 } 28 catch (Exception e) { 29 if (LOG.isWarnEnabled()) 30 LOG.warn("Couldn't clear tomcat cache", e, new String[] { cl.getName() }); 31 } 32 }
看到org.apache.catalina.loader.WebappClassLoaderBase好像是tomcat中的问题,于是查看网上资料,发现tomcat8.0的类加载器webappclsaaLoader中没有属性resourceEntries,于是我换了tomcat7.0的看了下,7.0的有属性resourceEntries。看来这个是tomcat版本的问题了。如果需要用tomcat8,这就必须修改struts2的这个bug,再重新编译,这也是开源的一个好处。