velocity自定义标签实现给静态资源添加版本号

使用方法:

 $staticFile.src("/css/main.css")
 $staticFile.src("/js/common/jquery-1.9.1.min.js")

其中 $staticFile是我自定义的标签,src是其中的一个方法,用来引入静态资源文件,有兴趣的同学可以试试效果。


1、java代码

package com.jd.oms.web.util;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jd.oms.web.ControllerContext;

/**
 * 
 * @author zhuzi
 *
 */
public class VersionlizeStaticFileTag {

	private static final long serialVersionUID = -5777586393588508658L;
	private static Logger logger = LoggerFactory.getLogger(VersionlizeStaticFileTag.class);
	private static Map<String, String> fileElementMap = new HashMap<String, String>();
	private static String jsElementHead = "<script type=\"text/javascript\" src=\"";
	private static String jsElementTail = "\"></script>\n";
	private static String cssElementHead = "<link type=\"text/css\" rel=\"stylesheet\" href=\"";
	private static String cssElementTail = "\"/>\n";
	public final static int EVAL_BODY_INCLUDE = 1;

	public String src(String filePath) {
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		if (StringUtils.isNotBlank(fileElementMap.get(filePath))) {
			return fileElementMap.get(filePath);
		}
		String contextPath = request.getContextPath();
		if (StringUtils.isNotBlank(fileElementMap.get(filePath))) {
			return fileElementMap.get(filePath);
		}
		File file = new File(request.getSession().getServletContext().getRealPath(filePath));
		if (!file.exists()) {
			logger.info("staticResource filepath: "+filePath+" does not exist");
			if (filePath.toLowerCase().endsWith("js")) {
				StringBuilder jsElement =new StringBuilder();
				jsElement.append(jsElementHead).append(contextPath).append(filePath).append(jsElementTail);
				return jsElement.toString();
			} else if (filePath.toLowerCase().endsWith("css")) {
				StringBuilder cssElement =new StringBuilder();
				cssElement.append(cssElementHead).append(contextPath).append(filePath).append(cssElementTail);
				return cssElement.toString();
			}
		}
		long lastModified = file.lastModified();
		if (filePath.toLowerCase().endsWith("js")) {
			StringBuilder jsElement =new StringBuilder();
			jsElement.append(jsElementHead).append(contextPath).append(filePath).append("?v=").append(lastModified).append(jsElementTail);
			fileElementMap.put(filePath, jsElement.toString());
			return jsElement.toString();
		} else if (filePath.toLowerCase().endsWith("css")) {
			StringBuilder cssElement =new StringBuilder();
			cssElement.append(cssElementHead).append(contextPath).append(filePath).append( "?v=").append(lastModified).append(cssElementTail);
			fileElementMap.put(filePath, cssElement.toString());
			return cssElement.toString();
		}
		logger.info("staticResource: "+filePath+" is uncorrect,it should be ended with css|js");
		return filePath;
	}

}



2、需要配置toolbox

<?xml version="1.0" encoding="UTF-8"?>
<!--
=============================================================
  静态资源加载,自定义标签,使用方法:
 $staticFile.src(arg),arg:js|css文件相对路径
=============================================================
-->

<toolbox>
      <tool>
        <key>staticFile</key>
        <scope>application</scope>
        <class>com.**.**.web.util.VersionlizeStaticFileTag</class>
    </tool>
    
</toolbox>

3、velocity中配置视图的属性,这里toolbox.xml放在非/WEB路径下会报nullpoint,找不到文件,暂时没研究出未啥
<bean id="velocityViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
         ......
		<property name="toolboxConfigLocation" value="/WEB-INF/vm/toolbox.xml" /><!--toolbox配置文件路径-->
	 ...... 
		 
</bean>


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页