07---jsp标签编程06(简单标签)

来源:互联网 发布:程序员学什么专业 编辑:程序博客网 时间:2024/06/07 00:21

简单标签:
 在jsp1.2之前如果要是想进行标签库的开发,要么选择继承TagSupport类,
 要么就是继承BodyTagSupport类,而且还要去覆写里面的doStartTag()、 
 doEndTag()、doAfterBody(),而且还有非常清楚这些方法的返回值类型,例如
 SKIP_BODY,EVAL_BODY_INCLUDE等,这对于用户开发标签库来说很麻烦,所以到
 jsp2.0之后,为简化标签开发的复杂度,专门增加了一个制作简单标签库的
 SimpleTagSupport类,直接覆写里面的doTag()方法即可完成;
 
 SimpleTagSupport类的定义:
  public class SimpleTagSupport extends Object implements SimpleTag;

SimpleTagSupport类的主要方法:
 public void doTag() throws JspException  完成具体标签功能的编写
 public JspContext getJspContext()  取得JSP上下文,主要是用于输出;
 protected JspFragment getJspBody()  取得JspFragment对象,主要用于迭代输

出;

直接使用简单标签开发有个格式化日期的标签:
  package tag.lid.simpledatetag;
  import java.util.*;
  import java.text.*;
  import java.io.*;
  import javax.servlet.jsp.*;
  import javax.servlet.jsp.tagext.*;
  public class SimpleDateTag extends SimpleTagSupport{
   private String format;
   
   public void doTag() throws JspException,IOException{
    SimpleDateFormat sdf=new SimpleDateFormat(this.format);
    try{
     super.getJspContext().getOut().write(sdf.format(new

Date()));
     }
    catch(Exception e){
     }
    }
   public void setFormat(String format){
    this.format=format;
    }
   public String getFormat(){
     return this.format;
    }
   }
 编译上面的程序;
在标签描述文件中进行标签的描述:
  <tag>
   <name>simpledate</name> 
   <tag-class>
    tag.lid.simpledatetag.SimpleDateTag
   </tag-class>  
   <body-content>empty</body-content>
   <attribute>
    <name>format</name>
    <required>true</required>
    <rtexprvalue>true</rtexprvalue>
   </attribute>   
  </tag>
web.xml中:
  <taglib>
       <taglib-uri>lid</taglib-uri>
       <taglib-location>/WEB-INF/lidtag.tld</taglib-location>
      </taglib>

创建一个simpledatetag.jsp
 <%@ page contentType="text/html" pageEncoding="gbk"%>
 <%@ taglib prefix="mytag" uri="lid" %>
 <html>
 <head><title>这是测试</title></head>
 <body>
  <mytag:simpledate format="yyyy-MM-dd HH:mm:ss.SSS"/>
  <h3></h3>
 </body>
 </html>

与以前比现在实现起来不在用处理各个复杂的返回值数据,直接编写即可;
但是此时也存在问题了,就是没有返回值那么该怎么循环呢?
在进行迭代输出的时候之前是通过,doStartTag()和doAfterBody()操作的page范围,保存
一个个的对象属性,但是现在的操作中,可以直接通过getJspBody(),调用标签体中的内容。
 package tag.lid.simpledatetag;
 import java.util.*;
 import java.text.*;
 import java.io.*;
 import javax.servlet.jsp.*;
 import javax.servlet.jsp.tagext.*;
 public class SimpleIteratorTag 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)){//是否是page范围
    value=super.getJspContext().getAttribute

(this.name,PageContext.PAGE_SCOPE);
    }
   if("request".equals(this.scope)){//是否是request范围
    value=super.getJspContext().getAttribute

(this.name,PageContext.REQUEST_SCOPE);
    }
   if("session".equals(this.scope)){//是否是session范围
    value=super.getJspContext().getAttribute

(this.name,PageContext.SESSION_SCOPE);
    }
   if("application".equals(this.scope)){//是否是application范围
    value=super.getJspContext().getAttribute

(this.name,PageContext.APPLICATION_SCOPE);
    }
   if(value!=null && value instanceof List<?>){//此处一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;
   }
  }

在web.xml中:
  <tag>
   <name>simpleiterator</name> 
   <tag-class>
    tag.lid.simpledatetag.SimpleIteratorTag
   </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>

simpleiteratetag.jsp:
 <%@ page contentType="text/html" pageEncoding="gbk"%>
 <%@ page import="java.util.*"%> 
 <%@ taglib prefix="mytag" uri="lid" %>
 <html>
 <head><title>这是测试</title></head>
 <body>
  <%
   List<String> all=new ArrayList<String>();
   all.add("lid");
   all.add("yuj");
   all.add("family");
   request.setAttribute("all",all);
  %>
  <mytag:simpleiterator id="name" name="all" scope="request">
  <h2>姓名:${name}</h2>
  </mytag:simpleiterator>
  <h3></h3>
 </body>
 </html>

原创粉丝点击