好记性不如烂笔头,书下这些,方便以后查阅,节省时间。
1. 循环
<c:forEach var="page" items="${pages}" varStatus="status" begin="begin" end="end">
${page.name}
</c:forEach>
pages是一个List或数组
varStatus四种属性
index:${status.index}
count:${status.count}
first:${status.first}
last:${status.last}
2. 分支
(1) c:if
<c:if test="${empty key}">
<c:if test="${host.id == guest.id}">
<a href="">返回我的***»</a>
</c:if>
</c:if>
在URL中嵌套c:if
<a href="/home/allpages.do?a=join<c:if test='${!empty key}'>&key=${key}</c:if>">成为关注者</a>
(2) c:choose
<c:choose>
<c:when test="${!empty count && count > 0}">
</c:when>
<c:otherwise>
</c:otherwise>
</c:choose>
3. 设置变量
设置:<c:set var="navUrl" value="/home/mypages.do" />
使用:${navUrl}
开发步骤
1).开发函数处理类,处理类就是普通的类;每个函数对应类中的一个静态方法;
package mytag;
/**
* EL 表达式函数处理类
*/
public class ElTag {
public static String reverse(String name){
return new StringBuffer(name).reverse().toString();
}
public static int countChar(String text){
return text.trim().length();
}
}
2).建立TLD文件,定义表达式函数
<?xml version="1.0" encoding="GBK"?> <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"> <!-- 定义函数版本 --> <tlib-version>1.0</tlib-version> <!-- 定义函数名称 --> <short-name>el</short-name> <!-- 定义第一个函数 --> <function> <!-- 定义第一个函数:reverse --> <name>reverse</name> <!-- 定义函数处理类 --> <function-class>mytag.ElTag</function-class> <!-- 定义函数的对应方法 --> <function-signature> java.lang.String reverse(java.lang.String) </function-signature> </function> <function> <name>countChar</name> <function-class>mytag.ElTag</function-class> <function-signature> java.lang.Integer countChar(java.lang.String) </function-signature> </function> </taglib>
3).在WEB.XML文件中配置;(可省略)
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <jsp-config> <taglib> <!-- 配置标签的引用地址 JSP页面中引用时使用--> <taglib-uri>/eltag</taglib-uri> <!-- 配置标签的TLD文件地址 --> <taglib-location>/WEB-INF/ElTag.tld</taglib-location> </taglib> </jsp-config> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
4).在JSP页面内导入并且使用
<%@ taglib uri="/eltag" prefix="el" %>
<body>
${el:reverse("ad") }
</body>