JSP自定义标签案例分析

案例一:实现一个基本防盗链标签

1. 标签处理类

public class MyReferer extends BodyTagSupport { private String site; private String back; public String getSite() { return site; } public void setSite(String site) { this.site = site; } public String getBack() { return back; } public void setBack(String back) { this.back = back; } public int doEndTag() throws JspException { // 获取JSP上下文环境对象 PageContext pageContext = this.pageContext; // 获取到request对象 HttpServletRequest request = (HttpServletRequest)pageContext.getRequest(); // 判断 String header = request.getHeader("referer"); if(header != null && header.startsWith(getSite())){ // 执行后续的页面 return Tag.EVAL_PAGE; }else{ // 页面的重定向 HttpServletResponse response = (HttpServletResponse)pageContext.getResponse(); try { response.sendRedirect(getBack()); } catch (IOException e) { e.printStackTrace(); } // 不执行 return Tag.SKIP_PAGE; } } }

2. 描述文件

<?xml version="1.0" encoding="UTF-8"?> <taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee " version="2.1"> <!-- 2. 编写标签库描述文件 --> <tlib-version>1.0</tlib-version> <short-name>jnb</short-name> <tag> <name>referer</name> <tag-class>cn.itcast.custom.MyReferer</tag-class> <body-content>empty</body-content> <attribute> <name>site</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>back</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>

3. 引入和使用

<%@taglib uri="/WEB-INF/referer.tld" prefix="my"%> <my:referer site=http://localhost:8080/day11/list.jsp back="/day11/list.jsp"/>

JSP2.0自定义标签

---| SimpleTag 接口

定义了标签处理类的生命周期方法。doTag()

-----| SimpleTagSupport 类

全部实现了SimpleTag接口的方法,因此后面我们只需要继承并重写该类即可。

案例二:实现自己的if….else标签

目标:

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <c:choose> <c:when test="<%= 12>1 %>"> 大于 </c:when> <c:otherwise> 小于 </c:otherwise> </c:choose>

分析:

1. ChooseTag.java,必须定义一个标记字段属性

public class ChooseTag extends SimpleTagSupport { private boolean tag = true; public boolean isTag() { return tag; } public void setTag(boolean tag) { this.tag = tag; } // 遇到标签自动执行 public void doTag() throws JspException, IOException { // 获取标签体对象 JspFragment body = this.getJspBody(); // 执行标签体 body.invoke(null); super.doTag(); } }

2. WhenTag.java

public class WhenTag extends SimpleTagSupport { private boolean test; public boolean isTest() { return test; } public void setTest(boolean test) { this.test = test; } // 遇到标签自动执行 public void doTag() throws JspException, IOException { // 获取父元素 ChooseTag choose = (ChooseTag)this.getParent(); // 获取父元素的标记变量值 boolean parent = choose.isTag(); // 判断 if( parent && this.isTest() ){ // 执行标签体 JspFragment body = this.getJspBody(); body.invoke(null); } super.doTag(); } }

3. Otherwise.java

public class OtherwiseTag extends SimpleTagSupport { // 遇到标签自动执行 public void doTag() throws JspException, IOException { // 获取父元素 ChooseTag choose = (ChooseTag)this.getParent(); // 获取父元素的标记变量值 boolean parent = choose.isTag(); // 判断 if(parent){ // 执行标签体 JspFragment body = this.getJspBody(); body.invoke(null); } super.doTag(); } }

4. 描述文件

<?xml version="1.0" encoding="UTF-8"?> <taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee " version="2.1"> <!-- 2. 编写标签库描述文件 --> <tlib-version>1.0</tlib-version> <short-name>jnb</short-name> <tag> <name>choose</name> <tag-class>cn.itcast.tags.ChooseTag</tag-class> <body-content>scriptless</body-content>  JSP2.0方式 </tag> <tag> <name>when</name> <tag-class>cn.itcast.tags.WhenTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>test</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>otherwise</name> <tag-class>cn.itcast.tags.OtherwiseTag</tag-class> <body-content>scriptless</body-content> </tag> </taglib>

5. 引入和使用

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzjspx.html