一.自定义标签语言的特点
<开始标签 属性=”属性值”>标签体</结束标签>
空标签
<br/><hr/>
<开始标签></结束标签>
<开始标签/>
ui标签 控制标签 数据标签(数据标签就是用来存储数据的)
z:out 1 m:if 2 m:set 4
m:select 5 m:forEach 3
二.JSP自定义标签生命周期图
SKIP_BODY:跳过主体 EVAL_BODY_INCLUDE:计算标签主体内容并[输出] EVAL_BODY_BUFFERED:计算标签主体内容并[缓存]
EVAL_PAGE:计算页面的后续部分 SKIP_PAGE:跳过页面的后续部分 EVAL_BODY_AGAIN:再计算主体一次
三. 自定义标签的开发及使用步骤
1.创建标签库类(继承BodyTagSupport)
标签属性必须与助手类的属性对应、并且要提供对应get/set方法 2.创建标签库文件(在WEB-INF)下面,以tld为后缀,添加自定义标签的配置 3.页面引入 在JSP通过taglib指令导入标签库,并通过指定后缀自定义标签
四.自定义标签案例
ui标签 控制标签 数据标签(数据标签就是用来存储数据的)
z:out 1 m:if 2 m:set 4
m:select 5 m:forEach 3
助手类(set out if )
SetTag
1 package com.JavaCustomLabel.day; 2 3 import javax.servlet.jsp.JspException; 4 import javax.servlet.jsp.tagext.BodyTagSupport; 5 6 public class SetTag extends BodyTagSupport{ 7 8 /** 9 * 10 */ 11 private static final long serialVersionUID = 4392633628405470426L; 12 13 private String var; 14 private String value; 15 public String getVar() { 16 return var; 17 } 18 public void setVar(String var) { 19 this.var = var; 20 } 21 public String getValue() { 22 return value; 23 } 24 public void setValue(String value) { 25 this.value = value; 26 } 27 @Override 28 public int doStartTag() throws JspException { 29 // TODO Auto-generated method stub 30 pageContext.setAttribute(var, value); 31 32 return SKIP_BODY; 33 } 34 }
OutTag
package com.JavaCustomLabel.day;
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 = 3372012340573875946L;
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;
}
}
IfTag
package com.JavaCustomLabel.day; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.BodyTagSupport; public class IfTag extends BodyTagSupport{ /** * */ private static final long serialVersionUID = -1792746228632351099L; private Boolean test; public Boolean getTest() { 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; } }
x.tld文件
<?xml version="1.0" encoding="UTF-8"?> <taglib xmlns="http://java.sun.com/JSP/TagLibraryDescriptor"> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>Simple Tags</short-name> <uri>/XuFanQi</uri> <!-- set标签 --> <tag> <!-- 标签库中的标签 (类似c:set c:out的定义) --> <name>set</name> <!-- 是标签运行具体代码,也就是助手类,下面填写的助手类的全路径名 --> <tag-class>com.JavaCustomLabel.day.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> <!-- out标签 --> <tag> <name>out</name> <tag-class>com.JavaCustomLabel.day.OutTag</tag-class> <body-content>JSP</body-content> <attribute> <name>value</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <!-- IF标签 --> <tag> <name>if</name> <tag-class>com.JavaCustomLabel.day.IfTag</tag-class> <body-content>JSP</body-content> <attribute> <name>test</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
jsp页面引入
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="/XuFanQi" prefix="x" %> <!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> <x:Set var="name" value="李四"></x:Set> <x:Out value="${name }"></x:Out> <br> --------------------- <br> <x:if test="true">张三</x:if> <x:if test="false">王五</x:if> </body> </html>
页面引入显示
Foreach标签
ForeachTag(助手类)
1 package com.JavaCustomLabel.day; 2 3 import java.util.ArrayList; 4 import java.util.Iterator; 5 import java.util.List; 6 7 import javax.servlet.jsp.JspException; 8 import javax.servlet.jsp.tagext.BodyTagSupport; 9 10 /** 11 * c:forech 12 * 13 * var 指针 14 * items 指的是需要遍历的集合对象 15 * <c:forech var="stu" items="stus"> 16 * ${stu.sid} 17 * ...... 18 * </c:forech> 19 * @author Administrator 20 * 21 */ 22 public class ForeachTag extends BodyTagSupport { 23 24 25 private static final long serialVersionUID = 3755053789995595493L;26 27 private String var; 28 private List<Object> items = new ArrayList<>(); 29 public String getVar() { 30 return var; 31 } 32 public void setVar(String var) { 33 this.var = var; 34 } 35 public List<Object> getItems() { 36 return items; 37 } 38 public void setItems(List<Object> items) { 39 this.items = items; 40 } 41 42 @Override 43 public int doStartTag() throws JspException { 44 45 Iterator it = items.iterator(); 46 // 默认指针存放的位置是没有指向对象的,现在需要指向对象。那么需要指针下移 47 pageContext.setAttribute(var, it.next()); 48 // 英文集合中元素较多,那么指针需要循环的向下移动,那么需要我们将迭代器保存,用于下一次指针下移所用 49 pageContext.setAttribute("it", it); 50 return EVAL_BODY_INCLUDE; 51 } 52 53 @Override 54 public int doAfterBody() throws JspException { 55 // 获取以保存的迭代器 56 Iterator it = (Iterator) pageContext.getAttribute("it"); 57 if(it.hasNext()) { 58 // 指针下移指向新的元素 59 pageContext.setAttribute(var, it.next()); 60 // 再保存正在使用的迭代器 61 pageContext.setAttribute("it", it); 62 return EVAL_BODY_AGAIN; 63 }else { 64 return EVAL_PAGE; 65 } 66 } 67 }
x.tld文件
1 <!-- Foreach标签 --> 2 <tag> 3 <name>foreach</name> 4 <tag-class>com.JavaCustomLabel.day.ForeachTag</tag-class> 5 <body-content>JSP</body-content> 6 <attribute> 7 <name>var</name> 8 <required>true</required> 9 <rtexprvalue>false</rtexprvalue> 10 </attribute> 11 <attribute> 12 <name>items</name> 13 <required>true</required> 14 <rtexprvalue>true</rtexprvalue> 15 </attribute> 16 </tag>
jsp页面引入
1 <% 2 List<Student> list = new ArrayList<Student>(); 3 list.add(new Student("s001","zs")); 4 list.add(new Student("s002","li")); 5 list.add(new Student("s003","zz")); 6 request.setAttribute("stus", list); 7 %> 8 <br> 9 <ul> 10 <x:foreach items="${stus }" var="s"> 11 ${s.sid } 12 ${s.sname }<br> 13 </z:foreach> 14 </ul> 15 <br>
select标签
SelectTag(助手类)
1 package com.JavaCustomLabel.day; 2 3 import java.io.IOException; 4 import java.lang.reflect.Field; 5 import java.lang.reflect.InvocationTargetException; 6 import java.util.ArrayList; 7 import java.util.List; 8 9 import javax.servlet.jsp.JspException; 10 import javax.servlet.jsp.JspWriter; 11 import javax.servlet.jsp.tagext.BodyTagSupport; 12 13 import org.apache.commons.beanutils.BeanUtils; 14 15 /** 16 * <select id='' name=''> 17 * <option value='-1' selected >---请选择---</option> 18 * </select> 19 * 20 * 21 * ---> 22 * <z:select ....></select> 23 * 查询维度: 下拉列表 24 * 修改页面: 下拉列表 数据回显 25 * 26 * 1.id name 27 * 2.数据源,存入数据库中的值 textKey、展示列textVal 28 * 3.加入属性(默认的头部属性值headTextKey、默认的展示列值headTextVal) 29 * 4.加入属性,能够实现数据回显的功能selectedVal 30 * 31 * @author Administrator 32 * 33 */ 34 public class SelectTag extends BodyTagSupport { 35 36 37 private static final long serialVersionUID = -8499199884457216397L; 38 39 private String id; 40 private String name; 41 private List<Object> items = new ArrayList<>(); 42 private String textKey; 43 private String textVal; 44 private String headTextKey; 45 private String headTextVal; 46 private String selectedVal; 47 public String getId() { 48 return id; 49 } 50 public void setId(String id) { 51 this.id = id; 52 } 53 public String getName() { 54 return name; 55 } 56 public void setName(String name) { 57 this.name = name; 58 } 59 public List<Object> getItems() { 60 return items; 61 } 62 public void setItems(List<Object> items) { 63 this.items = items; 64 } 65 public String getTextKey() { 66 return textKey; 67 } 68 public void setTextKey(String textKey) { 69 this.textKey = textKey; 70 } 71 public String getTextVal() { 72 return textVal; 73 } 74 public void setTextVal(String textVal) { 75 this.textVal = textVal; 76 } 77 public String getHeadTextKey() { 78 return headTextKey; 79 } 80 public void setHeadTextKey(String headTextKey) { 81 this.headTextKey = headTextKey; 82 } 83 public String getHeadTextVal() { 84 return headTextVal; 85 } 86 public void setHeadTextVal(String headTextVal) { 87 this.headTextVal = headTextVal; 88 } 89 public String getSelectedVal() { 90 return selectedVal; 91 } 92 public void setSelectedVal(String selectedVal) { 93 this.selectedVal = selectedVal; 94 } 95 96 @Override 97 public int doStartTag() throws JspException { 98 JspWriter out = pageContext.getOut(); 99 try { 100 out.write(toHTML()); 101 } catch (IOException | NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { 102 // TODO Auto-generated catch block 103 e.printStackTrace(); 104 } 105 106 return super.doStartTag(); 107 } 108 109 /** 110 * 拼接出下拉 列表所对应的selecte的HTML代码 111 * <select id='' name=''> 112 <option value='-1' selected >---请选择---</option> 113 </select> 114 * @return 115 * @throws SecurityException 116 * @throws NoSuchFieldException 117 * @throws IllegalAccessException 118 * @throws IllegalArgumentException 119 * @throws NoSuchMethodException 120 * @throws InvocationTargetException 121 */ 122 private String toHTML() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { 123 StringBuilder sb = new StringBuilder(); 124 sb.append("<select id='"+id+"' name='"+name+"' >"); 125 if(!(headTextKey == null || "".equals(headTextKey) || headTextVal == null || "".equals(headTextVal))) { 126 sb.append("<option value='"+headTextKey+"' selected >"+headTextVal+"</option>"); 127 } 128 String val; 129 String html; 130 for (Object obj : items) { 131 // 让学生的id存入数据库,让学生的名字展示在jsp页面 132 // <option value='s001'>zs</option> 133 // obj sid sname 134 // 第一种方式 135 Field textKeyField = obj.getClass().getDeclaredField(textKey); 136 textKeyField.setAccessible(true); 137 val = (String) textKeyField.get(obj); 138 // 第二种方式(需要架包) 139 html = BeanUtils.getProperty(obj, textVal); 140 if(val.equals(selectedVal)) { 141 sb.append("<option value='"+val+"' selected>"+html+"</option>"); 142 }else { 143 sb.append("<option value='"+val+"' >"+html+"</option>"); 144 } 145 } 146 sb.append("</select>"); 147 return sb.toString(); 148 } 149 }
x.tld文件
1 <tag> 2 <name>select</name> 3 <tag-class>com.JavaCustomLabel.day.SelectTag</tag-class> 4 <body-content>JSP</body-content> 5 <attribute> 6 <name>id</name> 7 <required>false</required> 8 <rtexprvalue>false</rtexprvalue> 9 </attribute> 10 <attribute> 11 <name>name</name> 12 <required>false</required> 13 <rtexprvalue>false</rtexprvalue> 14 </attribute> 15 <attribute> 16 <name>items</name> 17 <required>true</required> 18 <rtexprvalue>true</rtexprvalue> 19 </attribute> 20 <attribute> 21 <name>textKey</name> 22 <required>true</required> 23 <rtexprvalue>false</rtexprvalue> 24 </attribute> 25 <attribute> 26 <name>textVal</name> 27 <required>true</required> 28 <rtexprvalue>false</rtexprvalue> 29 </attribute> 30 <attribute> 31 <name>headTextKey</name> 32 <required>false</required> 33 <rtexprvalue>false</rtexprvalue> 34 </attribute> 35 <attribute> 36 <name>headTextVal</name> 37 <required>false</required> 38 <rtexprvalue>false</rtexprvalue> 39 </attribute> 40 <attribute> 41 <name>selectedVal</name> 42 <required>false</required> 43 <rtexprvalue>true</rtexprvalue> 44 </attribute> 45 </tag>
jsp页面引入
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib uri="/XuFanQi" prefix="x" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 12 <x:select headTextKey="-1" headTextVal="---请选择---" selectedVal="s003" textVal="sname" items="${stus }" textKey="sid"></z:select> 13 14 </body> 15 </html>
今天的文章自定义标签_独特的个人标签分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:http://bianchenghao.cn/46648.html