SpringMVC为什么在修改一个静态页面必须重新启动服务器才可以?
本帖最后由 notenlife 于 2012-11-30 23:25:08 编辑 最近自己学习搭建开发框架的时候遇到了一个困惑,因为在修改静态页面的内容的时候,必须重新启动tomcat才行,起初认为是项目的问题,结果查了很久发现,如果在web.xml中取消
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
这个拦截器,直接访问静态页面的时候,修改页面内容后不需要重新启动服务器,刷新修改过的页面就可以了。但是,如果添加了springMVC访问一个页面的话,对静态页面的内容进行更改后,刷新浏览器根本没有变化。但是tomcat部署的目录的静态文件已经改过了,只是刷新没用,换个浏览器访问这个页面还是修改前的页面。这是怎么回事呢?能否给个思路?
webapp\common\meta.html
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<#assign ctx=(rc.getContextPath())!''>
<!--EXTJS样式文件-->
<link rel="stylesheet" type="text/css" href="${ctx}/resources/ext/resources/css/ext-all.css"/>
<!--EXTJS文件-->
<script type="text/javascript" src="${ctx}/resources/ext/bootstrap.js"></script>
<!--EXTJS本地化文件,会将EXTJS中的提示信息等自动转换为简体中文-->
<script type="text/javascript" src="${ctx}/resources/ext/locale/ext-lang-zh_CN.js"></script>
webapp\index.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<#include "common/meta.html"/>
<script type="text/javascript" src="${ctx}/resources/app.js"></script>
<title>首页</title>
<script type="text/javascript">
</script>
<meta http-equiv="pragma" content="no-cache" />
</head>
<body>
<button id='myButton'>点击</button>
测试neng
</body>
</html>
[最优解释]
试下我的:
<!-- 配置 FreeMarkerConfigurer-->
<bean id="freemarkerManagerWeb" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer" p:templateLoaderPath="/WEB-INF/ftl/" p:defaultEncoding="UTF-8">
<property name="freemarkerSettings">
<props>
<prop key="classic_compatible">true</prop>
<prop key="number_format">#.##</prop>
<!-- <prop key="template_update_delay">3600</prop>隔3600秒检查模板是否更新 -->
<!-- <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop> -->
<!-- <prop key="template_exception_handler">ignore</prop> -->
</props>
</property>
</bean>
[其他解释]
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:service-context.xml</param-value>
</context-param>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
[其他解释]
servlet-context.xml
<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<description>Spring-web MVC配置</description>
<mvc:resources mapping="/resources/**" location="/resources/"/>
<!-- 对所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
<context:component-scan base-package="com.firefly.tire">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Service" />
</context:component-scan>
<!-- 当请求应用程序首页时,将执行/hello请求,还可以设定成redirect -->
<mvc:view-controller path="/" view-name="forward:toIndexPage" />
<!-- 这两个类用来启动基于Spring MVC的注解功能,将控制器与方法映射加入到容器中 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="cache" value="true" />
<!-- property name="prefix" value="/pages" /> -->
<!-- 模板后缀,指定html页面为模板 -->
<property name="suffix" value=".jsp" />
<!-- 使用这个模板类来解析视图 -->
<property name="viewClass"
value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
<property name="exposeSpringMacroHelpers" value="true" />
<!-- 允许访问请求属性,默认为false -->
<property name="exposeRequestAttributes" value="true" />
<!-- 允许访问会话属性,默认为false -->
<property name="exposeSessionAttributes" value="true" />
<!-- 页面上下文,类似于request.contextPath -->
<property name="requestContextAttribute" value="rc" />
<!-- 模板输出内容编码,此处应与defaultEncoding保持一致 -->
<property name="contentType" value="text/html;charset=UTF-8" />
</bean>
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<!-- 模板文件所在目录 -->
<property name="templateLoaderPath" value="/pages/" />
<!-- 指定FreeMarker模板文件的编码格式 -->
<property name="defaultEncoding" value="UTF-8" />
<!-- FreeMarker属性配置 -->
<property name="freemarkerSettings">
<props>
<!-- 每隔5小时检查模板是否更新,单位为秒 如果不经常更新模板可将更新的延迟时间设定长一点 -->
<prop key="template_update_delay">18000</prop>
<!-- 指定地区语言环境,我们的语言是中文 -->
<prop key="locale">zh_CN</prop>
</props>
</property>
</bean>
</beans>
[其他解释]
RootController.java
package com.firefly.tire;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* 欢迎页面
* @author
*/
@Controller
public class RootController {
@RequestMapping(value="/toIndexPage")
public String toIndexPage(){
return "index";
}
}
[其他解释]
其实你这些代码没必要发的。
在我的理解,spring是一个容器,既然是容器就需要加载文件。
你页面做了某些修改,容易中加载的那些并没有被更新吧?
只是我的理解,说错了请见谅。
[其他解释]
是有些道理,但是,如果我在开发的时候调试页面的js引用,或者做一些js变动,测试的时候还需要重启启动web容器才可以的话,那么开发很不方便呢?我是考虑到这个问题,不知道如何解决?
[其他解释]
哎....自己顶一下,朋友们,给出出主义吧
[其他解释]
这个有问题吧:
<!-- 每隔5小时检查模板是否更新,单位为秒 如果不经常更新模板可将更新的延迟时间设定长一点 -->
<prop key="template_update_delay">18000</prop>
[其他解释]
注意缓存...
[其他解释]
谢了~!厉害,就是把那个时间去掉了~,谢谢~,部署的时候添加上模版更新时间
这个问题困扰我好久了,谢谢~
结贴