Welcome

首页 / 网页编程 / JSP / JSP自定义标签案例分析

本文为大家介绍了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 http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"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 http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"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. 引入和使用

<%@taglib uri="/WEB-INF/ifelse.tld" prefix="jnb"%> <jnb:choose><jnb:when test="<%= 1>2 %>">小于</jnb:when><jnb:otherwise> 大于</jnb:otherwise> </jnb:choose> 
打包自定义标签库
1.   建立一个taglibs文件夹
2.   将所有的标签处理类对应的class文件连同包拷贝到1中的目录中
3.   在1中的文件夹中建立一个META-INF文件夹
4.   将tld文件拷贝到META-INF目录
5.   编辑tld文件引入uri元素:<uri>http://www.jnb.com</uri>     à提供引入的url路径
6.   使用jar命令进行打包:D:mytaglibs>jar cvf jnb.jar *
总结
主要掌握如何使用JSP2.0进行自定义标签的开发和打包。
1.   建立一个taglibs文件夹
2.   将所有的标签处理类对应的class文件连同包拷贝到1中的目录中
3.   在1中的文件夹中建立一个META-INF文件夹
4.   将tld文件拷贝到META-INF目录
5.   编辑tld文件引入uri元素:<uri>http://www.jnb.com</uri>     à提供引入的url路径
6.   使用jar命令进行打包:D:mytaglibs>jar cvf jnb.jar *
总结
主要掌握如何使用JSP2.0进行自定义标签的开发和打包。