热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

第一次写自定义标签

写了近两个小时,终于搞定了四个标签,暂时告一段落仿造jstl写的四个标签第一个helloworld标签packagecom.my.web.tagli

写了近两个小时,终于搞定了四个标签,暂时告一段落

仿造jstl写的四个标签


第一个helloworld标签

package com.my.web.taglib;import java.io.IOException;import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;public class HelloWorldTag extends TagSupport {@Overridepublic int doEndTag() throws JspException {return super.EVAL_PAGE;}@Overridepublic int doStartTag() throws JspException {JspWriter out = super.pageContext.getOut();try {out.println("


第二个forEach标签

package com.my.web.taglib;import java.util.List;import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;public class ForEachTag extends TagSupport {private Object items;private String var;private String varStatus;private int step = 1;private int index = 0;private List list;@Overridepublic int doAfterBody() throws JspException {System.out.println("------------------");if(index


第三个if标签

package com.my.web.taglib;import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;public class IfTag extends TagSupport {private boolean test;private String var;private String scope = "page";@Overridepublic int doEndTag() throws JspException {return super.EVAL_PAGE;}@Overridepublic int doStartTag() throws JspException {if(var!=null&&!var.equals("")){if(scope.equals("page")){super.pageContext.setAttribute(var, test);}else if(scope.equals("session")){super.pageContext.getSession().setAttribute(var, test);}else if(scope.equals("request")){super.pageContext.getRequest().setAttribute(var, test);}else if(scope.equals("application")||scope.equals("ServletContext")){super.pageContext.getServletContext().setAttribute(var, test);}else if(scope==null||scope.equals("")){super.pageContext.setAttribute(var, test);}}if(test){return super.EVAL_BODY_INCLUDE;}else{return super.SKIP_BODY;}}public boolean isTest() {return test;}public void setTest(boolean test) {this.test = test;}public String getVar() {return var;}public void setVar(String var) {this.var = var;}public String getScope() {return scope;}public void setScope(String scope) {this.scope = scope;}}




第四个time标签

package com.my.web.taglib;import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;public class TimeTag extends TagSupport {private String format="yyyy-MM-dd";@Overridepublic int doEndTag() throws JspException {return super.EVAL_PAGE;}@Overridepublic int doStartTag() throws JspException {DateFormat df = new SimpleDateFormat(format);JspWriter out = super.pageContext.getOut();try {out.println(df.format(new Date()));out.flush();} catch (IOException e) {e.printStackTrace();}return super.SKIP_BODY;}public String getFormat() {return format;}public void setFormat(String format) {this.format = format;}}



推荐阅读
author-avatar
北京家圆医院999
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有