07---jsp标签编程04(迭代标签)

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

在一个MVC设计模式之中,一个jsp文件中最好不要出现任何的scriptlet代码,因为这样会破坏程序的结构。
而且维护起来非常的麻烦,上一次已经演示了如何进行判断标签的开发,那么
这次来观察一下如何使用标签进行迭代操作的开发;
需要先开发一个迭代标签的处理操作类;
 package tag.lid.iterator;
 import java.io.*;
 import java.util.*;
 import javax.servlet.jsp.*;
 import javax.servlet.jsp.tagext.*;

 public class IteratorTag extends TagSupport{
  private String name;//接受属性的名称
  private String scope;//接受属性的范围
  private String id;//这个id用于保存集合中的每一个元素
  private Iterator<?> iterator;
  
  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)){//是否是application范围
    value=super.pageContext.getAttribute(this.name,PageContext.APPLICATION_SCOPE);
    }
   if(value!=null && value instanceof List<?>){//此处一List集合为例学习
     this.iterator=((List<?>)value).iterator();
     if(iterator.hasNext()){
      //将属性保存在page属性范围之中
      super.pageContext.setAttribute(this.id,iterator.next());
      return TagSupport.EVAL_BODY_INCLUDE;
      }
     else{
      return TagSupport.SKIP_BODY;
      }
    }else{
      return TagSupport.SKIP_BODY;
      }
   }
  public int doAfterBody() throws JspException{
    if(iterator.hasNext()){
      //将属性保存在page属性范围之中
      super.pageContext.setAttribute(this.id,iterator.next());
      return TagSupport.EVAL_BODY_AGAIN;
      }
     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;
   }
  }
 编译上面程序;
下面开始编写标签的描述文件;
  <tag>
   <name>iterator</name> 
   <tag-class>
    tag.lid.iterator.IteratorTag
   </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页面中测试以上的标签是否可以正常使用:
 <%@ page contentType="text/html" pageEncoding="gbk"%>
 <%@ page import="java.util.*"%>
 <%@ taglib prefix="mytag" uri="lid"%>
 <html>
 <head><title>这是测试</title></head>
 <body>
  <%//此代码这是测试,实际中此部分应该有servlet传递的
   List<String> all=new ArrayList<String>();
   all.add("lid");
   all.add("yuj");
   all.add("family");
   request.setAttribute("all",all);//将内容保存在标签执行
  %> 
  <mytag:present name="all" scope="request"><!--迭代之前的判断标签(之前已写过,此处直接用)-->
   <mytag:iterator id="url" name="all" scope="request">
    <h3>姓名:${url}</h3>
   </mytag:iterator>
  </mytag:present>
  
 </body>
 </html>

此处的操作比较简单,主要是为了掩饰各个方法之间的操作,以及为了以后讲解Struts提供了更方便的原理操作;
到此为我我们解决了jsp中的三种代码:
 ·接收属性
 ·判断
 ·迭代输出

原创粉丝点击