Jsp 自定义tag标签

Jsp自定义tag标签

自定义tag标签的好处
程序员可以自定一些特定功能的标记, 用来封装代码, 达到分工, 重用性等多种好处.
如何存放tag标签
通常在web工程WEB-INF文件夹下创建tags文件夹来存放自定义的tag,如/WEB-INF/tags
tag标签的语法
要知道怎样定义tag标签就需要知道tag标签的基本属性,例如:

<%@ tag body-content="empty" trimDirectiveWhitespaces="true" pageEncoding="UTF-8"%>

通常我们在*.tag文件的开头加上以上的代码来告诉jsp容器这是一个tag标记文件。

body-content有以下三种属性:
1. empty:这个是一个空标记.
2. scriptless:标记主体可以有内容, 并且jsp容器会去处理里面的jsp元素, 这些内容可以是文本, EL表达式, 标准动作甚至另一个自定义标记.
3. tagdependent:标记主体可以有内容, 而jsp容器会把它们当作纯文件处理

trimDirectiveWhitespaces=“true”表示删除多余的空行
pageEncoding=”UTF-8” 表示page的编码格式

标记中也可以定义attribute,例如:

<%@ attribute name="x" required="true" rtexprvalue="true" %>
<%@ attribute name="y" required="true" rtexprvalue="true" %>

attribute的属性介绍如下:
1. name :这个attribute的名称.
2. required : true/false, 是否必须的.
3. rtexprvalue : true/false, 这个attribute可否使用EL表达式, 否则为纯文本.
4. type : 设定这个attribute的类型, jsp容器会把结果自动转换成这个类.

单纯的说以上的定义是不是有点糊涂呢?农夫选豆种——举例(粒)为证:
先展示出自定义的tag在web工程下的目录结构,
这里写图片描述

我们看一下index.jsp的代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib tagdir="/WEB-INF/tags/" prefix="yu" %>
<!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>define tag</title>
</head>
<body>
    <!-- test tag [body-content="empty"] -->
    <yu:add x="1" y="2"/>

</body>
</html>

该index.jsp中引用了add.tag文件,看一下add.tag文件的定义:

<%@ tag body-content="empty" trimDirectiveWhitespaces="true" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ attribute name="x" required="true" rtexprvalue="true" %>
<%@ attribute name="y" required="true" rtexprvalue="true" %>
<div>
    <h4>The result of x+y is</h4> 
    result = ${x+y}
</div>

运行起来程序我们看看运行结果:
这里写图片描述
至此,基本上已经理解了tag文件的使用了吧,那我们做进一步的学习。
继续编写index.jsp代码,来验证body-content=”scriptless”的tag文件:

<!-- test tag [body-content="scriptless"] -->
<yu:scriptless_tag>
This tag's body-content is [scriptless], let's have a test, i'm here.<br/>
<label>contextPath:<label/> ${pageContext.request.contextPath}
</yu:scriptless_tag>

那我们来看一下scriptless_tag.tag文件的定义:

<%@ tag body-content="scriptless" trimDirectiveWhitespaces="true"  pageEncoding="UTF-8"%>
<div style="background-color: red; width=auto">my backgroud color is red</div>
<jsp:doBody></jsp:doBody>
<div style="background-color: blue; width=70px;">my backgroud color is blue</div>

细心的朋友发现这里面多了一个\<\jsp:doBody>标签,该标签的作用就是讲index.jsp中该标记文件标记的主体显示在该处,来看一下程序运行结果吧,一目了然:
这里写图片描述
在index.jsp文件中scriptless_tag标签标记的主体显示在了红色背景和蓝色背景之间,并且将代码${pageContext.request.contextPath}解析成了 /yuTestTag

继续编写index.jsp代码,来验证body-content=”tagindependent”的tag文件:

<!-- test tag [body-content="tagindependent"] -->
<yu:tagdependent_tag>
This tag's body-content is [tagindependent], let's have a test, i'm here.
<label>contextPath:<label/> ${pageContext.request.contextPath}
</yu:tagdependent_tag>

scriptless_tag.tag文件的定义和scriptless_tag.tag的内容基本一样:

<%@ tag body-content="tagdependent" trimDirectiveWhitespaces="true" language="java" pageEncoding="UTF-8"%>
<div style="background-color: red; width=auto">my backgroud color is red</div>
<jsp:doBody></jsp:doBody>
<div style="background-color: blue; width=70px;">my backgroud color is blue</div>

继续看看运行结果:
这里写图片描述
代码${pageContext.request.contextPath}没有被解析,只是当做纯文本文件

最后附上index.jsp的完整代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib tagdir="/WEB-INF/tags/" prefix="yu" %>
<!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>define tag</title>
</head>
<body>
    <!-- test tag [body-content="empty"] -->
    <yu:add x="1" y="2"/>

    <hr>

    <!-- test tag [body-content="scriptless"] -->
    <yu:scriptless_tag>
    This tag's body-content is [scriptless], let's have a test, i'm here.<br/>
    <label>contextPath:<label/> ${pageContext.request.contextPath}
    </yu:scriptless_tag>

    <hr>

    <!-- test tag [body-content="tagindependent"] -->
    <yu:tagdependent_tag>
    This tag's body-content is [tagindependent], let's have a test, i'm here.
    <label>contextPath:<label/> ${pageContext.request.contextPath}
    </yu:tagdependent_tag>
</body>
</html>

到此我相信这三种类型的tag的区别已经显而易见了,先简单的写到这,如有不妥请留言补充说明。

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值