博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java jsp自定义标签
阅读量:4915 次
发布时间:2019-06-11

本文共 15334 字,大约阅读时间需要 51 分钟。

1、web.xml配置

1 
2
6
test_Servlet
7
8
9
kongxh_tag
10
/WEB-INF/mytag.tld
11
12
13
14
15
index.jsp
16
17

2、tld的编写

1 
2
5
1.0
6
2.0
7
firsttags
8
9
IterTag
10
test_servlet_package.tag.IterTag
11
jsp
12
13
scope
14
true
15
true
16
17
18
id
19
true
20
true
21
22
23
name
24
true
25
true
26
27
28
29
MyBodyTag
30
test_servlet_package.tag.MyBodyTag
31
test_servlet_package.tag.MyTagExtraInfo
32
jsp
33
34
scope
35
true
36
true
37
38
39
id
40
true
41
true
42
43
44
name
45
true
46
true
47
48
49
50
MySimpleTag
51
test_servlet_package.tag.MySimpleTag
52
empty
53
54
format
55
true
56
true
57
58
59
60
MySimpleTagForIter
61
test_servlet_package.tag.MySimpleTagForIter
62
scriptless
63
64
scope
65
true
66
true
67
68
69
id
70
true
71
true
72
73
74
name
75
true
76
true
77
78
79
80
DynamicAttributesTag
81
test_servlet_package.tag.DynamicAttributesTag
82
empty
83
true
84
85
View Code

3、Tag的编写

IterTag:循环标签

