在工程中定義jsp頁面可以使用的標籤

1.咱們先引入java.servlet.jsp包。因爲是maven項目,直接引入依賴java

<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.2.1-b03</version>
			<scope>provided</scope>
		</dependency>

2.定義limit-taglib.tld文件,該文件中定義了limit標籤和副標籤,使用的時候爲limit:checker相似 其中的target-class即爲對應的操做實體類,attribute爲實體類中的屬性。web

<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
  <tlib-version>1.0</tlib-version>
  <jsp-version>1.2</jsp-version>
  <short-name>limit</short-name>
  <tag>
  	<name>checker</name>
  	<tag-class>com.credithc.workorder.manage.utils.dict.ViewPowerChecker</tag-class>
  	<attribute>
  		<name>standard</name>
  		<required>true</required>
		<rtexprvalue>true</rtexprvalue>
  	</attribute>
  	<attribute>
  		<name>standardList</name>
  		<required>true</required>
		<rtexprvalue>true</rtexprvalue>
  	</attribute>
  </tag>
  <tag>
  	<name>success</name>
  	<tag-class>com.credithc.workorder.manage.utils.dict.ViewPowerSuccessTag</tag-class>
  </tag>
  <tag>
  	<name>fail</name>
  	<tag-class>com.credithc.workorder.manage.utils.dict.ViewPowerFailTag</tag-class>
  </tag>
</taglib>

3.定義對應的實體類api

public class ViewPowerChecker extends TagSupport{
	//這裏必須繼承tagSupport類,重寫他的doStartTag()方法,標籤在校驗的時候走的就是這個方法
	/**
	 * 
	 */
	private static final long serialVersionUID = 3362394184109444638L;
	/**
	 * 標籤驗證的權限值
	 */
	private String standard;
	
	private String standardList;
	
	/**
	 * 驗證結果的存儲key值
	 */
	static final String CHECK_FLAG = "CHECK_FLAG";
	
	@Override
	public int doStartTag() throws JspException {
		boolean tag = checked(standard, standardList);
		this.setValue(CHECK_FLAG, tag);
		return EVAL_BODY_INCLUDE;
	}
	

	private boolean checked(String standard,String standardList){
		String[] ss =standardList.split(",");
		if(StringUtils.isEmpty(standardList)){
			return false;
		}
		return Arrays.asList(ss).contains(standard);
	}

	public String getStandard() {
		return standard;
	}

	public void setStandard(String standard) {
		this.standard = standard;
	}


	public String getStandardList() {
		return standardList;
	}


	public void setStandardList(String standardList) {
		this.standardList = standardList;
	}

}

加入success對應的實體類,該實體類主要就是對本身的上層標籤的值的校驗結果進行獲取jsp

public class ViewPowerSuccessTag extends TagSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = -5674428112555303827L;

	@Override
	public int doStartTag() throws JspException {
		boolean tag = (Boolean)((TagSupport)this.getParent()).getValue(ViewPowerChecker.CHECK_FLAG);
		if(tag){
			return EVAL_BODY_INCLUDE;
		}else{
			return SKIP_BODY;
		}
	}
}

而後定義fail 對應的實體類maven

public class ViewPowerFailTag extends TagSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = -2700800433218941142L;

	@Override
	public int doStartTag() throws JspException {
		boolean tag = (Boolean)((TagSupport)this.getParent()).getValue(ViewPowerChecker.CHECK_FLAG);
		if(!tag){
			return EVAL_BODY_INCLUDE;
		}else{
			return SKIP_BODY;
		}
	}
}

4.在項目的web.xml文件中定義該標籤,這樣在該項目的jsp頁面就能夠使用limit標籤了ide

<jsp-config>
		<taglib>
			 <taglib-uri>limit</taglib-uri>
			 <taglib-location>/WEB-INF/limit-taglib.tld</taglib-location>
		</taglib>
    </jsp-config>

5.使用舉例, 如今該jsp頁面進行標籤引入 <%@ taglib uri="limit" prefix="limit"%>ui

<limit:checker standard="sendback" standardList="${map.button }">
									<limit:success>
										<button class="inline-block btn2" onclick="backTask()">退回</button>
									</limit:success>
								</limit:checker>

該案例即爲使用的limit:checker標籤,屬性爲standard和standardList,經過check對應實例裏的方法進行計算,limit:success對其父層標籤,即limit:check標籤的結果進行分析,若是爲true,則顯示該標籤,若是爲false則隱藏該標籤. 這些邏輯都清楚的寫在這些標籤的實體類中this

相關文章
相關標籤/搜索