JSP中自定义jstl标签

本期精彩:

      之前的博客中有介绍jstl标签和EL表达式:web知识之域对象&EL表达式&JSTL标签_小阿飞_的博客-CSDN博客_web域对象JSP中,关于域对象、EL、JSTLhttps://blog.csdn.net/yifei_345678/article/details/124203005?spm=1001.2014.3001.5501

之前的文章中是直接使用jstl中已经存在的标签,而本期的文章就是教会大家定义自己的标签去使用,为什么可以让我们自定义标签呢?其目的就是为了封装一些公共功能,比如分页功能的代码、一个复杂样式的按钮,如果这些代码每个人都去编写一遍,显然会增加工作量,所以我们可以把相关代码封装成我们自定义的标签,这样在自己和他人需要使用时,直接使用这个标签就可以获得相关的功能了🙂

什么是jstl标签库

在自定义jstl标签之前我们i先简单了解一些概念👇

标签的概念👇

  • 是标记语言(Mark Language),是一种注释文本的语言,以便于计算机可以操作。很多与“ML”结尾的语言都是标记语言,比如:HTML,XML,XHTML,VML等等
  • 标记语言与其他语言一样,也需要运行它们的环境,比如HTML的运行环境时浏览器,XML也要自己的解析和运行的环境

标签类型👇

  1. UI标签, 输出页面元素
  2. 控制标签, 如if标签,foreach标签等
  3. 数据标签,用于向页面输入数据

基本结构👇

  • <开始标签>标签体</结束标签>

空标签(没有标签体的标签)👇

  • <开始标签 属性名="属性值"/></结束标签>
  • <br/><br/>
  • <开始标签 属性名="属性值"/>

jstl标签库的概念👇

是一个JSP标签集合,它封装了JSP应用的通用核心功能, 基于JSP标签我们可以理解为,是JSP应该通用功能的一种封装方式

jstl自定义标签的生命周期

要想自定义标签,首先要了解好jstl自定义标签的生命周期,因为我们自定义标签的步骤就是根据下图中的生命周期来的👇

自定义标签 

1、自定义标签类

这里我编写了out标签和if标签来举例👇

  • out 标签类(继承BodyTagSupport)
/**
*     out 标签作用:向JSP页面中写入数据
*/
public class OutTag extends BodyTagSupport{  
  
    private String val;
	private String defaultVal;

	public void setVal(String val) {
		this.val = val;
	}
    //为空时设置默认值的方法
	public void setDefaultVal(String defaultVal) {
		this.defaultVal = defaultVal;
	}
    
    //标签的开始方法(需要给自定义的标签添加什么操作,就重写对应的生命周期中的方法)
	@Override
	public int doStartTag() throws JspException {
        //this.pageContext:通过当前类获取pageContext对象
        //pageContext对象中有一个getOut写出方法
		JspWriter out = this.pageContext.getOut();		
		try {
			if(val==null&&val.equals("")) {
				out.println(this.defaultVal);
			} else {
				out.println(this.val);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}		
        //跳过标签体,out定义的是空标签
		return SKIP_BODY;
	}
}
  •  if 标签类(继承BodyTagSupport)
@SuppressWarnings("serial")
public class IfTag extends BodyTagSupport{
	private boolean test;
	public boolean isTest() {
		return test;
	}
	public void setTest(boolean test) {
		this.test = test;
	}	
	@Override
	public int doStartTag() { 		
		if(this.isTest()) {
            //判断标签体时
			return EVAL_BODY_INCLUDE;
		} 
		return SKIP_BODY;		
	}
}
  • foreach标签类
public class ForeachTag extends BodyTagSupport {
	private List<?> items;
	private String var;
	public void setItems(List<?> items) {
		this.items = items;
	}
	public void setVar(String var) {
		this.var = var;
	}	
	@Override
	public int doStartTag() { 
		
		if(Objects.isNull(this.items) || this.items.size() <= 0) {
			return SKIP_BODY;
		}
		
		Iterator<?> it = this.items.iterator();
		Object next = it.next();
		this.pageContext.setAttribute(var, next);
		this.pageContext.setAttribute("iterator", it);
		
		return EVAL_BODY_INCLUDE;
	}
		@Override
	public int doAfterBody() {
		Iterator<?> it= (Iterator<?>)this.pageContext.getAttribute("iterator");
		if(it.hasNext()) {
			this.pageContext.setAttribute(var, it.next());
			return EVAL_BODY_AGAIN;
		}
		
		return SKIP_BODY;
	}	
}
  •  select标签类
public class SelectTag extends BodyTagSupport {	
	private String id;	
	private String name;
	private List<?> items;
	private String cssClass;
	private String cssStyle;
	private String value;
	private String text;
	private String selectValue;

	public void setText(String text) {
		this.text = text;
	}
	public void setValue(String value) {
		this.value = value;
	}
	public void setSelectValue(String selectValue) {
		this.selectValue = selectValue;
	}
	public void setId(String id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setItems(List<?> items) {
		this.items = items;
	}
	public void setCssClass(String cssClass) {
		this.cssClass = cssClass;
	}
	public void setCssStyle(String cssStyle) {
		this.cssStyle = cssStyle;
	}		
	@Override
	public int doStartTag()  {		
		JspWriter out = this.pageContext.getOut();
		try {
			String html = getSelectHtml();
			out.print(html);
		} catch (Exception e) {
			e.printStackTrace();
		}		
		return SKIP_BODY;
	}
	//通过注入的属性值,来构造select元素的字符串
	private String  getSelectHtml() throws Exception {
		StringBuilder sb = new StringBuilder();		
		//构造页面需要的select元素
		sb.append("<select id='"+id+ "' name='"+name+"'");
		if(!StringUtils.isNullOrEmpty(this.cssClass)) {
			sb.append(" class='"+this.cssClass+"'");
		}
		if(!StringUtils.isNullOrEmpty(this.cssStyle)) {
			sb.append(" style='"+this.cssStyle+"'");
		}
		sb.append(">");		
		//循环生成options
		for(Object option: this.items) {
			String val = BeanUtils.getProperty(option, this.value);
			String txt = BeanUtils.getProperty(option, this.text);
			//value="id"  text="name" 
			if(val.equals(this.selectValue)) {
				sb.append("<option value='"+val+"' selected>" + txt + "</option>");
			} else {
				sb.append("<option value='"+val+"' >" + txt + "</option>");
			}
		}
		sb.append("</select>");
		return sb.toString();
	}
}

2、编写标签库描述文件(tld) 

将你定义的标签具备相应的格式规范编辑在 tld 文件中

<!DOCTYPE taglib
  PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
   "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<!-- 标签库描述符 -->
<taglib xmlns="http://java.sun.com/JSP/TagLibraryDescriptor">
	<tlib-version>1.0</tlib-version>
	<jsp-version>1.2</jsp-version>
	<short-name>Simple Tags</short-name>
	<uri>/zking</uri>
	
	<tag>
		<name>out</name>
		<tag-class>com.myjstl.OutTag</tag-class>
		<body-content>empty</body-content>
		<attribute>
			<name>val</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
			<description>out标签val属性,用于输出val的值</description>
		</attribute>
		<attribute>
			<name>defaultVal</name>
			<required>false</required>
			<rtexprvalue>false</rtexprvalue>
			<description>用于定义默认值</description>
		</attribute>
	</tag>
	
	<tag>
		<name>if</name>
		<tag-class>com.myjstl.IfTag</tag-class>
		<body-content>jsp</body-content>
		<attribute>
			<name>test</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
			<description>if标签</description>
		</attribute>
	</tag>

    <tag>
		<name>foreach</name>
		<tag-class>com.zking.mymvc.tag.ForeachTag</tag-class>
		<body-content>jsp</body-content>
		<description>foreach标签</description>
		<attribute>
			<name>items</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>var</name>
			<required>true</required>
			<rtexprvalue>false</rtexprvalue>
		</attribute>
	</tag>
	
	<tag>
		<name>select</name>
		<tag-class>com.zking.mymvc.tag.SelectTag</tag-class>
		<body-content>empty</body-content>
		<attribute>
			<name>id</name>
			<required>true</required>
			<rtexprvalue>false</rtexprvalue>
		</attribute>
		<attribute>
			<name>name</name>
			<required>true</required>
			<rtexprvalue>false</rtexprvalue>
		</attribute>
		<attribute>
			<name>items</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>cssClass</name>
			<required>false</required>
			<rtexprvalue>false</rtexprvalue>
		</attribute>
		<attribute>
			<name>cssStyle</name>
			<required>false</required>
			<rtexprvalue>false</rtexprvalue>
		</attribute>
		<attribute>
			<name>value</name>
			<required>true</required>
			<rtexprvalue>false</rtexprvalue>
		</attribute>
		<attribute>
			<name>text</name>
			<required>true</required>
			<rtexprvalue>false</rtexprvalue>
		</attribute>
		<attribute>
			<name>selectValue</name>
			<required>false</required>
			<rtexprvalue>false</rtexprvalue>
		</attribute>
	</tag>

</taglib>

👆详解

  • <tag></tag>:里面放一个自定义标签的所有格式规范
  • <name>out</name>:表示自定义的标签名
  • <tag-class>com.zking.mymvc.tag.OutTag</tag-class>:表示第一步中自定义的标签类的完整类名(这里是通过反射机制找到自定义的标签类的)
  •  <body-content>empty</body-content>:代表没有标签体,填jsp就代表有
  • <attribute>:代表自定义的标签类中的属性规范  <name>val</name>:自定义的属性名字
                <required>true</required>:自定义的标签中必须写出的属性
                <rtexprvalue>true</rtexprvalue>:属性值是否可以使用EL表达式
                <description>out标签val属性,用于输出val的值</description>:自定义属性的描述
    </attribute>
      

3、在JSP上使用自定义的标签(测试)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="z" uri="/zking" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

	<%
		request.setAttribute("name", null);
	%>
	<!-- out 标签 -->
	<z:out val="${name}" defaultVal="你输入了null关键字"/>
	
	
	<z:if test="${100 == 100}">
		测试if(100 == 100)
	</z:if>
	
	<z:if test="${100 != 200}">
		测试if(100 != 200)
	</z:if>
</body>
</html>

 输出结果👇


感谢阅读,你学会了吗(●'◡'●) 

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值