自定义标签 (choose)

因为有3个标签,所以写3个标签处理器类

1.ChooseTag

public class ChooseTag extends SimpleTagSupport {
  //定义一个标记,用于控制跳转去向.
public Boolean tag = true;    public Boolean getTag() { return tag; } public void setTag(Boolean tag) { this.tag = tag; } @Override public void doTag() throws JspException, IOException {
     //获取标签体 JspFragment jspBody
= this.getJspBody();
     //因为第一个是父标签,所以直接执行就好了,不用处理. jspBody.invoke(
null); super.doTag(); } }

2.WhenTag

public class WhenTag extends SimpleTagSupport {
  //用来接收判断条件
public Boolean test; public Boolean getTest() { return test; } public void setTest(Boolean test) { this.test = test; } @Override public void doTag() throws JspException, IOException {
     //获取父标签,并且获取他的tag的值 ChooseTag chooseTag
= (ChooseTag) this.getParent(); Boolean tag = chooseTag.getTag();
     //判断tag的值.如果test也成立,就执行
if (tag && this.getTest()) { chooseTag.setTag(false);
        //获取标签体 JspFragment jspBody
= this.getJspBody();
       //执行标签体 jspBody.invoke(
null); } super.doTag(); } }

3.OtherwiseTag

public class OtherwiseTag extends SimpleTagSupport{
    
    @Override
    public void doTag() throws JspException, IOException {
        ChooseTag  chooseTag = (ChooseTag) this.getParent();
        Boolean tag = chooseTag.getTag();
     //如果tag为true则说明没有进when
if(tag){ JspFragment jspBody = this.getJspBody(); jspBody.invoke(null); } super.doTag(); } }

 

写tld文件

<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>

    <tlibversion>1.2</tlibversion>
    <jspversion>1.1</jspversion>


    <shortname>mylib</shortname>
    <uri>http://www.fz.song.myLib</uri>



    <tag>
        <name>choose</name>
        <tagclass>fz.song.tag.ifelse.ChooseTag</tagclass>
        <bodycontent>scriptless</bodycontent>
      </tag> <tag> <name>when</name> <tagclass>fz.song.tag.ifelse.WhenTag</tagclass> <bodycontent>scriptless</bodycontent> <attribute>
        <!--属性名--> <name>test</name>
        <!--是否必须--> <required>true</required>
        <!--是否使用el--> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>otherwise</name> <tagclass>fz.song.tag.ifelse.OtherwiseTag</tagclass> <bodycontent>scriptless</bodycontent> </tag> <tag> <name>foreach</name> <tagclass>fz.song.tag.foreach.ForeachTag</tagclass> <bodycontent>scriptless</bodycontent> <attribute> <name>items</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>var</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> </tag> </taglib>

 

 

jsp页面

<%@ taglib uri="http://www.fz.song.myLib" prefix="x"%>




    <x:choose>
        <x:when test="${10>1}">true</x:when>
        <x:otherwise>false</x:otherwise>
    </x:choose>

 

补充

<body-content></body-content>中可以写入的参数有四种

① empty

即标记体为空

② scriptless

这个标记不能有脚本元素,但可以有模板文本和EL, 还可以是定制和标准动作

③ tagdependent

标记体要看做是纯文本,所以不会计算EL,也不会出发标记/动作

④ JSP

能放在JSP中的东西都能放在这个标记体中

posted @ 2015-10-28 22:28  宋发准  阅读(437)  评论(0编辑  收藏  举报