04.自定义标签
1、自定义标签(在JSP页面上使用自定义标签)
* *为什么要使用自定义标签:*
简化开发,可以提高代码的重用性,让JSP页面更加专注于视图层。
*很少,甚至不会去自定义标签
2、什么是自定义标签
< 前缀:标签名>
< 前缀:标签名>
使用自定义标签,在该页面引入标签的tld文件
3、自定义标签
空标签
带属性的标签()
带标签体的标签(… …
)
带属性带标签体(… …)
4、自定义标签
A、定义标签处理类
1)、java类
2)、实现SimpleTag接口
3)、重写SimpleTag接口中的方法
doTag:当页面解析到该自定义标签的时候执行该方法
getParent
setJspBody
setJspContext
setParent
B、定义标签描述文档
在webinfo下创建描述文档
创建XML文件->
给文档命名xxx.tld->
选择文档的模板create XML from schema file->select XML Catalog entry->web-jsptaglibrary_2_1
- 练习题:
自定义标签
创建钱币格式化标签
<m:formatMoney val="${userMoney}"/>
要求:根据EL表达式传入的double类型数据,格式化钱币例如:
输入:userMoney=888.8888
输出:¥88.89
结果保留两位有效数字
创建商品名称截取标签
<m:subGoodsName val="${goodsName}" len="5">
要求:按照指定长度截取商品名称,截取后在商品名称后面追加省略号(...),例如:
输出:goodsName="本田雅阁2020款式 2.0T" len=5
输出:本田雅阁2...
创建表格生成标签
<m:listToTable var="${stus}" tableClassName="tableClass" tdClassName="tdClass">
要求:根据传入的集合生成表格进行输出,并为生成的表格指定用户传入的表格样式名称(tableClassName),
为生成的单元格(td)指定用户传入的单元格样式(tdClassName)例如:
输入:
List<StuInfo> stus = new ArrayList<StuInfo>();
stus.add(new StuInfo(1,"小张","男","1999-09-09","重庆市沙坪坝区",1.75f));
stus.add(new StuInfo(2,"小王","男","1999-09-09","重庆市沙坪坝区",1.75f));
stus.add(new StuInfo(3,"小李","男","1999-09-09","重庆市沙坪坝区",1.75f));
stus.add(new StuInfo(4,"小周","男","1999-09-09","重庆市沙坪坝区",1.75f));
stus.add(new StuInfo(5,"小找","男","1999-09-09","重庆市沙坪坝区",1.75f));
stus.add(new StuInfo(6,"小罗","男","1999-09-09","重庆市沙坪坝区",1.75f));
<table>
<tr>
<td>1</td>
<td>小张</td>
<td>男</td>
<td>1999-09-09</td>
<td>1.75f</td>
</tr>
... ...
</table>
案例:
jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- 页面需要知道该标签的描述文档,才能使用该自定义标签 -->
<%@ taglib prefix="z" uri="http://www.java53.com/core" %>
<%@ taglib uri="http://www.java53.com/core" prefix="z"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>欢迎使用自定义标签</h1>
<h3>空标签</h3>
<z:nowDate/>
<h3>带属性的标签</h3>
<z:nowDateFormat format="yyyy年MM月dd日 HH时mm分ss秒"/><br/>
<z:nowDateFormat format="yyyy/MM/dd HH:mm:ss"/><br/>
<z:nowDateFormat format="HH时mm分ss秒 yyyy年MM月dd日"/><br/>
<h4>带两个属性的标签</h4>
<z:nowDateFormatColor format="yyyy年MM月dd日 HH时mm分ss秒" color="red"/><br/>
<z:nowDateFormatColor format="yyyy/MM/dd HH:mm:ss" /><br/>
<h4>验证标签的rtexprvalue</h4>
<%
pageContext.setAttribute("name", "小朱呀");
%>
<z:test testVal="${pageScope.name }"/>
<z:test testVal="${pageScope.name }"/>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>javaWebSenior04</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
myTag.tld文档
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-jsptaglibrary_2_1.xsd ">
<!-- 关于标签库的描述 -->
<!-- 我们自定义标签的版本 -->
<tlib-version>1.0</tlib-version>
<!-- 使用该自定义标签时的推荐前缀 -->
<short-name>z</short-name>
<!-- 该描述文档的统一资源标识符 -->
<!-- http://www.java53.com/core -->
<uri>http://www.java53.com/core</uri>
<!-- 关于标签的描述 -->
<tag>
<!-- 设置该自定义标签的名称 -->
<name>nowDate</name>
<!-- 访问到该自定义标签的时候交给哪个标签类去处理 -->
<tag-class>com.zuxia.tag.MyTag01</tag-class>
<!-- 标签体内容为空 -->
<body-content></body-content>
</tag>
<tag>
<name>nowDateFormat</name>
<tag-class>com.zuxia.tag.MyTag02</tag-class>
<body-content>empty</body-content>
<!-- 如果标签有属性,则需要通过该标签进行申明 -->
<attribute>
<!-- 属性名 -->
<name>format</name>
<!-- 是否必须改属性 -->
<required>false</required>
<!-- 是否允许使用 EL -->
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
<tag>
<name>nowDateFormatColor</name>
<tag-class>com.zuxia.tag.MyTag03</tag-class>
<body-content>empty</body-content>
<!-- 如果标签有属性,则需要通过该标签进行申明 -->
<attribute>
<name>format</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>color</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
<tag>
<name>test</name>
<tag-class>com.zuxia.tag.MyTag04</tag-class>
<body-content>empty</body-content>
<!-- 如果标签有属性,则需要通过该标签进行申明 -->
<attribute>
<name>testVal</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
maTag01.java
package com.zuxia.tag;
import java.io.IOException;
import java.util.Date;
import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.JspTag;
import javax.servlet.jsp.tagext.SimpleTag;
/**
* 第一个标签处理类文件
* @author Administrator
*
*/
public class MyTag01 implements SimpleTag {
//声明变量,用于存放执行该标签的JSP页面的pageContext
private JspContext pageContext;
/**
* 当JSP页面执行到该标签的时候,执行该方法
*/
@Override
public void doTag() throws JspException, IOException {
System.out.println("我是自定义标签的doTag方法中输出的内容");
Date dt = new Date();
//System.out.println(dt.toLocaleString());
//获得向JSP页面输出内容的out对象
JspWriter out = pageContext.getOut();
out.print(dt.toLocaleString());
}
/**
* 该方法由JSP引擎自动调用
* 只用于获取PageContext对象
*/
@Override
public void setJspContext(JspContext arg0) {
this.pageContext = arg0;
}
@Override
public JspTag getParent() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setJspBody(JspFragment arg0) {
// TODO Auto-generated method stub
}
@Override
public void setParent(JspTag arg0) {
// TODO Auto-generated method stub
}//
}
myTag02.java
package com.zuxia.tag;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
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 MyTag02 implements SimpleTag {
private JspContext pageContext;
private String format;
@Override
public void doTag() throws JspException, IOException {
SimpleDateFormat sdf= new SimpleDateFormat(format);
Date dt = new Date();
String res = sdf.format(dt);
pageContext.getOut().print(res);
}
public void setFormat(String format) {
this.format = format;
}
@Override
public void setJspContext(JspContext arg0) {
this.pageContext = arg0;
}
@Override
public JspTag getParent() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setJspBody(JspFragment arg0) {
// TODO Auto-generated method stub
}
@Override
public void setParent(JspTag arg0) {
// TODO Auto-generated method stub
}
}
myTag03.java
package com.zuxia.tag;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
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 MyTag03 implements SimpleTag {
private JspContext pageContext;
private String format;
private String color;
@Override
public void doTag() throws JspException, IOException {
SimpleDateFormat sdf= new SimpleDateFormat(format);
Date dt = new Date();
String res = sdf.format(dt);
pageContext.getOut().print("<font color='"+color+"'>"+res+"</font>");
}
public void setFormat(String format) {
this.format = format;
}
public void setColor(String color) {
this.color = color;
}
@Override
public void setJspContext(JspContext arg0) {
this.pageContext = arg0;
}
@Override
public JspTag getParent() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setJspBody(JspFragment arg0) {
// TODO Auto-generated method stub
}
@Override
public void setParent(JspTag arg0) {
// TODO Auto-generated method stub
}
}
myTag04.java
package com.zuxia.tag;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
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 MyTag04 implements SimpleTag {
private JspContext pageContext;
private String testVal;
@Override
public void doTag() throws JspException, IOException {
System.out.println(testVal);
}
public void setTestVal(String testVal) {
this.testVal = testVal;
}
@Override
public void setJspContext(JspContext arg0) {
this.pageContext = arg0;
}
@Override
public JspTag getParent() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setJspBody(JspFragment arg0) {
// TODO Auto-generated method stub
}
@Override
public void setParent(JspTag arg0) {
// TODO Auto-generated method stub
}
}