首页 > 代码库 > javaWeb 使用jsp开发 if 标签

javaWeb 使用jsp开发 if 标签

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="t" uri="/WEB-INF/tag.tld"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>

    <%
        request.setAttribute("user", "user");
    %>
    <t:if test="${user==null }">还没有登录</t:if>

    <t:if test="${user!=null }">欢迎您: ${user }</t:if>

</body>
</html>
<?xml version="1.0" encoding="UTF-8" ?>
<!-- 文件名 /WEB-INF/referer.tld -->
<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">
    <tlib-version>1.0</tlib-version>
    <short-name>anyName</short-name><!-- 这个值可以任意设置 -->
    <uri>anyUri</uri><!-- 这个Uri可以任意设置,但是不要与别的 .tld 文件相同 -->
    <tag>
        <name>if</name>
        <tag-class>de.bvb.web.tag.IfTag</tag-class>
        <body-content>scriptless</body-content>
        <attribute>
            <name>test</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
</taglib>
package de.bvb.web.tag;

import java.io.IOException;

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

public class IfTag extends SimpleTagSupport {
    private boolean test;

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

    @Override
    public void doTag() throws JspException, IOException {
        if (test) {
            this.getJspBody().invoke(null); //  执行标签体
        }
    }

}

 

javaWeb 使用jsp开发 if 标签