1

我想ro.isdc.wro.model.WroModel在使用 wro4j 的 Spring 应用程序中获取 wro4j。但我总是得到一个

ro.isdc.wro.WroRuntimeException:没有与当前请求周期关联的上下文!

当我尝试构建时出现WroManager异常wroManagerFactory.create()

有谁知道为什么会有这个异常以及由谁来修复它?WroModel或者有人知道在 Spring Controller中获取的更好方法吗?

(wro4j 本身工作没有任何问题)

这是我的代码(完整的例外可以在下面找到):

@Controller
public class HomeController {

    @Autowired
    private WroFilter wroFilter;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home() {

         WroManagerFactory managerFactory = this.wroFilter.getWroManagerFactory();
/*line 127*/WroManager manager = managerFactory.create();
         WroModel wroModel = manager.getModelFactory().create();
         ....
    }
}

网页.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<filter>
    <filter-name>WebResourceOptimizer</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <init-param>
        <param-name>targetBeanName</param-name>
        <param-value>wroFilter</param-value>
    </init-param>
    <init-param>
        <param-name>targetFilterLifecycle</param-name>
        <param-value>true</param-value>
    </init-param>    
</filter>

<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<filter-mapping>
    <filter-name>WebResourceOptimizer</filter-name>
    <url-pattern>/wro/*</url-pattern>
</filter-mapping>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

春天root-context.xml

...
<bean id="wroFilter" class="ro.isdc.wro.http.ConfigurableWroFilter">
   <property name="properties" ref="wroProperties" />
</bean>

<util:properties id="wroProperties" location="classpath:wro.properties">
    <prop key="debug">true</prop>
    <prop key="cacheUpdatePeriod">0</prop>
    <prop key="modelUpdatePeriod">0</prop>      
</util:properties>

wro.xml

<groups xmlns="http://www.isdc.ro/wro">
  <group name="g1">
    <css>/styles/part1.css</css>
    <css>/styles/part2.css</css>
  </group>
  <group name="g2">
    <css>/styles/part1.css</css>    
  </group>
</groups>

完整的例外是

ERROR: ro.isdc.wro.util.ProxyFactory - exception
ro.isdc.wro.WroRuntimeException: No context associated with CURRENT request cycle!
    at ro.isdc.wro.config.Context.validateContext(Context.java:121)
    at ro.isdc.wro.config.Context.get(Context.java:103)
    at ro.isdc.wro.model.group.processor.InjectorBuilder$16.create(InjectorBuilder.java:233)
    at ro.isdc.wro.model.group.processor.InjectorBuilder$16.create(InjectorBuilder.java:231)
    at ro.isdc.wro.util.ProxyFactory.getInterfacesSet(ProxyFactory.java:85)
    at ro.isdc.wro.util.ProxyFactory.create(ProxyFactory.java:73)
    at ro.isdc.wro.util.ProxyFactory.proxy(ProxyFactory.java:49)
    at ro.isdc.wro.model.group.processor.InjectorBuilder.createReadOnlyContextProxy(InjectorBuilder.java:231)
    at ro.isdc.wro.model.group.processor.InjectorBuilder.initMap(InjectorBuilder.java:92)
    at ro.isdc.wro.model.group.processor.InjectorBuilder.build(InjectorBuilder.java:248)
    at ro.isdc.wro.manager.WroManager$Builder.build(WroManager.java:432)
    at ro.isdc.wro.manager.factory.BaseWroManagerFactory$1.initialize(BaseWroManagerFactory.java:144)
    at ro.isdc.wro.manager.factory.BaseWroManagerFactory$1.initialize(BaseWroManagerFactory.java:73)
    at ro.isdc.wro.util.LazyInitializer.get(LazyInitializer.java:32)
    at ro.isdc.wro.manager.factory.BaseWroManagerFactory.create(BaseWroManagerFactory.java:156)
    at ro.isdc.wro.manager.factory.BaseWroManagerFactory.create(BaseWroManagerFactory.java:51)
    at ro.isdc.wro.manager.factory.DefaultWroManagerFactory.create(DefaultWroManagerFactory.java:73)
    at ro.isdc.wro.manager.factory.DefaultWroManagerFactory.create(DefaultWroManagerFactory.java:18)
--> at test.testmvc.HomeController.home(HomeController.java:127)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:746)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:687)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:851)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:278)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:300)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
4

1 回答 1

1

为了访问任何 wro4j 相关的详细信息,您必须使 Context 实例可用。这可以使用 WroContextFilter 轻松实现:

<filter>
  <filter-name>WroContextFilter</filter-name>
  <filter-class>ro.isdc.wro.http.WroContextFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>WroContextFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

此过滤器负责将 Context 对象设置为当前请求周期。

于 2013-05-28T22:07:13.293 回答