- 定义一个static的public的方法unescape(),如:
package com.rrtong.util; public class Common { /*实现js的unescapse解码*/ public static String unescape(String src) { StringBuffer tmp = new StringBuffer(); tmp.ensureCapacity(src.length()); int lastPos = 0, pos = 0; char ch; while (lastPos < src.length()) { pos = src.indexOf("%", lastPos); if (pos == lastPos) { if (src.charAt(pos + 1) == 'u') { ch = (char) Integer.parseInt(src .substring(pos + 2, pos + 6), 16); tmp.append(ch); lastPos = pos + 6; } else { ch = (char) Integer.parseInt(src .substring(pos + 1, pos + 3), 16); tmp.append(ch); lastPos = pos + 3; } } else { if (pos == -1) { tmp.append(src.substring(lastPos)); lastPos = src.length(); } else { tmp.append(src.substring(lastPos, pos)); lastPos = pos; } } } return tmp.toString(); } }
- 定义一个unescape.tld的文件,并将该文件放在WEB-INF/jstl/目录下(这个可以根据个人需要定义位置)。
<?xml version="1.0" encoding="UTF-8" ?> <taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0"> <description>unescape</description> <display-name>unescape</display-name> <tlib-version>1.0</tlib-version> <short-name>unescape</short-name> <uri>http://java.sun.com/jsp/jstl/rrtong</uri> <function> <name>unescape</name> <function-class>com.rrtong.util.Common</function-class> <function-signature>java.lang.String unescape(java.lang.String)</function-signature> </function> </taglib>
http://java.sun.com/jsp/jstl/functions,否则会覆盖掉原有定义的jstl方法。
- 在web.xml配置unescape.tld
- 在代码中引用unescape()方法
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/rrtong"%>
<div class="mesageMid">${fn:unescape(videoComment.content)}</div>
文章来源: jstl中添加自定义的函数