javaweb學習筆記(十四):JSP(4)
阿新 • • 發佈:2018-11-08
目錄
製作高仿的JSTL標籤庫之核心標籤庫
通過自定義標籤,製作高仿的JSTL標籤庫之核心標籤庫。包括:
if標籤:單條件判斷。
choose標籤:多條件判斷。
forEach標籤:迴圈輸出。
《1》xiaohua.tld檔案:
<?xml version="1.0" encoding="UTF-8" ?> <taglib 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-jsptaglibrary_2_1.xsd" version="2.1"> <!-- 標籤庫的版本 --> <tlib-version>1.1</tlib-version> <!-- 標籤庫字首 --> <short-name>xiaohua</short-name> <!-- tld檔案的唯一標記 --> <uri>https://blog.csdn.net/qq_34774655</uri> <!-- 一個標籤的宣告 --> <tag> <!-- 標籤名稱 --> <name>showIp</name> <!-- 標籤處理器類的全名 --> <tag-class>tag.ShowIpTag</tag-class> <!-- 輸出標籤體內容格式 --> <body-content>scriptless</body-content> </tag> <tag> <name>demoTag</name> <tag-class>tag.DemoTag</tag-class> <body-content>scriptless</body-content> <!-- 屬性宣告 --> <attribute> <!-- 屬性名稱 --> <name>num</name> <!-- 是否必填 --> <required>true</required> <!-- 是否支援EL表示式 --> <rtexprvalue>false</rtexprvalue> </attribute> </tag> <tag> <name>if</name> <tag-class>tag.IfTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>test</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>choose</name> <tag-class>tag.ChooseTag</tag-class> <body-content>scriptless</body-content> </tag> <tag> <name>when</name> <tag-class>tag.WhenTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>test</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>otherwise</name> <tag-class>tag.OtherwiseTag</tag-class> <body-content>scriptless</body-content> </tag> <tag> <name>forEach</name> <tag-class>tag.ForEachTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>items</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>var</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
《2》依附的各個類:
IfTag.java
package tag; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.SimpleTagSupport; public class IfTag extends SimpleTagSupport { private boolean test; public void setTest(boolean test) { this.test=test; } @Override public void doTag() throws JspException, IOException { if(test) { this.getJspBody().invoke(null); } } }
ChooseTag.java
package tag; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.SimpleTagSupport; public class ChooseTag extends SimpleTagSupport{ //不是屬性,而是臨時變數 private boolean flag; public boolean isFlag() { return flag; } public void setFlag(boolean flag) { this.flag = flag; } @Override public void doTag() throws JspException, IOException { this.getJspBody().invoke(null); } }
WhenTag.java
package tag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class WhenTag extends SimpleTagSupport{
private boolean test;
public boolean isTest() {
return test;
}
public void setTest(boolean test) {
this.test = test;
}
@Override
public void doTag() throws JspException, IOException {
if(test) {
this.getJspBody().invoke(null);
}
//獲取父標籤,並將test的boolean值傳遞過去
ChooseTag ct=(ChooseTag)this.getParent();
ct.setFlag(test);
}
}
OtherWiseTag.java
package tag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class OtherwiseTag extends SimpleTagSupport{
@Override
public void doTag() throws JspException, IOException {
//通過父標籤傳遞when標籤中的test值
ChooseTag ct=(ChooseTag)this.getParent();
boolean test=ct.isFlag();
if(!test) {
this.getJspBody().invoke(null);
}
}
}
ForEachTag.java
package tag;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class ForEachTag extends SimpleTagSupport{
private Object items;//需要遍歷的資料 list和map
private String var;//每個元素的名稱
public void setItems(Object items) {
this.items=items;
}
public void setVar(String var) {
this.var=var;
}
@Override
public void doTag() throws JspException, IOException {
PageContext pageContext=(PageContext)this.getJspContext();
Collection colls=null;
if(items instanceof List) {
colls=(List)items;
}
if(items instanceof Map) {
colls=((Map) items).entrySet();
}
for(Object object:colls) {
//把每個物件放入域物件中
pageContext.setAttribute(var, object);
this.getJspBody().invoke(null);
}
}
}
《3》imitate.core.jsp檔案:
<%@ page language="java" import="java.util.*,entity.Student" pageEncoding="utf-8"%>
<%@taglib uri="https://blog.csdn.net/qq_34774655" prefix="xiaohua"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>高仿的jstl之核心標籤庫</title>
</head>
<body>
<%--if標籤:單條件判斷 --%>
<xiaohua:if test="${5>2}">
條件成立,執行。
</xiaohua:if> <br/><hr/>
<%--choose標籤:多條件判斷 --%>
<c:set var="score" value="80"></c:set>
<xiaohua:choose>
<xiaohua:when test="${score>=90 }">優秀</xiaohua:when>
<xiaohua:when test="${score>=60 && score<90 }">良好</xiaohua:when>
<xiaohua:otherwise>不及格</xiaohua:otherwise>
</xiaohua:choose> <br/><hr/>
<%--forEach標籤 --%>
<%
//list
List<Student> list=new ArrayList<Student>();
list.add(new Student("xiaohua", 24));
list.add(new Student("xiaona",24));
pageContext.setAttribute("list", list);
//Map
Map<String,Student> map = new HashMap<String,Student>();
map.put("001",new Student("xiaohua", 24));
map.put("002",new Student("xiaona",24));
pageContext.setAttribute("map",map);
%>
<xiaohua:forEach items="${list}" var="stu" >
姓名: ${stu.name} - 年齡:${stu.age}<br/>
</xiaohua:forEach><hr/>
<xiaohua:forEach items="${map}" var="entry">
序號:${entry.key } - 姓名: ${entry.value.name } - 年齡:${entry.value.age }<br/>
</xiaohua:forEach><hr/>
xiaohua 2018-11-1
</body>
</html>
《4》瀏覽器檢視: