JSTL标签库
2017-01-08 本文已影响13人
大批
JSTL
- apache对EL表达式的扩展
- jstl.jar包
- core 核心库
- fmt 格式化标签库
Core (c标签)
-
out && set
- <c:out value="xx"/>
- <c:out value="${xxx}"/> // ${xxx}
- <c:out value="${xxx}" default="xxx" escapeXml="true"/> // 默认true
- <c:set var="name" value="value"/>
- <c:set var="name" value="value" scope="session"/> // scope默认pageContext
-
remove
- <c:remove var="name"/> //所有域的变量
- <c:remove var="name" scope="session"/>
-
url (路径前面自动加上项目名)
- <c:url value="/index.jsp"/> //必须以斜杠开头
- 子标签 <c:param/> url后面添加参数
- <c:param name="name" value="value"/> // value会有url编码
-
if
- <c:if test="${not empty a}">
<c:out value="${a}"/>
</c:if>
- <c:if test="${not empty a}">
-
choose
- <c:choose>
<c:when test="boolean" ></c:when>
<c:when test="boolean"></c:when>
<c:otherwise></c:otherwise>
</c:choose>
- <c:choose>
-
forEach
- <c:forEach var="i" begin="1" end="10" step="2">
</c:forEach> - <c:forEach items="${list}" var="item">
${item}
</c:forEach>
- <c:forEach var="i" begin="1" end="10" step="2">
-
循环状态
-
<c:forEach items="${list}" item="ele" varStatus="vs">
${vs.index} ${vs.count} ${vs.first} ${vs.last}
</c:forEach>
fmt (格式化标签)
- <fmt:formatDate value="${date}" pattern="yyyy-MM-dd HH:mm:ss" />
- <fmt:formatNumber value="${number}" pattern="#.###" />
SimpleTag || SimpleTagSurport
- void doTag() //每次执行
- void setJspBody(JspFragment jspBody)
- void setJspContext(JspContext context)
- JspTag getParent() //设置标签体
- void setParent(JspTag parent)
eg:
package com.suse.yuxin.tags;
import java.io.IOException;
import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.JspTag;
import javax.servlet.jsp.tagext.SimpleTag;
public class TestSimpleTag implements SimpleTag {
@Override
public void doTag() throws JspException, IOException {
context.getOut().write("Hello tags");
}
@Override
public JspTag getParent() {
return parent;
}
@Override
public void setJspBody(JspFragment jspBody) {
this.fragment = jspBody;
}
private JspTag parent;
private JspContext context;
private JspFragment fragment;
@Override
public void setJspContext(JspContext pc) {
this.context = pc;
}
@Override
public void setParent(JspTag parent) {
this.parent = parent;
}
}
<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>hellotag</short-name>
<uri>http://www.suse.yuxin/tags/1.0</uri>
<tag>
<name>hellotag</name>
<tag-class>com.suse.yuxin.tags.TestSimpleTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
<%@ taglib prefix="mm" uri="/WEB-INF/tlds/hellotag.tld" %>
<mm:hellotag/>
标签体内容
- empty
- scriptless 只能是EL表达式 或者其他标签 // <tag>${el}</tag>
标签的属性
- 标签处理类至少要有一个set方法
- 在tld里面配置属性
<tag> <name>hellotag</name> <tag-class>com.suse.yuxin.tags.TestSimpleTag</tag-class> <body-content>empty</body-content> <attribute> <name>test</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <!-- 指定属性是否使用EL --> </attribute>
</tag>
- - -
> Nothing is certain in this life. The only thing i know for sure is that. I love you and my life. That is the only thing i know. **have a good day**
# :)