springMVC使用自定义标签控制按钮显示

	1. 以下为java代码:
        package com.fh.tag;
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.jsp.JspException;
        import javax.servlet.jsp.tagext.BodyTagSupport;
        
        import org.springframework.web.context.WebApplicationContext;
        import org.springframework.web.context.support.WebApplicationContextUtils;
        
        import com.fh.service.system.permission.PermissionService;
        
        public class PermissionTag extends BodyTagSupport {
        	
        	/**
        	 * 
        	 */
        	private static final long serialVersionUID = -8534443755590632585L;
        	private String res;
        	
        	public String getRes() {
        		return res;
        	}
        
        	public void setRes(String res) {
        		this.res = res;
        	}
        
        	@Override
        	public int doStartTag() throws JspException {
        		HttpServletRequest request = (HttpServletRequest)this.pageContext.getRequest();
                WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
                PermissionService permissionService = (PermissionService)wac.getBean("permissionService");
                
                try {
        			if (permissionService.hasPermissionRes(res)) {
        				return EVAL_BODY_INCLUDE;
        			} else {
        				return SKIP_BODY;
        			}
        		} catch (Exception e1) {
        			e1.printStackTrace();
        			return SKIP_BODY;
        		}
        	}
        }
  1. 以下为java代码的项目位置:
    springMVC使用自定义标签控制按钮显示_第1张图片

  2. 以下为tld文件的代码:

         
         
         	1.0
         	1.2
         	permTag
         	http://itheima/permTag
         	Common Tag
         	Common Tag library
         	
         		perm
         		com.fh.tag.PermissionTag
         		JSP
         		  
                     res
                     false  
                     true
                     java.lang.String
                  
         	
         
    
  3. 以下为tld文件的项目位置:
    springMVC使用自定义标签控制按钮显示_第2张图片

  4. 在jsp页面中应用tag:

     <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
     <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
     <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
     <%
     	String path = request.getContextPath();
     	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     %>
     <%@ taglib prefix="permTag" uri="http://itheima/permTag"%>
     
     
     	
     		
     		
     		<%@ include file="../admin/top.jsp"%> 
     		
      	    
             
     	
     
     	
     			按钮
     	
     
    

你可能感兴趣的:(springMVC使用自定义标签控制按钮显示)