JSP自定义业务标签

时间:2022-08-12 09:00:55

JSP自定义业务标签JSP自定义业务标签JSP自定义业务标签JSP自定义业务标签

自定义标签:

package cn.hv.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport; public class BodyTag extends BodyTagSupport { private String model;
private int pc ; public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
return model.equals("yyt") ? super.EVAL_BODY_INCLUDE : super.SKIP_BODY;
} public int doAfterBody() throws JspException {
if(pc > 0 ){
pc--;
return super.EVAL_BODY_AGAIN;
}
return super.SKIP_BODY;
} public String getModel() {
return model;
} public void setModel(String model) {
this.model = model;
} public int getPc() {
return pc;
} public void setPc(int pc) {
this.pc = pc;
} }
<?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>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>c</short-name>
<tag>
<name>enable</name>
<tag-class>cn.hv.tag.BodyTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>model</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>pc</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
<jsp-config>
<taglib>
<taglib-uri>/web-html</taglib-uri>
<taglib-location>
/WEB-INF/web-html.tld
</taglib-location>
</taglib>
</jsp-config>

以上就是自定义标签:

web.xml :配置自定义的标签文件

EVAL_BODY_INCLUDE:显示标签体内

EVAL_BODY_AGAIN:循环

SKIP_BODY:不执行body体里面的内容

SKIP_PAGE:

本质:就是控制组件化,但是在我目前的项目中使用的较少,我们都是结合数据库表设计权限系统,前台基于JS做菜单以及按钮,页签的权限控制

===========================================================================================

场景:

  1.<c:permission menus=${lists} val=><li></li></c:permission>

  2.<% if %>html代码<% else %>html代码<%}%> 一般都是基于这种控制html输出

  3.JS创建组件,完全前台控制

===========================================================================================