




已阅读5页,还剩12页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
自定义标签package javax.servlet.jsp.tagext interface classJspTag SimpleTag接口SimpleTagSupport(实现doTag()) | Tag | IterationTag TagSupport | | | | BodyTag BodyTagSupport生命周期实例化标签程序设置页面上下文setPageContext()初始化属性setXXX()doStartTag() 根据返回值确定下一步执行的方法 1return EVAL_BODY_INCLUDE 则执行setBodyContent()doAtferBody()然后又根据doAtferBody()的返回值确定下一步 return EVAL_BODY_AGAIN 则再次执行doAtferBody()return SKIP_BODY则执行 doEndTag() 2 return SKIP_BODY 则直接执行doEndTag() doEndTag() return EVAL_PAGE 执行页面剩余部分 return SKIP_PAGE 跳过页面剩余部分 release() 释放标签实例(由容器决定)实现步骤:1. 编写标签处理类 这个类实现方式有多种,可以采用实现接口或继承父类的方式 一般采用继承的方式 如果你的标签体里有内容并且想处理这些内容则需要继承BodyTagSupport 否则可以继承TagSupport需要注意:如果你标签有属性 则类里需要有相应的setXXX()无主体内容且无属性的标签处理类 public class DateTag extends TagSupport private static final long serialVersionUID = -343055242785573672L;private String dateFormat=yyyy年MM月dd日 HH:mm:ss E;public String getDateFormat() return dateFormat;public void setDateFormat(String dateFormat) this.dateFormat = dateFormat;Overridepublic int doEndTag() throws JspException DateFormat format=new SimpleDateFormat(dateFormat);try pageContext.getOut().println(format.format(new Date(); catch (IOException e) e.printStackTrace();System.out.println(format.format(new Date();/返回EVAL_PAGE表示会执行jsp页面的剩余部分return EVAL_PAGE;无主体内容有属性的标签处理类public class DeptOptionsTag extends TagSupport private static final long serialVersionUID = -4674864242155143002L;private int selectValue;public int getSelectValue() return selectValue;public void setSelectValue(int selectValue) this.selectValue = selectValue;Overridepublic int doEndTag() throws JspException JspWriter out = pageContext.getOut();EmpDAO dao = new EmpDAO();List list = dao.getAllDept();try for (Dept d : list) if (selectValue = d.getDeptid() out.println(+ d.getDeptName() + ); else out.println(+ d.getDeptName() + ); catch (IOException e) e.printStackTrace();return EVAL_PAGE;有主体内容有属性的标签处理类public class LoopTag extends BodyTagSupport private static final long serialVersionUID = 6800680657186732025L;private int count;public void setCount(int count) System.out.println(setCount);this.count = count;public int doStartTag() throws JspException System.out.println(doStartTag();return super.doStartTag();public int doAfterBody() throws JspException System.out.println(doAfterBody();count-;if(count0)return BodyTag.EVAL_BODY_AGAIN;elsereturn BodyTag.SKIP_BODY;public int doEndTag() throws JspException System.out.println(doEndTag();try bodyContent.writeOut(bodyContent.getEnclosingWriter(); catch (IOException e) e.printStackTrace();return EVAL_PAGE;public void release() System.out.println(release();super.release();public void setBodyContent(BodyContent b) System.out.println(setBodyContent();super.setBodyContent(b);public void setPageContext(PageContext pageContext) System.out.println(setPageContext();super.setPageContext(pageContext);嵌套标签public class ParentTag extends BodyTagSupport private static final long serialVersionUID = 2955495958451151992L;private boolean value;public void setValue(boolean value) this.value = value;public int doStartTag() throws JspException if(value)return EVAL_BODY_INCLUDE;elsereturn SKIP_BODY;public int doEndTag() throws JspException return super.doEndTag();public class SubTag extends BodyTagSupport private static final long serialVersionUID = -1791896846943415063L;private String color;public void setColor(String color) this.color = color;public int doEndTag() throws JspException JspWriter out = pageContext.getOut();String str = bodyContent.getString();try if (!.equals(str) out.println( + str+ ); else out.println( + str + ); catch (IOException e) e.printStackTrace();return super.doEndTag();2. 编写tld标签部署文档 指定标签名称等信息 一般放在WEB-INF 目录下1.1dateTagcom.px.tag.DateTagemptyoptionsTagcom.px.tag.DeptOptionsTagempty selectValuefalsetrueloopcom.px.tag.LoopTagJSPcounttruetrue parentcom.px.tag.ParentTagJSPvaluetruetruesubcom.px.tag.SubTagJSPcolortruetrue3。jsp使用标签 1通过导入 通过前缀:标签名引用标签 2也可以在web.xml中配置:/MyTag/WEB-INF/tld/mytags.tld 然后在页面taglib指令uri只需指定为 /MyTag 就可以了标签中的重要对象 bodyContent对象 bodyContent.getString() 获取标签体中的内容bodyContent.getEnclosingWriter() 获取javax.servlet.jsp.JspWriter对象 bodyContent.writeOut(javax.servlet.jsp.JspW
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年靶点发现与验证技术在创新药物研发中的生物信息学伦理问题报告
- 门店出售协议合同书模板
- 高效的对赌协议合同模板
- 机械加工厂劳务合同范本
- 签订合同后主体变更协议
- 精装修房子购买合同范本
- 甲方如何写合同协议模板
- 村委与贫困户养殖协议书
- 签了认购协议被动签合同
- 电源安装工程合同协议书
- 2025年秋数学(新)人教版三年级上课件:第1课时 观察物体
- 湖北宜昌高新区社区专职人员招聘笔试真题2024
- 德勤:2025“十五五”时期中国能源行业关键议题报告
- 挖掘机安全操作规程完整版
- 2024年中国高纯铂族金属行业调查报告
- 影视项目可行性研究报告
- 2025辅警招聘公安基础知识考试题库及答案
- ETX12.0.4安装配置手册
- 2025年广东省中考数学试卷真题(含答案详解)
- DeepSeek在教育和学术领域的应用场景与案例(上中下合集)
- 第10课+影响世界的工业革命+课件-2024-2025学年高一下学期统编版(2019)必修中外历史纲要下
评论
0/150
提交评论