定义有标签体的标签库

来源:互联网 发布:myeclipse build js 编辑:程序博客网 时间:2024/05/01 05:01

实例

标签处理类----AttributeTag.java

package org.tagdemo ;import java.io.* ;import javax.servlet.jsp.* ;import javax.servlet.jsp.tagext.* ;public class AttributeTag extends TagSupport {private String name ;// 接收属性的名称private String scope ;// 接收属性的范围public int doStartTag()               throws JspException{// 是判断属性是否存在Object value = null ;if("page".equals(this.scope)){// 是否是page范围value = super.pageContext.getAttribute(this.name,PageContext.PAGE_SCOPE) ;}if("request".equals(this.scope)){// 是否是request范围value = super.pageContext.getAttribute(this.name,PageContext.REQUEST_SCOPE) ;}if("session".equals(this.scope)){// 是否是session范围value = super.pageContext.getAttribute(this.name,PageContext.SESSION_SCOPE) ;}if("application".equals(this.scope)){// 是否是request范围value = super.pageContext.getAttribute(this.name,PageContext.APPLICATION_SCOPE) ;}if(value == null){// 表示现在根本就没有此属性return TagSupport.SKIP_BODY ;// 没有属性不执行标签体} else {return TagSupport.EVAL_BODY_INCLUDE ;// 执行标签体}}public void setName(String name){this.name = name ;}public void setScope(String scope){this.scope = scope ;}public String getName(){return this.name ;}public String getScope(){return this.scope ;}}
定义标签描述文件----/WEB-INF/tag.tld

<?xml version="1.0" encoding="UTF-8"?><taglib xmlns="http://java.sun.com/xml/ns/j2ee"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_1.xsd"    version="2.1">    <tlib-version>1.0</tlib-version>    <short-name>mldntag</short-name>    <tag><name>present</name><tag-class>org.tagdemo.AttributeTag</tag-class><body-content>JSP</body-content><attribute><name>name</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute><attribute><name>scope</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute>    </tag></taglib>
配置web.xml,设置映射名称

<jsp-config><taglib><taglib-uri>mldn_date</taglib-uri><taglib-location>/WEB-INF/datetag.tld</taglib-location></taglib><taglib><taglib-uri>mldn</taglib-uri><taglib-location>/WEB-INF/tag.tld</taglib-location></taglib></jsp-config>
调用标签,完成判断----presenttag.jsp

<%@ page contentType="text/html" pageEncoding="GBK"%><%@ taglib prefix="mytag" uri="mldn"%><html><head><title></title></head><body><%String scope = "session" ;// 假设现在判断的是session范围session.setAttribute("username","李兴华") ;%><mytag:present name="username" scope="<%=scope%>"><h2><%=scope%>范围存在属性,内容是:“${sessionScope.username}”</h2></mytag:present><mytag:present name="allusers" scope="request"><h2>request范围存在属性,内容是:“${requestScope.allusers}”</h2></mytag:present></body></html>

开发迭代标签

开发迭代标签处理类----IterateTag.java

package org.tagdemo ;import java.util.* ;import javax.servlet.jsp.* ;import javax.servlet.jsp.tagext.* ;public class IterateTag extends TagSupport {private String name ;private String scope ;private String id ;// 这个id用于保存集合中的每一个元素private Iterator<?> iter = null ;public int doStartTag()               throws JspException{Object value = null ;if("page".equals(this.scope)){value = super.pageContext.getAttribute(this.name,PageContext.PAGE_SCOPE) ;}if("request".equals(this.scope)){value = super.pageContext.getAttribute(this.name,PageContext.REQUEST_SCOPE) ;}if("session".equals(this.scope)){value = super.pageContext.getAttribute(this.name,PageContext.SESSION_SCOPE) ;}if("application".equals(this.scope)){value = super.pageContext.getAttribute(this.name,PageContext.APPLICATION_SCOPE) ;}if(value!=null && value instanceof List<?>){this.iter = ((List<?>)value).iterator() ;if(iter.hasNext()){// 将属性保存在page属性范围之中super.pageContext.setAttribute(this.id,iter.next()) ;return TagSupport.EVAL_BODY_INCLUDE ;} else {return TagSupport.SKIP_BODY ;}} else {return TagSupport.SKIP_BODY ;}}public int doAfterBody()                throws JspException{if(iter.hasNext()){// 将属性保存在page属性范围之中super.pageContext.setAttribute(this.id,iter.next()) ;return TagSupport.EVAL_BODY_AGAIN ;// 反复执行doAfterBody()方法} else {return TagSupport.SKIP_BODY ;}}public void setName(String name){this.name = name ;}public void setScope(String scope){this.scope = scope ;}public void setId(String id){this.id = id ;}public String getName(){return this.name ;}public String getScope(){return this.scope ;}public String getId(){return this.id ;}}
修改标签描述文件,增加迭代标签配置----mldntag.tld

<tag><name>present</name><tag-class>org.lxh.tagdemo.AttributeTag</tag-class><body-content>JSP</body-content><attribute><name>name</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute><attribute><name>scope</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute>    </tag>    <tag><name>iterate</name><tag-class>org.lxh.tagdemo.IterateTag</tag-class><body-content>JSP</body-content><attribute><name>name</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute><attribute><name>scope</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute><attribute><name>id</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute>    </tag>
编写JSP执行标签---iteratetag.jsp

<%@ page contentType="text/html" pageEncoding="GBK"%><%@ page import="java.util.*"%><%@ taglib prefix="mytag" uri="mldn"%><html><head><title></title></head><body><%// 此代码只是为了测试,实际中此部分应该由servlet传递List<String> all = new ArrayList<String>() ;all.add("www.baidu.cn") ;all.add("www.google.cn") ;all.add("www.CSDN.com") ;request.setAttribute("all",all) ;// 将内容保存在标签执行%><mytag:present name="all" scope="request"><mytag:iterate id="url" name="all" scope="request"><h3>网站:${url}</h3></mytag:iterate></mytag:present></body></html>
-----------------------------------------------------------------------------------------------------------------

简单标签实现迭代

定义迭代标签处理类----SimpleIterateTag.java

package org.lxh.tagdemo ;import java.io.* ;import java.util.* ;import java.text.* ;import javax.servlet.jsp.* ;import javax.servlet.jsp.tagext.* ;public class SimpleIterateTag extends SimpleTagSupport {private String id ;private String name ;private String scope ;public void doTag()           throws JspException,                  IOException{Object value = null ;if("page".equals(this.scope)){value = super.getJspContext().getAttribute(this.name,PageContext.PAGE_SCOPE) ;}if("request".equals(this.scope)){value = super.getJspContext().getAttribute(this.name,PageContext.REQUEST_SCOPE) ;}if("session".equals(this.scope)){value = super.getJspContext().getAttribute(this.name,PageContext.SESSION_SCOPE) ;}if("application".equals(this.scope)){value = super.getJspContext().getAttribute(this.name,PageContext.APPLICATION_SCOPE) ;}if(value != null && value instanceof List<?>){Iterator<?> iter = ((List<?>) value).iterator() ;while(iter.hasNext()){super.getJspContext().setAttribute(id,iter.next()) ;super.getJspBody().invoke(null) ;}}}public void setId(String id){this.id = id ;}public void setName(String name){this.name = name ;}public void setScope(String scope){this.scope = scope ;}public String getId(){return this.id ;}public String getName(){return this.name ;}public String getScope(){return this.scope ;}}
修改标签描述文件,增加新的标签配置----mldntag.tld

<tag><name>simpleiterate</name><tag-class>org.lxh.tagdemo.SimpleIterateTag</tag-class><body-content>scriptless</body-content><attribute><name>name</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute><attribute><name>scope</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute><attribute><name>id</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute>    </tag>
在JSP中使用标签----simpleiteratetag.jsp

<%@ page contentType="text/html" pageEncoding="GBK"%><%@ page import="java.util.*"%><%@ taglib prefix="mytag" uri="mldn"%><html><head><title></title></head><body><%List<String> all = new ArrayList<String>() ;<pre code_snippet_id="415000" snippet_file_name="blog_20140703_7_3493509" name="code" class="java">        all.add("www.baidu.cn") ;        all.add("www.google.cn") ;        all.add("www.CSDN.com") ;
request.setAttribute("all",all) ;%><h1><mytag:simpleiterate id="url" name="all" scope="request"><h2>网站:${url}</h2></mytag:simpleiterate></h1></body></html>










0 0