自定义标签(TagSupport )
来源:互联网 发布:工作流数据库设计 编辑:程序博客网 时间:2024/06/01 09:58
定义最简单的标签
自定义标签采用Default Adapter模式(缺省适配模式)
Java代码
1 //最简单的标签 2 public class LangHuaTag extends TagSupport { 3 private long startTime; 4 private long endTime; 5 6 public int doStartTag() throws JspException { 7 startTime = System.currentTimeMillis(); 8 //表示定制标记里面有所包括的JSP页面 9 return TagSupport.EVAL_BODY_INCLUDE; 10 } 11 public int doEndTag() throws JspException { 12 endTime = System.currentTimeMillis(); 13 long elapsed = endTime - startTime; 14 try { 15 JspWriter out = pageContext.getOut(); 16 out.println("runtime is "+ elapsed); 17 } catch (IOException e) { 18 e.printStackTrace(); 19 } 20 //表示JSP页面继续运行 21 return TagSupport.EVAL_PAGE; 22 } 23 24 } 25 //代属性的标签 26 public class DateTag extends TagSupport { 27 private String pattern = "yyyy-MM-dd hh:mm:ss"; 28 private Date date; 29 //必须要有Set方法,因为是属性可以设值 30 public void setPattern(String pattern) { 31 this.pattern = pattern; 32 } 33 34 public void setDate(Date date) { 35 this.date = date; 36 } 37 38 public int doEndTag() throws JspException { 39 SimpleDateFormat sdf = new SimpleDateFormat(pattern); 40 //如果没有就是当前时间 41 if(date==null){ 42 date = new Date(); 43 } 44 JspWriter out = pageContext.getOut(); 45 try { 46 out.print(sdf.format(date)); 47 } catch (IOException e) { 48 e.printStackTrace(); 49 } 50 return TagSupport.EVAL_PAGE; 51 } 52 } 53 54 /** 55 * 循环输出 56 * @author Administrator 57 * 58 */ 59 public class LoopTag extends TagSupport { 60 private int times =0; 61 //Set方法设值 62 public void setTimes(int times) { 63 this.times = times; 64 } 65 66 public int doStartTag() throws JspException { 67 //表示定制标记里面有所包括的JSP页面 68 return TagSupport.EVAL_BODY_INCLUDE; 69 } 70 71 public int doAfterBody() throws JspException { 72 if(times>0){ 73 times--; 74 //表示双从标签开始输入 75 return TagSupport.EVAL_BODY_AGAIN; 76 } 77 //表示结束,忽略标签内部的内容 78 return TagSupport.SKIP_BODY; 79 } 80 81 }
配置文件
Xml代码
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <taglib xmlns="http://java.sun.com/xml/ns/j2ee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" 5 version="2.0"> 6 <tlib-version>1.0</tlib-version> 7 <short-name>util</short-name> 8 <uri>http://langhua.com/taglib/util</uri> 9 <tag> 10 <name>timer</name> 11 <tag-class>com.langhua.tagsupport.LangHuaTag</tag-class> 12 <body-content>JSP</body-content> 13 <!-- JSP,empty表示能能包函内容的,scriptless,tagdependent --> 14 </tag> 15 16 <tag> 17 <name>date</name> 18 <tag-class>com.langhua.tagsupport.DateTag</tag-class> 19 <body-content>empty</body-content> 20 <!-- JSP,empty表示不能包函内容的,scriptless,tagdependent --> 21 <attribute> 22 <!-- 标签名 --> 23 <name>time</name> 24 <!-- 是否为可选属性 --> 25 <required>false</required> 26 <!-- 是否接受JSP表达示计算结果 --> 27 <rtexprvalue>true</rtexprvalue> 28 </attribute> 29 <attribute> 30 <name>pattern</name> 31 <required>true</required> 32 <rtexprvalue>false</rtexprvalue> 33 </attribute> 34 </tag> 35 36 <tag> 37 <name>loop</name> 38 <tag-class>com.langhua.tagsupport.LoopTag</tag-class> 39 <body-content>JSP</body-content> 40 <!-- JSP,empty表示不能包函内容的,scriptless,tagdependent --> 41 <attribute> 42 <!-- 标签名 --> 43 <name>times</name> 44 <!-- 是否为可选属性 --> 45 <required>true</required> 46 <!-- 是否接受JSP表达示计算结果 --> 47 <rtexprvalue>true</rtexprvalue> 48 </attribute> 49 </tag> 50 </taglib>
JSP页面
Html代码
1 <%@ taglib prefix="util" uri="http://langhua.com/taglib/util"%> 2 3 <util:timer></util:timer> 4 <util:loop times="3"> 5 <util:date pattern="yyyy-MM-dd" /><br/> 6 </util:loop> 7 8 <%@ taglib prefix="util" uri="http://langhua.com/taglib/util"%> 9 10 <util:timer></util:timer> 11 <util:loop times="3"> 12 <util:date pattern="yyyy-MM-dd" /><br/> 13 </util:loop>
TagSupport的流程图
SKIP_BODY 表示不用处理标签体,直接调用doEndTag()方法。
SKIP_PAGE 忽略标签后面的JSP内容。
EVAL_PAGE 处理标签后,继续处理JSP后面的内容。
EVAL_BODY_BUFFERED 表示需要处理标签体。
EVAL_BODY_INCLUDE 表示需要处理标签体,但绕过setBodyContent()和doInitBody()方法
EVAL_BODY_AGAIN 对标签体循环处理。
上面转自http://zhuhuide2004.iteye.com/blog/555737
下面是项目中的应用。
在做权限的时候,不同角色用户看到不同的权限,比如:一个页面上的添加按钮,有的角色看的到,有的角色看不到,必须判断角色是否有这个权限。
1 public class MenuTLD extends TagSupport { 2 3 4 private String menuStr; 5 6 @Override 7 public int doStartTag() throws JspException { 8 // TODO Auto-generated method stub 9 HttpSession session = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession();10 List<SysMenuBean> userMenus = (List<SysMenuBean>) session.getAttribute("sysMenuBeans");11 for (SysMenuBean sysMenuBean : userMenus) {12 try {13 if(sysMenuBean.getUrl()!=null&&sysMenuBean.getUrl().contains(menuStr))14 {15 16 return TagSupport.EVAL_BODY_INCLUDE;17 }18 } catch (Exception e) {19 // TODO Auto-generated catch block20 return TagSupport.SKIP_BODY; 21 } 22 }23 return TagSupport.SKIP_BODY; 24 }25 26 public String getMenuStr() {27 return menuStr;28 }29 30 public void setMenuStr(String menuStr) {31 this.menuStr = menuStr;32 }33 34 }
其内容是:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"><taglib> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>menu</short-name> <uri>http://www.dsdl.com/dev/jsp/menu</uri> <description>DSDL menu 1.1 core library</description> <tag> <name>menu</name> <tag-class>com.dsdl.common.menutld.MenuTLD</tag-class> <body-content>JSP</body-content> <description>menu</description> <attribute> <name>menuStr</name> <required>false</required> <rtexprvalue>false</rtexprvalue> <type>type</type> <description>MenuId</description> </attribute> </tag></taglib>
在页面中的应用:
阅读全文
0 0
- 自定义标签(TagSupport)!!!
- 自定义标签(TagSupport )
- 自定义标签(TagSupport )
- TagSupport 自定义权限标签
- jsp自定义标签 TagSupport
- 使用TagSupport自定义标签
- 自定义标签TagSupport
- 自定义标签 嵌套标签 TagSupport
- 借助TagSupport 实现自定义标签
- 借助TagSupport 实现自定义标签
- 借助TagSupport 实现自定义标签
- 借助TagSupport 实现自定义标签
- 自定义JSP标签(TagSupport类的生命周期)
- 自定义JSP标签(TagSupport类的生命周期)
- 最简单的自定义标签 (TagSupport)
- (二)创建自定义标签TagSupport类步骤
- 自定义标签中tagsupport的一些方法
- 自定义JSP标签:TagSupport类的生命周期
- latex首次使用遇到的问题
- 实验三:计数器、波形仿真、SignalTap
- “信用卡”&“信用贷”,别再傻傻分不清了~
- linux远程传文件scp和sftp详解
- oracle 保留小数点前面的0的方法
- 自定义标签(TagSupport )
- iOS开发之主题皮肤
- 线性回归与梯度下降(linear regression and gradient descent)
- 48. Rotate Image
- node.js 全局对象及全局变量
- white-space、word-wrap、word-break 字母 数字不换行问题
- org.springframework.http.converter.json.MappingJacksonHttpMessageConverter
- WebSocket部署到服务器时,出现连接失败的问题解决与分析
- JAVA多线程系列--ThreadLocal详解