1 package test_servlet_package.tag; 2 import java.util.Iterator; 3 import java.util.List; 4 import javax.servlet.jsp.JspException; 5 import javax.servlet.jsp.PageContext; 6 import javax.servlet.jsp.tagext.TagSupport; 7 public class IterTag extends TagSupport { 8     /** 9      * jsp 1.2 之前 的 做法 10      */11     private static final long serialVersionUID = 1L;12     //接受属性名称13     private String name;14     //接受查找范围15     private String scope;16     private String id;17     private Iterator
iter = null;18 @Override19 public int doStartTag() throws JspException {20 Object value = null;21 //是否是page范围22 if ("page".equals(this.scope)) {23 value = super.pageContext24 .getAttribute(name, PageContext.PAGE_SCOPE);25 } else if ("request".equals(this.scope)) {26 //是否是request范围27 value = super.pageContext.getAttribute(name,28 PageContext.REQUEST_SCOPE);29 } else if ("session".equals(this.scope)) {30 //是否是session范围31 value = super.pageContext.getAttribute(name,32 PageContext.SESSION_SCOPE);33 } else {34 //是否是application范围35 value = super.pageContext.getAttribute(name,36 PageContext.APPLICATION_SCOPE);37 }38 //如果没有查到属性39 if (value !=null && value instanceof List
) {40 this.iter = ((List
)value).iterator();41 if(iter.hasNext()){42 super.pageContext.setAttribute(this.id,iter.next());43 //找到属性,执行标签题内容44 return TagSupport.EVAL_BODY_INCLUDE;45 }else{46 //找到属性,执行标签题内容47 return TagSupport.SKIP_BODY;48 }49 }else{50 //不执行标签体内容51 return TagSupport.SKIP_BODY;52 }53 }54 public String getName() {55 return name;56 }57 58 @Override59 public int doAfterBody() throws JspException {60 if(iter.hasNext()){61 super.pageContext.setAttribute(this.id,iter.next());62 return TagSupport.EVAL_BODY_AGAIN; //反复执行63 }else{64 return TagSupport.SKIP_BODY;65 }66 }67 @Override68 public int doEndTag() throws JspException {69 return super.doEndTag();70 }71 public void setName(String name) {72 this.name = name;73 }74 public String getScope() {75 return scope;76 }77 public void setScope(String scope) {78 this.scope = scope;79 }80 public String getId() {81 return id;82 }83 public void setId(String id) {84 this.id = id;85 }86 }
View Code

MyBodyTag

1 package test_servlet_package.tag; 2 import java.io.IOException; 3 import java.util.Iterator; 4 import java.util.List; 5 import javax.servlet.jsp.JspException; 6 import javax.servlet.jsp.PageContext; 7 import javax.servlet.jsp.tagext.BodyTagSupport; 8 public class MyBodyTag extends BodyTagSupport{ 9     private static final long serialVersionUID = 1L;10     private String name; //接受属性名称11     private String scope; //接受查找范围12     private String id;13     private Iterator
iter = null;14 @Override15 public int doStartTag() throws JspException {16 Object value = null;17 if ("page".equals(this.scope)) {18 value = super.pageContext.getAttribute(name, PageContext.PAGE_SCOPE); //是否是page范围19 } else if ("request".equals(this.scope)) { 20 value = super.pageContext.getAttribute(name,PageContext.REQUEST_SCOPE); //是否是request范围21 } else if ("session".equals(this.scope)) {22 value = super.pageContext.getAttribute(name,PageContext.SESSION_SCOPE); //是否是session范围23 } else {24 value = super.pageContext.getAttribute(name,PageContext.APPLICATION_SCOPE); //是否是application范围25 }26 if (value !=null && value instanceof List
) { //如果没有查到属性27 this.iter = ((List
)value).iterator();28 if(iter.hasNext()){29 super.pageContext.setAttribute(this.id,iter.next());30 return BodyTagSupport.EVAL_BODY_BUFFERED; //找到属性,执行标签题内容31 }else{32 return BodyTagSupport.SKIP_BODY; //找到属性,执行标签题内容33 }34 }else{35 return BodyTagSupport.SKIP_BODY; //不执行标签体内容36 }37 }38 @Override39 public int doAfterBody() throws JspException {40 if(iter.hasNext()){41 super.pageContext.setAttribute(this.id,iter.next());42 return BodyTagSupport.EVAL_BODY_AGAIN; //反复执行43 }else{44 return BodyTagSupport.SKIP_BODY;45 }46 }47 @Override48 public int doEndTag() throws JspException { //这个 方法 不执行 则 没有 输出 49 if(super.bodyContent != null){50 try {51 super.bodyContent.writeOut(super.getPreviousOut());52 } catch (IOException e) {53 e.printStackTrace();54 }55 }56 return BodyTagSupport.EVAL_PAGE;57 }58 public String getName() {59 return name;60 }61 public void setName(String name) {62 this.name = name;63 }64 public String getScope() {65 return scope;66 }67 public void setScope(String scope) {68 this.scope = scope;69 }70 public String getId() {71 return id;72 }73 public void setId(String id) {74 this.id = id;75 }76 }
View Code
1 package test_servlet_package.tag; 2 import javax.servlet.jsp.tagext.TagData; 3 import javax.servlet.jsp.tagext.TagExtraInfo; 4 import javax.servlet.jsp.tagext.VariableInfo; 5 public class MyTagExtraInfo extends TagExtraInfo{ 6     @Override 7     public VariableInfo[] getVariableInfo(TagData data) { 8         // TODO Auto-generated method stub 9         return new  VariableInfo[]{ new VariableInfo(data.getId(),"java.lang.String",true,VariableInfo.NESTED)};10     }11 }
View Code

MySimpleTag

1 package test_servlet_package.tag; 2 import java.io.IOException; 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 import javax.servlet.jsp.JspException; 6 import javax.servlet.jsp.tagext.SimpleTagSupport; 7 public class MySimpleTag extends SimpleTagSupport{ 8     //jsp 2.0 之后 的 出现  为了 简化 开发  9     private String format;10     @Override11     public void doTag() throws JspException, IOException {12         SimpleDateFormat sdf = new SimpleDateFormat(this.format);13         this.getJspContext().getOut().write(sdf.format(new Date()));14     }15     public String getFormat() {16         return format;17     }18     public void setFormat(String format) {19         this.format = format;20     }21 }
View Code

MySimpleTagForIter

1 package test_servlet_package.tag; 2 import java.io.IOException; 3 import java.io.StringWriter; 4 import java.util.Iterator; 5 import java.util.List; 6 import javax.servlet.jsp.JspException; 7 import javax.servlet.jsp.PageContext; 8 import javax.servlet.jsp.tagext.JspFragment; 9 import javax.servlet.jsp.tagext.SimpleTagSupport;10 public class MySimpleTagForIter extends SimpleTagSupport{11     //jsp 2.0 之后 的 出现  为了 简化 开发 12     private String name; //接受属性名称13     private String scope; //接受查找范围14     private String id;15     @Override16     public void doTag() throws JspException, IOException {17         Object value = null;18         if ("page".equals(this.scope)) {19             value = super.getJspContext().getAttribute(name, PageContext.PAGE_SCOPE); //是否是page范围20         } else if ("request".equals(this.scope)) {21             value = super.getJspContext().getAttribute(name,PageContext.REQUEST_SCOPE);//是否是request范围22         } else if ("session".equals(this.scope)) {23             value = super.getJspContext().getAttribute(name,PageContext.SESSION_SCOPE); //是否是session范围24         } else {25             value = super.getJspContext().getAttribute(name,PageContext.APPLICATION_SCOPE); //是否是application范围26         }27         if(value !=null && value instanceof List
){28 Iterator iter = ((List
)value).iterator();29 while(iter.hasNext()){30 super.getJspContext().setAttribute(this.id,iter.next());31 //PageContext pageContext = (PageContext) super.getJspBody().getJspContext();32 //super.getJspBody().invoke(pageContext.getOut());33 //super.getJspBody().invoke(null);//输出到相应里去 ,如果 是 个 流 文件 ,这会输出 到 文件 里去 ,,,34 JspFragment jf = this.getJspBody();35 StringWriter sw = new StringWriter();36 jf.invoke(sw);37 String s = sw.toString();38 s = "zczx";39 this.getJspContext().getOut().write(s); 40 }41 }42 }43 public String getName() {44 return name;45 }46 public void setName(String name) {47 this.name = name;48 }49 public String getScope() {50 return scope;51 }52 public void setScope(String scope) {53 this.scope = scope;54 }55 public String getId() {56 return id;57 }58 public void setId(String id) {59 this.id = id;60 }61 }
View Code

DynamicAttributesTag

1 package test_servlet_package.tag; 2 import java.io.IOException; 3 import java.util.HashMap; 4 import java.util.Iterator; 5 import java.util.Map; 6 import javax.servlet.jsp.JspException; 7 import javax.servlet.jsp.tagext.DynamicAttributes; 8 import javax.servlet.jsp.tagext.SimpleTagSupport; 9 10 public class DynamicAttributesTag extends SimpleTagSupport implements DynamicAttributes {11     // jsp 2.0 之后 的 出现 为了 简化 开发12     private Map
map = new HashMap
();13 @Override14 public void doTag() throws JspException, IOException {15 Float num = 0.0f;16 Iterator
> iter = map.entrySet().iterator();17 while (iter.hasNext()) {18 Map.Entry
val = iter.next();19 num += val.getValue();20 }21 this.getJspContext().getOut().write(num + "");22 }23 24 @Override25 public void setDynamicAttribute(String arg0, String arg1, Object arg2) throws JspException {26 map.put(arg1, Float.parseFloat(arg2.toString()));27 }28 }
View Code

测试

1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@ page import="java.util.*"%> 3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4 <%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml"%> 5 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> 6 <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%> 7 <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> 8 <%@ taglib uri="kongxh_tag" prefix="mytag"%> 9 10 11     index.jsp12 13 14         <%15             List
all = new ArrayList
();16 all.add("test 1,");17 all.add("test 2,");18 all.add("test 3,");19 request.setAttribute("all", all);20 %>21 22
23 ${url}24
25
26 ----------------------27
28 ${url}\ <%=url.length()%>29
30
31 ----------------------32
33
34 ----------------------35
36 ${url}37
38
39 ----------------------40
41
42 ----------------------43
44 45
View Code

总结:复习和记录一下之前用到一个知识点。

转载于:https://www.cnblogs.com/kongxianghao/p/6961993.html

你可能感兴趣的文章
编写弹窗 并居中
查看>>
XML Helper XML操作类
查看>>
2、iptables基本应用
查看>>
程序员成长过程
查看>>
项目实战02:nginx 反向代理负载均衡、动静分离和缓存的实现
查看>>
BZOJ3139/BZOJ1306 HNOI2013比赛/CQOI2009循环赛(搜索)
查看>>
C语言反汇编入门实例
查看>>
tab标签页
查看>>
IDEA快捷键
查看>>
冻结页面
查看>>
Scala-数组
查看>>
【计算机视觉】opencv读取多个摄像头
查看>>
【VS开发】MP4与H.264
查看>>
【Qt开发】V4L2 API详解 Camera详细设置
查看>>
谷歌弃用图片验证码,从此告别奇葩验证码
查看>>
JavaScript学习-require的用法
查看>>
libevent源码分析:epoll后端实现
查看>>
iOS和Android的app界面设计规范(转)
查看>>
【转】关于zend studio 9的4空格替换tab
查看>>
Apache Maven 入门篇(下)
查看>>