【问题标题】:Spring MVC resources (.js, .css, .png) being "aborted" before complete load in browserSpring MVC 资源(.js、.css、.png)在浏览器中完全加载之前被“中止”
【发布时间】:2013-04-07 17:26:21
【问题描述】:

我正在编写一个基于 Spring、Sitemesh、JPA 和 Shiro 的 Web 应用程序。这实际上是我第一次使用大多数这些框架,因为我已经使用 Struts/ibatis 很长时间了。

我遇到的问题很奇怪,我进行了几天的研究和反复试验,但一无所获。在资源加载中止的浏览器中,我在页面刷新时遇到间歇性问题。他们似乎有时会部分加载,而其他时候则完全丢失。

服务器设置为tomcat/apache。

我查看了服务器端,没有看到任何错误。

这是来自我的 web.xml 和我的 base-servlet.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">

<display-name>myapp</display-name>
<description>myapp</description>

<listener>
    <listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
</listener>

<filter>
    <filter-name>ShiroFilter</filter-name>
    <filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>ShiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher> 
    <dispatcher>FORWARD</dispatcher> 
    <dispatcher>INCLUDE</dispatcher> 
    <dispatcher>ERROR</dispatcher>
</filter-mapping>

<filter>
    <filter-name>sitemesh</filter-name>
    <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>sitemesh</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>


<!--  the name of this servlet maps to  [servlet-name]-servlet.xml that will also be found in WEB-INF -->   
<servlet>
    <servlet-name>base</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

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

基础

<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">


<!--  indicates the package(s) to be scanned to discover annotated controllers -->
<context:component-scan base-package="com.execupros.rox.webapp.controllers" />

<!-- the mvc resources tag does the magic to skip static  -->
<mvc:resources mapping="/resources/**" location="/resources/" />



<!--  maps the JSP locations -->
 <bean id="viewResolver"
       class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
       <property name="prefix">
          <value>/WEB-INF/html/</value>
       </property>
       <property name="suffix">
          <value>.jsp</value>
       </property>
 </bean>

 <!--  wires up commons upload -->
 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />


 <!--  define interceptors to be applied to all annotated controllers. -->
 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
 </bean>
 <bean id="annotationMapper" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="interceptors">
        <list>
            <!-- <ref bean="authInterceptor"/> -->
            <ref bean="userContextInterceptor" />
        </list>
    </property>
</bean>



<!--  <bean id="authInterceptor" class="com.execupros.rox.webapp.interceptors.AuthenticationInterceptor" /> -->
<bean id="userContextInterceptor" class="com.execupros.rox.webapp.interceptors.UserContextInterceptor" />

作为一个例子,我在默认装饰器上拉入这样的资源。

<script src="<c:url value="/resources/js/functions.js" />" type="text/javascript"></script>

这似乎只在您刷新页面时发生,并且它决定加载或不加载的内容对于 /resources 下的任何内容都是零星的。

【问题讨论】:

  • 可能与您的 shiro 过滤器有关,请仔细检查您的 url 映射和过滤器逻辑。

标签: java spring sitemesh


【解决方案1】:

我能找到的唯一答案是将资源移到应用程序之外,这是我们可以让它们 100% 正确加载的唯一方法,换句话说,我们获取资源并在其中创建了一个过滤器apache 来提取这些资源以通过 apache 提供服务,而不是从 tomcat 提供服务。

 RewriteCond $1 ^/(?!javascript|uploads|styles|images|test?).*$ [NC]

【讨论】:

    猜你喜欢
    • 2020-07-11
    • 2016-04-19
    • 1970-01-01
    • 2018-06-16
    • 2014-06-05
    • 2012-11-10
    • 2010-09-22
    • 1970-01-01
    • 2021-10-25
    相关资源
    最近更新 更多