velocity 获取文件服务器文件偶尔需要刷新才能加载出来的问题

来源:互联网 发布:mac硬盘恢复 编辑:程序博客网 时间:2024/05/20 19:15

情况如下:使用vm的tool去后台读取配置文件时,有时不生效,需要刷新一下才能加载出来,目前找到下面的解决方式,但是还不确定原因具体是因为什么导致,后续再补充。

代码如下:

springMvc.xml:

<bean id="viewResolver" class="com.yupen.util.vm.YPVelocityViewResolver">    <property name="cache" value="true"/>    <property name="prefix" value="/"/>    <property name="suffix" value=".vm"/>    <property name="contentType" value="text/html;charset=utf-8" />    <property name="layoutUrl" value="layout/default.vm"/>    <property name="allowSessionOverride" value="true"/>    <property name="allowRequestOverride" value="true"/>    <property name="exposeSessionAttributes" value="true"/>    <property name="requestContextAttribute" value="rc"/>    <property name="exposeRequestAttributes" value="true"/>    <property name="viewClass" value="com.yupen.util.vm.YPVelocityView"></property>    <property name="toolboxConfigResource" value="classpath:velocity_toolbox.xml"></property></bean>
YPVelocityViewResolver:

package com.yupen.util.vm;import org.springframework.core.io.Resource;import org.springframework.web.servlet.view.AbstractUrlBasedView;import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver;public class YPVelocityViewResolver extends VelocityLayoutViewResolver {   private Resource toolboxConfigResource;   @Override   protected AbstractUrlBasedView buildView(String viewName) throws Exception {      YPVelocityView view = (YPVelocityView) super.buildView(viewName);      if (this.toolboxConfigResource != null) {         view.setToolboxConfigResource(this.toolboxConfigResource);      }      return view;   }   public Resource getToolboxConfigResource() {      return toolboxConfigResource;   }   public void setToolboxConfigResource(Resource toolboxConfigResource) {      this.toolboxConfigResource = toolboxConfigResource;   }}
YPVelocityView:

package com.yupen.util.vm;import org.apache.velocity.context.Context;import org.apache.velocity.tools.Scope;import org.apache.velocity.tools.ToolboxFactory;import org.apache.velocity.tools.config.XmlFactoryConfiguration;import org.apache.velocity.tools.view.ViewToolContext;import org.springframework.core.io.Resource;import org.springframework.web.context.support.ServletContextResource;import org.springframework.web.servlet.view.velocity.VelocityLayoutView;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.net.URL;import java.util.Map;public class YPVelocityView extends VelocityLayoutView {   private Resource toolboxConfigResource;   private static ToolboxFactory factory;   @Override   protected Context createVelocityContext(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {      //    return super.createVelocityContext(model, request, response);      ViewToolContext context = new ViewToolContext(getVelocityEngine(), request, response, getServletContext());      context.putAll(model);      if (null != getToolboxConfigLocation() || null != getToolboxConfigResource()) {         synchronized(YPVelocityView.class){            if (factory==null){               XmlFactoryConfiguration cfg = new XmlFactoryConfiguration();               URL url;               if (null != getToolboxConfigLocation()) {                  url = new ServletContextResource(getServletContext(), getToolboxConfigLocation()).getURL();                  cfg.read(url);               } else if (null != getToolboxConfigResource()) {                  url = getToolboxConfigResource().getURL();                  cfg.read(url);               }               factory = cfg.createFactory();            }         }         if (factory!=null){            context.addToolbox(factory.createToolbox(Scope.APPLICATION));            context.addToolbox(factory.createToolbox(Scope.REQUEST));            context.addToolbox(factory.createToolbox(Scope.SESSION));         }      }      return context;   }   public void setToolboxConfigResource(Resource toolboxConfigResource) {      this.toolboxConfigResource = toolboxConfigResource;   }   private Resource getToolboxConfigResource() {      return toolboxConfigResource;   }}
















0 0