JSP自定义标签控制JSP内容显示

1、第一步

#会员开关on off
member.switch=on

package com.web.util;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

/**
* @ClassName: MemberTag.java
* @Description: 控制会员显示与不显示
* @author: fengzf [email protected]
* @Date: 2017年8月15日 下午3:29:17
*
*/
public class MemberTag extends TagSupport {

@Override
public int doStartTag() throws JspException {
    String value = Config.config.getString("member.switch");  //从配置文件中获取
    if("on".equals(value)){
        return Tag.EVAL_BODY_INCLUDE;  //显示
    }else{
        return Tag.SKIP_BODY; //不显示
    }

}

}
2、第二步

  
<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.0tlib-version>
    <jsp-version>1.1jsp-version>
    <short-name>Application Tag Libraryshort-name>
    <tag>  
        <name>memberSwitchname>  
        <tag-class>com.web.util.MemberTagtag-class>  
        <body-content>JSPbody-content>  
    tag>  
taglib> 

3、第三步,应用
在页面头部添加代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="m" uri="/WEB-INF/member.tld" %>

在代码中应用

                    
                    <m:memberSwitch>
                        <c:if test='${param.sub != "sharedwgdetail"}'>
                            <a class="l_hei36 mar_r5 mar_t16 become_member_btn" onclick=" buyFun(1)">成为会员a>
                        c:if>
                        <c:if test='${param.sub eq "sharedwgdetail"}'>
                            <a class="l_hei36 mar_r5 mar_t16 become_member_btn" href="/member-advertising">成为会员a>
                        c:if>
                    m:memberSwitch>

若是从配置文件中获取member.switch的值为on则显示

<m:memberSwitch>m:memberSwitch>

之间的内容,为其他值则不显示

你可能感兴趣的:(SpringMVC)