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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值