1. 自定义标签开发步骤
2. UI标签
3. 控制标签
4. 数据标签
例:
set out标签
1.先创建助手类(继承BodyTagSupport)
SetTag
package com.aixincheng.jsp02;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class SetTag extends BodyTagSupport{
/**
*
*/
private static final long serialVersionUID = -3728368202607905437L;
private String var;
private String value;
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public int doStartTag() throws JspException {
// TODO Auto-generated method stub
pageContext.setAttribute(var, value);
return SKIP_BODY;
}
}
OutTag
package com.aixincheng.jsp;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class OutTag extends BodyTagSupport {
/**
*
*/
private static final long serialVersionUID = 8035613228412107665L;
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public int doStartTag() throws JspException {
// TODO Auto-generated method stub
JspWriter out = pageContext.getOut();
try {
out.print(value);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SKIP_BODY;
}
}
将setTag和outTag加入tld文件中
<?xml version="1.0" encoding="UTF-8" ?>
<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">
<description>JSTL 1.1 core library</description>
<display-name>JSTL core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>c</short-name>
<uri>/houyitao</uri>
<tag>
<name>set</name>
<tag-class>com.aixincheng.jsp02.SetTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>out</name>
<tag-class>com.aixincheng.jsp02.OutTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
在写入jsp页面中
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="com.aixincheng.jsp02.Student"%>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="/aixincheng" 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<z:set var="name" value="wupeng"></z:set>
<z:out value="${name }"></z:out>
<br>
</body>
</html>
输出结果为
WUPENG
if标签
助手类IfTag
package com.aixincheng.jsp02;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class IfTag extends BodyTagSupport {
/**
*
*/
private static final long serialVersionUID = 3982324939625320781L;
private boolean test;
public boolean isTest() {
return test;
}
public void setTest(boolean test) {
this.test = test;
}
@Override
public int doStartTag() throws JspException {
// TODO Auto-generated method stub
return test?EVAL_BODY_INCLUDE:SKIP_BODY;
}
}
将ifTag加入tld文件中
<tag>
<name>if</name>
<tag-class>com.aixincheng.jsp02.IfTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
写入jsp页面中
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="com.aixincheng.jsp02.Student"%>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="/houyitao" 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<br>
<z:if test="true">aa</z:if>
<z:if test="false">bb</z:if>
<br>
</body>
</html>
forEach标签
助手类ForeachTag
package com.aixincheng.jsp02;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class ForeachTag extends BodyTagSupport {
/**
* var 指针
* items 指的是需要遍历的集合
*
*
*/
private static final long serialVersionUID = -5803826429287328786L;
private String var;
private List<Object> items=new ArrayList<>();
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
public List<Object> getItems() {
return items;
}
public void setItems(List<Object> items) {
this.items = items;
}
@Override
public int doStartTag() throws JspException {
// TODO Auto-generated method stub
Iterator<Object> it = items.iterator();
pageContext.setAttribute(var, it.next());
pageContext.setAttribute("it",it);
return EVAL_BODY_INCLUDE;
}
@Override
public int doAfterBody() throws JspException {
// TODO Auto-generated method stub
Iterator<Object> it = (Iterator<Object>)pageContext.getAttribute("it");
if(it.hasNext()) {
pageContext.setAttribute(var, it.next());
pageContext.setAttribute("it",it);
return EVAL_BODY_AGAIN;
}else {
return EVAL_PAGE;
}
}
}
把ForeachTag加入tld文件中
<?xml version="1.0" encoding="UTF-8" ?>
<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">
<description>JSTL 1.1 core library</description>
<display-name>JSTL core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>c</short-name>
<uri>/houyitao</uri>
<tag>
<name>foreach</name>
<tag-class>com.aixincheng.jsp02.ForeachTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
写入jsp页面中
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="com.aixincheng.jsp02.Student"%>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="/houyitao" 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
List list=new ArrayList();
list.add(new Student("1","aa"));
list.add(new Student("2","bb"));
list.add(new Student("3","cc"));
list.add(new Student("4","dd"));
request.setAttribute("list",list);
%>
<z:foreach items="${list }" var="stu">
${stu.sid }==${stu.sname }<br></z:foreach>
<br>
</body>
</html>
select标签
SelectTag助手类
package com.aixincheng.jsp02;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import org.apache.commons.beanutils.BeanUtils;
public class SelectTag extends BodyTagSupport {
/**查询维度:下拉列表
* 修改页面:下拉列表,数据回显
*
* 1.id,name
* 2.数据源,存入数据库的值textKey,展示列textVal
* 3.加入属性(默认的头部属性值,默认的展示列值)
* 4.加入属性,能够实现数据回显的功能selectedVal
*
*/
private static final long serialVersionUID = -5620694419384806065L;
private String id;
private String name;
private List<Object> items=new ArrayList<>();
private String testKey;
private String textval;
private String headTextkey;
private String headTextval;
private String selectedval;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Object> getItems() {
return items;
}
public void setItems(List<Object> items) {
this.items = items;
}
public String getTestKey() {
return testKey;
}
public void setTestKey(String testKey) {
this.testKey = testKey;
}
public String getTextval() {
return textval;
}
public void setTextval(String textval) {
this.textval = textval;
}
public String getHeadTextkey() {
return headTextkey;
}
public void setHeadTextkey(String headTextkey) {
this.headTextkey = headTextkey;
}
public String getHeadTextval() {
return headTextval;
}
public void setHeadTextval(String headTextval) {
this.headTextval = headTextval;
}
public String getSelectedval() {
return selectedval;
}
public void setSelectedval(String selectedval) {
this.selectedval = selectedval;
}
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.write(toHTML());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.doStartTag();
}
/**
* 拼接处下拉列表所对应的select的html的代码
* <select id='' name=''>
<option value='' selected>---请选择---</option>
</select>
* @return
* @throws SecurityException
* @throws NoSuchFieldException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws NoSuchMethodException
* @throws InvocationTargetException
*/
private String toHTML() throws Exception {
StringBuilder sb = new StringBuilder();
sb.append("<select id='" + id + "' name='" + name + "'>");
if (!(headTextkey == null || "".equals(headTextkey) || headTextval == null || "".equals(headTextkey))) {
sb.append("<option value='" + headTextkey + "' selected>" + headTextkey + "</option>");
}
String val;
String html;
for (Object obj : items) {
// 需求:
// 让学生id存入数据,让学生的名字展示在jsp页面
// 比如:<option value='01' selected>张三</option>
Field textKeyField = obj.getClass().getDeclaredField(testKey);
textKeyField.setAccessible(true);
val = (String) textKeyField.get(obj);
html = BeanUtils.getProperty(obj, textval);
if(val.equals(selectedval)) {
sb.append(" <option value='"+val+"'selected>"+html+"</option>");
}else {
sb.append("<option value='"+val+"'>"+html+"</option>");
}
}
sb.append("</select>");
return sb.toString();
}
}
将selectTag加入tld文件里
package com.aixincheng.jsp02;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import org.apache.commons.beanutils.BeanUtils;
public class SelectTag extends BodyTagSupport {
/**查询维度:下拉列表
* 修改页面:下拉列表,数据回显
*
* 1.id,name
* 2.数据源,存入数据库的值textKey,展示列textVal
* 3.加入属性(默认的头部属性值,默认的展示列值)
* 4.加入属性,能够实现数据回显的功能selectedVal
*
*/
private static final long serialVersionUID = -5620694419384806065L;
private String id;
private String name;
private List<Object> items=new ArrayList<>();
private String testKey;
private String textval;
private String headTextkey;
private String headTextval;
private String selectedval;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Object> getItems() {
return items;
}
public void setItems(List<Object> items) {
this.items = items;
}
public String getTestKey() {
return testKey;
}
public void setTestKey(String testKey) {
this.testKey = testKey;
}
public String getTextval() {
return textval;
}
public void setTextval(String textval) {
this.textval = textval;
}
public String getHeadTextkey() {
return headTextkey;
}
public void setHeadTextkey(String headTextkey) {
this.headTextkey = headTextkey;
}
public String getHeadTextval() {
return headTextval;
}
public void setHeadTextval(String headTextval) {
this.headTextval = headTextval;
}
public String getSelectedval() {
return selectedval;
}
public void setSelectedval(String selectedval) {
this.selectedval = selectedval;
}
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.write(toHTML());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.doStartTag();
}
/**
* 拼接处下拉列表所对应的select的html的代码
* <select id='' name=''>
<option value='' selected>---请选择---</option>
</select>
* @return
* @throws SecurityException
* @throws NoSuchFieldException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws NoSuchMethodException
* @throws InvocationTargetException
*/
private String toHTML() throws Exception {
StringBuilder sb = new StringBuilder();
sb.append("<select id='" + id + "' name='" + name + "'>");
if (!(headTextkey == null || "".equals(headTextkey) || headTextval == null || "".equals(headTextkey))) {
sb.append("<option value='" + headTextkey + "' selected>" + headTextkey + "</option>");
}
String val;
String html;
for (Object obj : items) {
// 需求:
// 让学生id存入数据,让学生的名字展示在jsp页面
// 比如:<option value='01' selected>张三</option>
Field textKeyField = obj.getClass().getDeclaredField(testKey);
textKeyField.setAccessible(true);
val = (String) textKeyField.get(obj);
html = BeanUtils.getProperty(obj, textval);
if(val.equals(selectedval)) {
sb.append(" <option value='"+val+"'selected>"+html+"</option>");
}else {
sb.append("<option value='"+val+"'>"+html+"</option>");
}
}
sb.append("</select>");
return sb.toString();
}
}
写入jsp页面中
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="com.aixincheng.jsp02.Student"%>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="/houyitao" 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
List list=new ArrayList();
list.add(new Student("1","aa"));
list.add(new Student("2","bb"));
list.add(new Student("3","cc"));
list.add(new Student("4","dd"));
request.setAttribute("list",list);
%>
<z:select items="${list }" textval="sname" testKey="sid" headTextkey="===请选择学生===" headTextval="-1" ></z:select>
</body>
</html>
输出结果
下拉回显:
<%
List list=new ArrayList();
list.add(new Student("1","aa"));
list.add(new Student("2","bb"));
list.add(new Student("3","cc"));
list.add(new Student("4","dd"));
request.setAttribute("list",list);
%>
<z:select items="${list }" textval="sname" testKey="sid" headTextkey="===请选择学生===" headTextval="-1" selectedval="3"></z:select>
结果: