1. We first introduce the java.servlet.jsp package. Since it is a maven project, directly import dependencies
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2.1-b03</version>
<scope>provided</scope>
</dependency>
2. Define the limit-taglib.tld file, which defines the limit tag and sub-tag. When used, limit:checker is similar to the target-class in which is the corresponding operation entity class, and the attribute is the attribute in the entity class.
<!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. Define the corresponding entity class
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;
}
}
Add the entity class corresponding to success, the entity class is mainly to obtain the verification result of the value of its own upper-level label
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;
}
}
}
Then define the entity class corresponding to fail
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. Define this tag in the project's web.xml file, so that the limit tag can be used in the project's jsp page
<jsp-config>
<taglib>
<taglib-uri>limit</taglib-uri>
<taglib-location>/WEB-INF/limit-taglib.tld</taglib-location>
</taglib>
</jsp-config>
5. Using an example, now the jsp page is tagged with <%@ taglib uri="limit" prefix="limit"%>
<limit:checker standard="sendback" standardList="${map.button }">
<limit:success>
<button class="inline-block btn2" onclick="backTask()">退回</button>
</limit:success>
</limit:checker>
This case is the limit:checker tag used. The attributes are standard and standardList. It is calculated by the method in the corresponding instance of check. Limit:success analyzes the result of its parent layer tag, that is, the limit:check tag. If it is true, The label is displayed, if false, the label is hidden. These logics are clearly written in the entity classes of these labels