作者:北京家圆医院999 | 来源:互联网 | 2023-09-25 15:05
写了近两个小时,终于搞定了四个标签,暂时告一段落
仿造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;}}