* *为什么要使用自定义标签:*
简化开发,可以提高代码的重用性,让JSP页面更加专注于视图层。
*很少,甚至不会去自定义标签
< 前缀:标签名>
< 前缀:标签名>
使用自定义标签,在该页面引入标签的tld文件
空标签
带属性的标签()
带标签体的标签(… …
)
带属性带标签体(… …)
1)、java类
2)、实现SimpleTag接口
3)、重写SimpleTag接口中的方法
doTag:当页面解析到该自定义标签的时候执行该方法
getParent
setJspBody
setJspContext
setParent
在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"%>
Insert title here
欢迎使用自定义标签
空标签
带属性的标签
带两个属性的标签
验证标签的rtexprvalue
<%
pageContext.setAttribute("name", "小朱呀");
%>
web.xml
<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>javaWebSenior04display-name>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
<welcome-file>index.htmwelcome-file>
<welcome-file>index.jspwelcome-file>
<welcome-file>default.htmlwelcome-file>
<welcome-file>default.htmwelcome-file>
<welcome-file>default.jspwelcome-file>
welcome-file-list>
web-app>
myTag.tld文档
<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.0tlib-version>
<short-name>zshort-name>
<uri>http://www.java53.com/coreuri>
<tag>
<name>nowDatename>
<tag-class>com.zuxia.tag.MyTag01tag-class>
<body-content>body-content>
tag>
<tag>
<name>nowDateFormatname>
<tag-class>com.zuxia.tag.MyTag02tag-class>
<body-content>emptybody-content>
<attribute>
<name>formatname>
<required>falserequired>
<rtexprvalue>falsertexprvalue>
attribute>
tag>
<tag>
<name>nowDateFormatColorname>
<tag-class>com.zuxia.tag.MyTag03tag-class>
<body-content>emptybody-content>
<attribute>
<name>formatname>
<required>falserequired>
<rtexprvalue>falsertexprvalue>
attribute>
<attribute>
<name>colorname>
<required>falserequired>
<rtexprvalue>falsertexprvalue>
attribute>
tag>
<tag>
<name>testname>
<tag-class>com.zuxia.tag.MyTag04tag-class>
<body-content>emptybody-content>
<attribute>
<name>testValname>
<required>truerequired>
<rtexprvalue>truertexprvalue>
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(""+res+"");
}
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
}
}