javaweb之自定义标签库——if标签

来源:互联网 发布:帝国cms采集教程 编辑:程序博客网 时间:2024/05/29 19:35
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib uri="/example" prefix="c" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title>开发if标签(是否执行标签体内容案例)</title>  </head>    <body>  <%--模拟servlet的代码 --%>  <%   request.getSession().setAttribute("user", "aaa");  %>  <c:if test="${user==null }">    aaaaaa    </c:if>        <c:if test="${user!=null }">    bbbbbb    </c:if>  </body></html>
package cn.itcast.web.tag.example;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.SimpleTagSupport;//开发if标签public class IfTag extends SimpleTagSupport {private boolean test;public void setTest(boolean test) {this.test = test;}//如果test是true输出标签体@Overridepublic void doTag() throws JspException, IOException {if(test) {this.getJspBody().invoke(null);}}}

<?xml version="1.0" encoding="UTF-8" ?><taglib xmlns="http://java.sun.com/xml/ns/j2ee"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"    version="2.0">    <description>A tag library exercising SimpleTag handlers.</description>    <tlib-version>1.0</tlib-version>    <short-name>example</short-name><!-- 描述文件的名称 -->    <uri>/example</uri><!-- 描述文件的uri -->            <tag>        <name>referer</name>        <tag-class>cn.itcast.web.tag.example.RefererTag</tag-class>        <body-content>empty</body-content>        <attribute>        <name>site</name>        <required>true</required>        <rtexprvalue>true</rtexprvalue>        </attribute>                <attribute>        <name>page</name>        <required>true</required>        <rtexprvalue>true</rtexprvalue>        </attribute>    </tag>        <tag>        <name>if</name>        <tag-class>cn.itcast.web.tag.example.IfTag</tag-class>        <body-content>scriptless</body-content>        <attribute>        <name>test</name>        <required>true</required>        <rtexprvalue>true</rtexprvalue>        </attribute>    </tag>   </taglib>


0 0