JavaWeb—自定义标签练习
练习一:<max:num1="" num2=""/>
<petrelsky:maxTagDemo num1="23" num2="35">
练习二:定制一个带有一个属性的标签<xxx:readFile src="">用于输出指定文件的内容
<petrelsky:readFileTagDemo src="WEB-INF/haha.txt">
练习一:
创建MaxTag类并实现SimpleTag,添加num1、num2属性和setter方法,在doTag()方法中完成比较和输出。
package com.demo.tag; import javax.servlet.jsp.JspContext; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.JspFragment; import javax.servlet.jsp.tagext.JspTag; import javax.servlet.jsp.tagext.SimpleTag; import java.io.IOException; public class MaxTag implements SimpleTag { private String num1; private String num2; public void setNum1(String num1) { this.num1 = num1; } public void setNum2(String num2) { this.num2 = num2; } @Override public void doTag() throws JspException, IOException { int a = 0; int b = 0; JspWriter out = pageContext.getOut(); try { a = Integer.parseInt(num1); b = Integer.parseInt(num2); out.print(a > b ? a : b); }catch (Exception e){ out.print("输入的属性格式不正确!!!"); } } @Override public void setParent(JspTag jspTag) { } @Override public JspTag getParent() { return null; } private PageContext pageContext; @Override public void setJspContext(JspContext jspContext) { this.pageContext = (PageContext) jspContext; } @Override public void setJspBody(JspFragment jspFragment) { } }
在mytld.tld中进行标签的相关配置
<tag> <name>max</name> <tag-class>com.demo.tag.MaxTag</tag-class> <body-content>empty</body-content> <attribute> <name>num1</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>num2</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag>
在jsp页面引用标签,首先需要导入标签
<%@taglib prefix="yhs" uri="http://mycompany.com" %>
<yhs:max num1="${param.a}" num2="${param.b}"></yhs:max>
效果图:
通常情况下开发简单标签直接继承SimpleTagSupport就可以了,可以直接调用其对应的getter方法的到 对应API
public class SimpleTagSupport implements SimpleTag { private JspTag parentTag; private JspContext jspContext; private JspFragment jspBody; public void doTag() throws JspException, IOException { } public void setParent(JspTag parent) { this.parentTag = parent; } public JspTag getParent() { return this.parentTag; } public void setJspContext(JspContext pc) { this.jspContext = pc; } protected JspContext getJspContext() { return this.jspContext; } public void setJspBody(JspFragment jspBody) { this.jspBody = jspBody; } protected JspFragment getJspBody() { return this.jspBody; } }
练习二:
创建ReadFileTag类继承SimpleTagSupport类,重写doTag()方法,添加src属性
package com.demo.tag; import javax.servlet.jsp.JspException; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.SimpleTagSupport; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class ReadFileTag extends SimpleTagSupport { //相对于当前web应用的根路径额文件名 private String src; public void setSrc(String src) { this.src = src; } @Override public void doTag() throws JspException, IOException { PageContext pageContext = (PageContext) getJspContext(); InputStream is = pageContext.getServletContext().getResourceAsStream(src); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String str = null; while ((str = reader.readLine())!=null){ pageContext.getOut().write(str); pageContext.getOut().write("<br>"); } } }
在读取时标签必须转换为特定的编码符号
str = Pattern.compile("<").matcher(str).replaceAll("<");
str = Pattern.compile(">").matcher(str).replaceAll(">");
在mytld.tld中进行标签的相关配置
<tag> <name>readfile</name> <tag-class>com.demo.tag.ReadFileTag</tag-class> <body-content>empty</body-content> <attribute> <name>src</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag>
在jsp页面引用标签,首先需要导入标签
<%@taglib prefix="yhs" uri="http://mycompany.com" %>
<yhs:readfile src="/WEB-INF/note.txt"></yhs:readfile>
效果图: