`

web JSP页面按钮权限控制(tld标签标签类实现BodyTagSupport)

 
阅读更多
1.xxx-tags.tld(关联具体的tag类)

放在WEB-INF目录下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
  <tlib-version>2.2.3</tlib-version>
  <jsp-version>1.2</jsp-version>
  <short-name>tms</short-name>
  <uri>/xxxx-tags</uri>
  <display-name>"Struts Tags"</display-name>
  <description><![CDATA["To make it easier to access dynamic data;
                    the Apache Struts framework includes a library of custom tags.
                    The tags interact with the framework's validation and internationalization features;
                    to ensure that input is correct and output is localized.
                    The Struts Tags can be used with JSP FreeMarker or Velocity."]]></description>
                    
  <tag>
    <name>button</name>
    <tag-class>xxx.tag.ButtonTag</tag-class>
    <body-content>JSP</body-content>
    <description><![CDATA[Render a HTML href element that when clicked can optionally call a URL via remote XMLHttpRequest and updates its targets]]></description>
    <attribute>
      <name>code</name>
      <required>true</required>
      <rtexprvalue>false</rtexprvalue>
      <description><![CDATA[Set the html accesskey attribute on rendered html element]]></description>
    </attribute>
    <attribute>
      <name>xxId</name>
      <required>false</required>
      <rtexprvalue>false</rtexprvalue>
      <description><![CDATA[Set the html accesskey attribute on rendered html element]]></description>
    </attribute>
    <attribute>
      <name>xxOnclick</name>
      <required>false</required>
      <rtexprvalue>false</rtexprvalue>
      <description><![CDATA[Set the html accesskey attribute on rendered html element]]></description>
    </attribute>
     <attribute>
      <name>xxType</name>
      <required>false</required>
      <rtexprvalue>false</rtexprvalue>
      <description><![CDATA[Set the html accesskey attribute on rendered html element]]></description>
    </attribute>
    <attribute>
      <name>xxClass</name>
      <required>false</required>
      <rtexprvalue>false</rtexprvalue>
      <description><![CDATA[Set the html accesskey attribute on rendered html element]]></description>
    </attribute>
    <attribute>
      <name>xxValue</name>
      <required>false</required>
      <rtexprvalue>false</rtexprvalue>
      <description><![CDATA[Set the html accesskey attribute on rendered html element]]></description>
    </attribute>
    </tag> 
</taglib>


2.xxx.tag.ButtonTag


public class ButtonTag extends BaseTag {

	
	private static final long serialVersionUID = 1L;

	
	private String xxValue;
	
	public String getXxValue() {
		return xxValue;
	}

	public void setXxValue(String xxValue) {
		this.xxValue = xxValue;
	}

	public String getHtml(){
		StringBuffer sb=new StringBuffer();
		String body=this.bodyContent.getString();
		sb.append("<button ");
		
		if(SysUtils.isNotEmpty(xxValue)){
			sb.append("  value=").append(xxValue);
		}
		sb.append(">");
		sb.append(body);
		sb.append("</button>");
		return sb.toString();
	}
}


BaseTag

BaseTag比较重要的方法是doEndTag进行用户是否有权限显示对应的tag


public abstract class BaseTag extends BodyTagSupport {

	private String workCode;
//其他标签类实现该方法
        protected abstract String getHtml();

	public String getWorkCode() {
		return workCode;
	}

	public void setWorkCode(String workCode) {
		this.workCode = workCode;
	}
	private static final long serialVersionUID = 1L;
//控制的关键
	@Override
	public int doEndTag() throws JspException {
		
		String autcode=getAutcodeByWorkCode();
		JSONObject user = (JSONObject)ServletActionContext.getRequest().getSession().getAttribute("USER_INFO");
		String[] auth = (String[])ServletActionContext.getRequest().getSession().getAttribute("USER_AUTH");
		if(user == null){
			return EVAL_PAGE;
		}
		if(autcode == null){
			try {
				pageContext.getOut().write(getHtml());
			} catch (IOException e) {
				e.printStackTrace();
			}
    	}else{
    		if(auth != null){
    			if(SysUtils.hasAuth(auth,workCode)){
    				try {
// 这句话是显示的关键
    					pageContext.getOut().write(getHtml());
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    			}
    		}
    	}
		return EVAL_PAGE;

	}
}


3.页面引用

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="/xxxx-tags"  prefix="cbm"%>  //对应tag文件中的uri内容
<cbm:button xxId="xx" code="权限码" xxType="button" xxClass="btn ">xx</cbm:button>
//权限的控制主要是与code进行交互				


1.BodyTagSupport主要是标签处理类需要与标签体交互时使用

2.BodyTagSupport三个重要方法
doStartTag(),doEndTag(),doAfterBody(),

doStartTag()方法是遇到标签开始时会呼叫的方法,其合法的返回值是EVAL_BODY_INCLUDE与SKIP_BODY,前者表示将显示标签间的文字,后者表示不显示标签间的文字;
doEndTag()方法是在遇到标签结束时呼叫的方法,其合法的返回值是EVAL_PAGE与 SKIP_PAGE,前者表示处理完标签后继续执行以下的JSP网页,后者是表示不处理接下来的JSP网页
doAfterBody()这个方法是在显示完标签间文字之后呼叫的,其返回值有EVAL_BODY_AGAIN与SKIP_BODY,前者会再显示一次标签间的文字,后者则继续执行标签处理的下一步。




分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics