有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaSpringMVC资源,上传后如何更新?

我在更新我的spring mvc css资源时遇到了问题,一旦上传到部署目录,它就不会进行任何更改。我尝试使用maven clean->;编译->;部署,然后运行tomcat服务器。但这些变化似乎并没有出现在仍然使用旧css的已部署应用程序中。。。在部署中。新的css出现了

servlet上下文。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>

共 (1) 个答案

  1. # 1 楼答案

    为了解决这个问题,我通常在css/js url中使用一个带有时间戳值的变量,如下所示

    <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" />
    ...
    

    问题是您需要在服务器上的作用域应用程序中声明这个${applicationScope['cacheHash']},您可以在任何列表器中声明

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