velocity自定义标签实现给静态资源添加版本号
2014-05-09 16:55
579 查看
使用方法:
$staticFile.src("/css/main.css")
$staticFile.src("/js/common/jquery-1.9.1.min.js")
其中 $staticFile是我自定义的标签,src是其中的一个方法,用来引入静态资源文件,有兴趣的同学可以试试效果。
1、java代码
2、需要配置toolbox
3、velocity中配置视图的属性,这里toolbox.xml放在非/WEB路径下会报nullpoint,找不到文件,暂时没研究出未啥
$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>
相关文章推荐
- CBitmap与HBITMAP的转换
- eclipse RCP配置问题
- ORA-00600 [4194]的解决
- 敏捷开发
- Nuane.SftpLite.dll 使用其可以反编译出sftp server
- 母亲节,写给我的妈妈
- 关于Class.getResource和ClassLoader.getResource的路径问题
- NSURLSession
- 转 mysql 中sql 语句查询今天、昨天、7天、近30天、本月、上一月 数据
- window7 远程连接 拒绝访问
- 数据结构课程设计---用Dijkstra算法实现一个简易的最佳方案选择
- Android中的Activity四种启动模式(3)
- C++习题 复数类--重载运算符+,-,*,/
- 统一设计,iOS6也玩扁平化
- 获取已安装或未安装的apk签名
- android虚拟机启动失败
- 搭建自己的Git服务器
- WPF草稿
- C++习题 复数类--重载运算符+,-,*,/
- php通过注释自动生成文档插件