Review_JSP(其中包括自定义标签)

1.JSTL表达式语言负责处理表达式和文字。表达式由${ }字符所包围。

2.include指令和jsp:include动作  :include指令是在JSP文件被转换成Servlet的时候引入文件,而这里的jsp:include动作不同,插入文件的

时间是在页面被请求的时候。jsp:include动作的文件引入时间决定了它的效率要稍微差一点,而且被引用文件不能包含某些JSP代码(例如不

能设置HTTP头),但它的灵活性却要好得多。

3.RequestDispatcher.forward()方法和HttpServletResponse.sendRedirect()方法的区别是:前者仅是容器中控制权的转向,在客户端浏览器

地址栏中不会显示出转向后的地址;后者则是完全的跳转,浏览器将会得到跳转的地址,并重新发送请求链接。这样,从浏览器的地址栏中可

以看到跳转后的链接地址。所以,前者更加高效,在前者可以满足需要时,尽量使用Request Dispatcher.forward()方法,并且,这样也有助

于隐藏实际的链接。在有些情况下,比如,需要跳转到一个其它服务器上的资源,则必须使用 HttpServletResponse.sendRequest()方法。

4.自定义标签:

iterateTag.java
public class iterateTag extends BodyTagSupport{
    private String name;//在pageContext中标识的一个属性名
    private Iterator it;//要迭代的对象
    private String type;//it中对象的类型
    Collection collection;
    public void setCollection(Collection collection){
        if(collection.size()>0)
            it=collection.iterator();
    }
    //----标签开始时调用此方法-------
    public int doStartTag(){
        if(it==null) return SKIP_BODY;
        else return continueNext(it);
    }
    //----标签体执行完后调用此方法----
    public int doAfterBody(){
        return continueNext(it);
    }
    //----标签结束时调用此方法-------
    public int doEndTag(){
        try{
            if(bodyContent!=null)
                bodyContent.writeOut(bodyContent.getEnclosingWriter());
        }catch(Exception e){
            System.out.println(e);
        }
        return EVAL_PAGE;
    } 
    //----迭代----
    protected int continueNext(Iterator it){
        if(it.hasNext()){
            pageContext.setAttribute(name,it.next(),PageContext.PAGE_SCOPE);
            return EVAL_BODY_AGAIN;
        }else return SKIP_BODY;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
 /**
  * @return Returns the collection.
  */
 public Collection getCollection() {
  return collection;
 }
}

IterateTEI.java   //TagExtraInfo用于提供编译时标签的额外信息,为脚本变量定义的标签额外信息类必须在TLD中声明,这样元素tei-

class将会如下:<tei-class>XXXX.XXXXTei</tei-class>

public class IterateTEI extends TagExtraInfo {
    public IterateTEI() {
        super();
    }
    public VariableInfo[] getVariableInfo(TagData data){
        return new VariableInfo[]{
                new VariableInfo(data.getAttributeString("name"),
                        data.getAttributeString("type"),
                        true,VariableInfo.NESTED)             
        };
    }
}

XXX.tld 中的部分配置:
<!-- iterateTag-->
    <tag>
   
      <name>iterateTag</name>

      <tag-class>com.format.iterateTag</tag-class>
      <tei-class>com.format.IterateTEI</tei-class>

      <body-content>JSP</body-content>
      <attribute>
       <name>collection</name>
       <required>true</required>
       <rtexprvalue>true</rtexprvalue>
      </attribute>
      <attribute>
       <name>name</name>
       <required>true</required>
      </attribute>
      <attribute>
       <name>type</name>
       <required>true</required>
      </attribute>
    </tag>
jsp调用:
<%//----------设置一个ArrayList对象的初始值----------
   ArrayList testCol=new ArrayList();
   testCol.add("abc");
   testCol.add("bcd");
   testCol.add("add");
   request.setAttribute("testCol",testCol);

  %>
  <csai:iterateTag name="testColOne" collection="<%=testCol%>" type="String">
   输出一个值:out.println("testColOne")<br>
  </csai:iterateTag> 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值