一:是什么:
JSTL是apache对EL表达式的扩展,JSTL是标签语言!JSTL标签使用以来非常方便,它与JSP动作标签一样,只不过它不是JSP内置的标签,需要我们自己导包,以及指定标签库而已!
二:为什么?有什么作用?
jsp主要用于显示业务逻辑代码处理以后的数据结果,不可避免的使用循环,布尔逻辑,数据格式转换等语句,使用jstl可以简化代码,便于管理,并且jstl可自定义标签,更加便捷
三:怎么用?
1: 导包
2:在jsp页面头部导入标签库。
1 <!--核心标签--> 2 <%@ taglib prefix="c" ?3 ???????????uri="http://java.sun.com/jsp/jstl/core" %> 4 <!--格式化标签--> 5 <%@ taglib prefix="fmt" ?6 ???????????uri="http://java.sun.com/jsp/jstl/fmt" %> 7 <!--SQL标签--> 8 <%@ taglib prefix="sql" ?9 ???????????uri="http://java.sun.com/jsp/jstl/sql" %>10 <!--XML标签-->11 <%@ taglib prefix="x" 12 ???????????uri="http://java.sun.com/jsp/jstl/xml" %>
13<!--jstl函数-->
14<%@ taglib prefix="fn"
15 ?????????uri="http://java.sun.com/jsp/jstl/functions" %>
3:使用标签
1 <!--在pageContext中添加name为score value为${param.score }的数据--> 2 <c:set var="score" value="${param.score }"/> 3 <!--choose标签对应Java中的if/else if/else结构。when标签的test为true时,会执行这个when的内容。当所有when标签的test都为false时,会执行otherwise标签的内容。--> 4 <c:choose> 5 ????<c:when test="${score > 100 || score < 0}">错误的分数:${score }</c:when> 6 ????<c:when test="${score >= 90 }">A级</c:when> 7 ????<c:when test="${score >= 80 }">B级</c:when> 8 ????<c:when test="${score >= 70 }">C级</c:when> 9 ????<c:when test="${score >= 60 }">D级</c:when>10 ????<c:otherwise>E级</c:otherwise>11 </c:choose>
4:其他标签网上都有,很多,不在多说,应熟练掌握练习
四:灵活用---自定义标签
1: 新建标签处理类,继承自SimpleTagSupport类重写doTag()方法。
1 public class IfTag extends SimpleTagSupport { 2 ?????????//test为iftag标签的一个布尔类型的属性 3 ????private boolean test; 4 ????????//提供相关方法 5 ????public boolean isTest() { 6 ????????return test; 7 ????} 8 ????public void setTest(boolean test) { 9 ????????this.test = test;10 ????}11 ????@Override12 ????public void doTag() throws JspException, IOException {13 ????????if(test) {14 ????????????this.getJspBody().invoke(null);15 ????????}16 ????}17 }
2:在web项目的WEB-INF目录的lib目录下建立tld文件,这个tld文件为标签库的声明文件,并配置好相关信息。
1 <tag> ?2 <!--自定义的标签名字--> 3 ????????<name>if</name> ?4 <!--类名--> 5 ????????<tag-class>cn.xxx.IfTag</tag-class> ?6 ?7 ????????<body-content>scriptless</body-content> 8 ?9 ????????<attribute>10 <!--属性名-->11 ????????????<name>test</name>12 <!--是否必选-->13 ????????????<required>true</required>14 <!--是否支持EL表达式-->15 ????????????<rtexprvalue>true</rtexprvalue>16 ????????</attribute> 17 ????</tag>
3:引用自定义标签
1 <%2 ????pageContext.setAttribute("one", true);3 ????pageContext.setAttribute("two", false);4 %>5 <it:if test="${one }">xixi</it:if>6 <it:if test="${two }">haha</it:if>7 <it:if test="true">hehe</it:if>
11
复习整理1:jsp标准标签库jstl
原文地址:https://www.cnblogs.com/nullering/p/9310953.html