public class HelloWorldTag extends TagSupport {
private static final long serialVersionUID = -3382691015235241708L;
@Override
public int doEndTag() throws JspException {
try {
pageContext.getOut().write("Hello World !");
return super.doEndTag();
} catch (JspException e) {
e.printStackTrace();
return 0;
} catch (IOException e) {
e.printStackTrace();
return 0;
}
}
@Override
public int doStartTag() {
try {
pageContext.getOut().write("Hello World");
return super.doStartTag();
} catch (JspException e) {
e.printStackTrace();
return 0;
} catch (IOException e) {
e.printStackTrace();
return 0;
}
}
}
<?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 web-jsptaglibrary_2_0.xsd"
version="2.0">
<tlib-version>1.0</tlib-version>
<short-name>myhelloworld</short-name>
<!-- 定义该标签库的URI 必须添加但可以空-->
<uri></uri>
<!-- 定义第一个标签 -->
<tag>
<!-- 定义标签名 -->
<name>helloWorld</name>
<!-- 定义标签处理类 -->
<tag-class>org.lxh.taglib.HelloWorldTag</tag-class>
<!-- 定义标签体为空 -->
<body-content>empty</body-content>
</tag>
</taglib>
package com.able.tag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class FieldTag extends TagSupport {
private static final long serialVersionUID = 1540529069962423355L;
private String field;
private Integer count;
@Override
public int doEndTag() throws JspException {
try {
JspWriter out = pageContext.getOut();
out.print(field);
out.print(count);
} catch (IOException e) {
e.printStackTrace();
}
return super.doEndTag();
}
public String getField() {
return field;
}
public void setField(String field) {
this.field = field;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
}
<tag>
<!-- 定义标签名 -->
<name>field</name>
<!-- 定义标签处理类 -->
<tag-class>com.able.tag.FieldTag</tag-class>
<!-- 定义标签体为空 -->
<body-content>empty</body-content>
<attribute>
<name>field</name>
<required>true</required> <!-- 是否必須赋值 -->
<rtexprvalue>true</rtexprvalue><!-- 表示是否接受jsp语法或者el语言或其他动态语言,默认false -->
</attribute>
<attribute>
<name>count</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
循环标签体类:ForEach.java
1import java.util.Collection;
2import java.util.Iterator;
3
4import javax.servlet.jsp.JspException;
5import javax.servlet.jsp.tagext.BodyContent;
6import javax.servlet.jsp.tagext.BodyTagSupport;
7
8public class ForEach extends BodyTagSupport
9{
10 private String id;
11 private String collection;
12 private Iterator iter;
13
14 public void setCollection(String collection)
15 {
16 this.collection = collection;
17 }
18 public void setId(String id)
19 {
20 this.id = id;
21 }
22
23 //遇到开始标签执行
24 public int doStartTag() throws JspException
25 {
26 Collection coll = (Collection) pageContext.findAttribute(collection);
27 // 表示如果未找到指定集合,则不用处理标签体,直接调用doEndTag()方法。
28 if(coll==null||coll.isEmpty()) return SKIP_BODY;
29
30 iter = coll.iterator();
31 pageContext.setAttribute(id, iter.next());
32 // 表示在现有的输出流对象中处理标签体,但绕过setBodyContent()和doInitBody()方法
33 // 这里一定要返回EVAL_BODY_INCLUDE,否则标签体的内容不会在网页上输出显示
34 return EVAL_BODY_INCLUDE;
35 }
36
37 //在doInitBody方法之前执行,在这里被绕过不执行
38 @Override
39 public void setBodyContent(BodyContent arg0)
40 {
41 System.out.println("setBodyContent");
42 super.setBodyContent(arg0);
43 }
44 //此方法被绕过不会被执行
45 @Override
46 public void doInitBody() throws JspException
47 {
48 System.out.println("doInitBody");
49 super.doInitBody();
50 }
51
52 //遇到标签体执行
53 public int doAfterBody() throws JspException
54 {
55 if(iter.hasNext())
56 {
57 pageContext.setAttribute(id, iter.next());
58 return EVAL_BODY_AGAIN;// 如果集合中还有对像,则循环执行标签体
59 }
60 return SKIP_BODY;//迭代完集合后,跳过标签体,调用doEndTag()方法。
61 }
62
63 //遇到结束标签执行
64 public int doEndTag() throws JspException
65 {
66 System.out.println("doEndTag");
67 return EVAL_PAGE;
68 }
69
70}
获取VO属性类:GetProperty.java
1import java.lang.reflect.Method;
2
3import javax.servlet.jsp.JspException;
4import javax.servlet.jsp.tagext.BodyTagSupport;
5
6public class GetProperty extends BodyTagSupport
7{
8
9 private String name;
10 private String property;
11
12 public void setName(String name)
13 {
14 this.name = name;
15 }
16
17 public void setProperty(String property)
18 {
19 this.property = property;
20 }
21
22 @SuppressWarnings("unchecked")
23 public int doStartTag() throws JspException
24 {
25 try
26 {
27 Object obj = pageContext.findAttribute(name);
28
29 if (obj == null) return SKIP_BODY;
30
31 Class c = obj.getClass();
32 //构造GET方法名字 get+属性名(属性名第一个字母大写)
33 String getMethodName = "get" + property.substring(0, 1).toUpperCase()
34 + property.substring(1, property.length());
35 Method getMethod = c.getMethod(getMethodName, new Class[]{});
36
37 pageContext.getOut().print(getMethod.invoke(obj));
38 System.out.print(property + ":" + getMethod.invoke(obj) + "t");
39 } catch (Exception e)
40 {
41 e.printStackTrace();
42 }
43 return SKIP_BODY;
44 }
45
46 public int doEndTag() throws JspException
47 {
48 return EVAL_PAGE;
49 }
50}
51
52表达式直接访问此类中静态的方法:ELFunction.java
53public class ELFunction
54{
55 public static int add( int i,int j )
56 {
57 return i+j;
58 }
59}
写一个测试用的VO类:UserVo.java
1public class UserVo
2{
3 private String name;
4 private String password;
5
6 public String getName()
7 {
8 return name;
9 }
10 public void setName(String name)
11 {
12 this.name = name;
13 }
14 public String getPassword()
15 {
16 return password;
17 }
18 public void setPassword(String password)
19 {
20 this.password = password;
21 }
22}
建好TLD文件tag.tld,放在WEB-INF目录下
1<?xml version="1.0" encoding="utf-8"?>
2<taglib version="2.0"
3 xmlns="http://java.sun.com/xml/ns/j2ee"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xmlns:shcemalocation="http://java.sun.com/xml/ns/j2ee
6 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
7
8 <description>自定义标签</description>
9 <display-name>JSTL core</display-name>
10 <tlib-version>1.1</tlib-version>
11 <short-name>firstLabel</short-name>
12 <uri>http://java.sun.com/jsp/jstl/core</uri>
13
14 <!-- 创建自定义 迭代标签 -->
15 <tag>
16 <name>forEach</name>
17 <tag-class>exercise.taglib.ForEach</tag-class>
18 <!-- 如果没有标签体,设置empty , 如果有标签休必须设置JSP-->
19 <body-content>JSP</body-content>
20 <attribute>
21 <name>id</name>
22 <required>true</required><!-- 标识属性是否是必须的 -->
23 <rtexprvalue>true</rtexprvalue><!-- 标识属性值是否可以用表达式语言 -->
24 </attribute>
25 <attribute>
26 <name>collection</name>
27 <required>true</required>
28 <rtexprvalue>true</rtexprvalue>
29 </attribute>
30 </tag>
31
32 <!-- 创建自定义获得属性标签 -->
33 <tag>
34 <name>getProperty</name>
35 <tag-class>exercise.taglib.GetProperty</tag-class>
36 <body-content>empty</body-content>
37 <attribute>
38 <name>name</name>
39 <required>true</required>
40 <rtexprvalue>true</rtexprvalue>
41 </attribute>
42 <attribute>
43 <name>property</name>
44 <required>true</required>
45 <rtexprvalue>true</rtexprvalue>
46 </attribute>
47 </tag>
48
49 <!-- 配置一个表达式调用 的函数 -->
50 <function>
51 <name>add</name><!-- 配置一个标签,在JSP页面通过引用前缀调用 -->
52 <function-class>exercise.taglib.ELFunction</function-class><!-- 实现类 -->
53 <function-signature>int add(int,int)</function-signature><!-- 静态的方法:包括返回类型,方法名,入参的类型 -->
54 </function>
55</taglib>
<jsp-config>
<taglib>
<taglib-uri>firstTag</taglib-uri>
<taglib-location>/WEB-INF/tag.tld</taglib-location>
</taglib>
</jsp-config>
在jsp文件中使用标签:tag.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="firstTag" prefix="my"%>
<jsp:useBean id="userVo1" class="exercise.vo.UserVo" scope="request">
<jsp:setProperty name="userVo1" property="name" value="Hackiller"/>
<jsp:setProperty name="userVo1" property="password" value="123"/>
</jsp:useBean>
<jsp:useBean id="userVo2" class="exercise.vo.UserVo" scope="request">
<jsp:setProperty name="userVo2" property="name" value="YangYang"/>
<jsp:setProperty name="userVo2" property="password" value="456"/>
</jsp:useBean>
<%
List list = new ArrayList();
list.add(userVo1);
list.add(userVo2);
pageContext.setAttribute("voList",list);
%>
<html>
<head>
<title>My JSP 'tag.jsp' starting page</title>
</head>
<body>
<h2 align="center">This is my JSP page:测试taglib.</h2>
<hr>
<h2>自定义迭代标签:</h2>
<table>
<tr><td>姓名</td><td>密码</td></tr>
<my:forEach collection="voList" id="uservo">
<tr>
<td><my:getProperty name="uservo" property="name"/></td>
<td><my:getProperty name="uservo" property="password"/></td>
</tr>
</my:forEach>
</table>
<hr>
<h2>表达式调用类的静态方法:</h2>
2+5=${my:add(2,5)}
</body>
</html>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有