简体   繁体   English

Spring MVC资源,上传后如何更新?

[英]Spring MVC resources, how to update once uploaded?

I'm having trouble with updating my spring mvc css resource, once uploaded to deploy directory it don't take any changes. 我在更新spring mvc css资源时遇到了麻烦,一旦上传到部署目录,就不会进行任何更改。 I tried to fix it using maven clean->compile->deploy, then run tomcat server. 我试图使用maven clean-> compile-> deploy修复它,然后运行tomcat服务器。 But changes don't appear deployed app still using old css...In deployed .war package new css appeared. 但是更改仍然不会显示仍使用旧CSS的已部署应用程序...在已部署的.war软件包中,出现了新的CSS。

servlet-context.xml servlet-context.xml

<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.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.sping.test.controller"/>

    <bean class=
                  "org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
                  value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <mvc:resources mapping="/resources/**" location="/resources/" />

    <mvc:annotation-driven />

</beans>

To solve this problem normally I use one variable with timestamp value in the css/js url, like this 为了解决这个问题,通常在css / js网址中使用一个带有时间戳值的变量,像这样

<link href="${pageContext.request.contextPath}/resources/css/main.css?${applicationScope['cacheHash']}" rel="stylesheet" type="text/css" />
<link href="${pageContext.request.contextPath}/resources/css/homeMenu.css?${applicationScope['cacheHash']}" rel="stylesheet" type="text/css" />
...

The problem is you need to declare this ${applicationScope['cacheHash']} in the scope application on the server, you can declare in any Listerner. 问题是您需要在服务器上的范围应用程序中声明此$ {applicationScope ['cacheHash']} ,您可以在任何Listerner中声明。

public class CacheListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
    sce.getServletContext().setAttribute("cacheHash", new Date().getTime());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM