天天看點

JSP簡單标簽的總結及案例

一、控制頁面部分内容是否輸出

1.控制頁面部分内容輸出

public void doTag() throws JspException,IOException{
    JspFragment jf=this.getJspBody();
    jf.invokee(null);//或jf.invoke(this.getJspContext().getOut());
}
           

2.控制頁面部分内容不輸出

public void doTag() throws JspException,IOException{
}
           

二、控制标簽内容重複輸出

public void doTag() throws JspException,IOException{
    JspFragment jf=this.getJspBody();
    for(int i=;i<;i++){
        jf.invoke(null);
    }
}
           

三、修改标簽體内容

pubilc void doTag() throws JspException,IOException{
    JspFragment jf=this.getJspBody();
    StringWriter sw=new StringWriter();
    jf.invoke(sw);
    String content=sw.toString();
    content=content.toUpperCase();
    this.getJspContext().getOut().write(content);   
}
           

四、控制整個頁面是否輸出

public void doTag() throws JspException,IOException{
    throw new SkipPageException();
}
           

【開發帶屬性的标簽】

<sitcast:demo count="5">
</sitcast:demo>
           

1.在标簽處理器編寫每個屬性對應的setter方法

public class SimpleTagDemo extends SimpleTagSupport{
    private int count;

    public void setCount(int count){
    this.count=count;   
    }

    public void doTag() throws JspException,IOException{
        JspFragment jf=this.getJspBody();
        for(int i=;i<count;i++){
            jf.invoke(null);
        }
    }
}
           

2.在TLD檔案中描述标簽的屬性

<attribute>
    <name>count</name>
    <required>true</required>
    <rtexprvalue>true</rtexprvalue>//是否支援El和腳本表達式
</attribute>
           

注意:屬性的值隻支援八種基本類型的轉換,若是其他的類型,需要用到表達式

1、開發反盜鍊标簽

使用标簽:
<fix:referer site="http://localhost" page="/index.jsp"></fix:referer>

代碼:
package cn.itcast.web.tag.eaxmple;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.SkipPageException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class RefererTag extends SimpleTagSupport {
    private String page;//要跳轉的頁面
    private String site;//針對自家的哪個網站防盜鍊
    public void setPage(String page) {
        this.page = page;
    }
    public void setSite(String site) {
        this.site = site;
    }
    @Override
    public void doTag() throws JspException, IOException {
        PageContext pageContext=(PageContext) this.getJspContext();
        HttpServletRequest request=(HttpServletRequest) pageContext.getRequest();
        HttpServletResponse response= (HttpServletResponse) pageContext.getResponse();
        String referer=request.getHeader("referer");

        if(referer==null || !referer.startsWith(site)){
            String cp=request.getContextPath();
            if(page.startsWith(cp))
            {   response.sendRedirect(page);
            }else if (page.startsWith("/")) {
                response.sendRedirect(cp+page);
            }else
            {
                response.sendRedirect(cp+"/"+page);
            }
            throw new SkipPageException();
        }       
        else
            super.doTag();
    }

}
           

2、開發for标簽

使用标簽:
<fix:if test="${user==null }">
    未登陸. <br>
</fix:if>

代碼:
public class IfTag extends SimpleTagSupport{
    private boolean test;
    public void setTest(boolean test){
        this.test=test;
    }

    public void doTag() throws JspException,IOException{
        if(test){
            this.getJspBody().invoke(null);
        }
    }
}
           

3、開發if else标簽

<fix:choose>
    <fix:when test="${user==null }">
                                          未登陸. <br>
    </fix:when>
    <fix:otherwith>
              welcome使用者已經登入. <br>
    </fix:otherwith>
</fix:choose>

public class Choose extends SimpleTagSupport {
    private boolean isDo;
    public boolean isDo() {
        return isDo;
    }
    public void setDo(boolean isDo) {
        this.isDo = isDo;
    }
    @Override
    public void doTag() throws JspException, IOException {
        this.getJspBody().invoke(null);
    }
}
public class WhenTag extends SimpleTagSupport {

    private boolean test;

    public void setTest(boolean test) {
        this.test = test;
    }

    @Override
    public void doTag() throws JspException, IOException {
        Choose parent=(Choose) this.getParent();
        if(test && !parent.isDo())
        {
            this.getJspBody().invoke(null);
            parent.setDo(true);
        }       
    }
}

public class OtherwithTag extends SimpleTagSupport {

    @Override
    public void doTag() throws JspException, IOException {
        Choose parent=(Choose) this.getParent();
        if(!parent.isDo())
        {
            this.getJspBody().invoke(null);
            parent.setDo(true);
        }       
    }
}
           

4、開發foreach标簽

使用标簽:
<fix:choose>
    <fix:when test="${user==null }">
                                          未登陸. <br>
    </fix:when>
    <fix:otherwith>
              welcome使用者已經登入. <br>
    </fix:otherwith>
</fix:choose>

代碼:
public class Choose extends SimpleTagSupport {
    private boolean isDo;
    public boolean isDo() {
        return isDo;
    }
    public void setDo(boolean isDo) {
        this.isDo = isDo;
    }
    @Override
    public void doTag() throws JspException, IOException {
        this.getJspBody().invoke(null);
    }
}
public class WhenTag extends SimpleTagSupport {

    private boolean test;

    public void setTest(boolean test) {
        this.test = test;
    }

    @Override
    public void doTag() throws JspException, IOException {
        Choose parent=(Choose) this.getParent();
        if(test && !parent.isDo())
        {
            this.getJspBody().invoke(null);
            parent.setDo(true);
        }       
    }
}

public class OtherwithTag extends SimpleTagSupport {

    @Override
    public void doTag() throws JspException, IOException {
        Choose parent=(Choose) this.getParent();
        if(!parent.isDo())
        {
            this.getJspBody().invoke(null);
            parent.setDo(true);
        }       
    }
}
           

5、開發html轉義标簽

使用标簽:
<fix:htmlFilter>
    <a href="a  發 a a">超連結的寫法</a>
</fix:htmlFilter>

代碼:
package cn.itcast.web.tag.example;

import java.io.IOException;
import java.io.StringWriter;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class HtmlFilter extends SimpleTagSupport {

    @Override
    public void doTag() throws JspException, IOException {
        JspFragment jf=this.getJspBody();
        StringWriter sw=new StringWriter();
        jf.invoke(sw);
        String content=sw.getBuffer().toString();
        content=filter(content);
        this.getJspContext().getOut().write(content);       
    }
    /*C:\\Tomcat 7.0\\webapps\\examples\\WEB-INF\\classes\\util\\HTMLFilter.java*/
    public static String filter(String message) {

        if (message == null)
            return (null);
        char content[] = new char[message.length()];
        message.getChars(, message.length(), content, );
        StringBuilder result = new StringBuilder(content.length + );
        for (int i = ; i < content.length; i++) {
            switch (content[i]) {
            case '<':
                result.append("&lt;");
                break;
            case '>':
                result.append("&gt;");
                break;
            case '&':
                result.append("&amp;");
                break;
            case '"':
                result.append("&quot;");
                break;
            default:
                result.append(content[i]);
            }
        }
        return (result.toString());
    }
}