JSP自定义tag控件标签

JSP支持自定tag的方法,那就是直接讲JSP代码保存成*.tag或者*.tagx的标签定义文件。tag和tagx文件不仅支持经典jsp代码,各种标签模版代码,还支持xml样式的jsp指令代码。

按照约定,tag和tagx文件需要放置在WEB-INF/tags目录下。

 

下面是一个简单的例子,使用自定义标签创建一个Button:

 1 <%@ tag language="java" pageEncoding="UTF-8"%>
 2 <%@ include file="/webpage/include/taglib.jsp"%>
 3 <%@ attribute name="id" type="java.lang.String" required="true"%>
 4 <%@ attribute name="url" type="java.lang.String" required="true"%>
 5 <%@ attribute name="title" type="java.lang.String" required="true"%>
 6 <%@ attribute name="width" type="java.lang.String" required="false"%>
 7 <%@ attribute name="height" type="java.lang.String" required="false"%>
 8 <%@ attribute name="target" type="java.lang.String" required="false"%>
 9 <%@ attribute name="label" type="java.lang.String" required="false"%>
10 <button class="btn btn-white btn-sm" data-toggle="tooltip" data-placement="left" onclick="edit()" title="修改"><i class="fa fa-file-text-o"></i> ${label==null?'修改':label}</button>
11                         </button>
12 <%-- 使用方法: 1.将本tag写在查询的form之前;2.传入table的id和controller的url --%>
13 <script type="text/javascript">
14 $(document).ready(function() {
15     $('#${id} thead tr th input.i-checks').on('ifChecked', function(event){ //ifCreated 事件应该在插件初始化之前绑定 
16           $('#${id} tbody tr td input.i-checks').iCheck('check');
17         });
18 
19     $('#${id} thead tr th input.i-checks').on('ifUnchecked', function(event){ //ifCreated 事件应该在插件初始化之前绑定 
20           $('#${id} tbody tr td input.i-checks').iCheck('uncheck');
21         });
22     
23 });
24 
25     function edit(){
26 
27           var size = $("#${id} tbody tr td input.i-checks:checked").size();
28           if(size == 0 ){
29                 top.layer.alert('请至少选择一条数据!', {icon: 0, title:'警告'});
30                 return;
31               }
32 
33           if(size > 1 ){
34                 top.layer.alert('只能选择一条数据!', {icon: 0, title:'警告'});
35                 return;
36               }
37             var id =  $("#${id} tbody tr td input.i-checks:checkbox:checked").attr("id");
38             openDialog("修改"+'${title}',"${url}?id="+id,"${width==null?'100%':width}", "${height==null?'100%':height}","${target}");
39         }
40 </script>

 

在页面中使用标签:

 <table:editRow url="${ctx}/samplingmanage/preparationManage/form" title="编辑" id="contentTable"></table:editRow><!-- 编辑按钮 -->

 

标签文件中常用的指令:

 

tag类似JSP page指令,可以用于import常用的java类库等
include导入其他的标签定义文件
taglib使用其他标签,如jstl, spring tag, struts tag等等
attribute定义一个属性
variable定义一个jsp page中可见的变量,默认范围为NESTED,表示标签内有效。可选项有AT_BEGIN和AT_END

这些指令的可选属性:

body-content标签body的处理方式 ,可选项: 'empty', 'tagdependent' or 'scriptless'
import导入使用的java类库
pageEncoding设置页面编码
isELIgnored是否忽略el表达式
dynamic-attributes用于存储自定义属性的map,所谓的自定义属性指:未隐式申明的变量
language使用的脚本语言,目前必须是java
display-name标签名
small-iconfor tools
large-iconfor tools
description标签作用描述
exampleinformal description of how the tag is used

 

<jsp:directive:attribute>的可选属性:

name属性名
requiredtrue or false,是否是必须的
rtexprvaluetrue or false - 指定是否支持运行时表达式
type值类型 - 默认是java.lang.String
fragmenttrue or false - 值先传递给容器(false), 直接传给标签处理方法(true)
description属性描述

 

<jsp:directive:variable>的可选属性:

name-given变量名(标签使用时的变量名)
name-from-attributeSpecifies the name of an attribute, whose value is the name of the variable that will be available in the calling JSP page. Exactly one of name-given or name-from-attribute must be supplied.
aliasA locally scoped variable which will store the variable's value. Used only with name-from-attribute.
variable-class变量类.默认是java.lang.String.
declareIndicates whether the variable is declared in the calling JSP page or tag file. Default is true. Not entirely clear what this means!
scope变量范围,可选项 AT_BEGIN(标签后jsp page内有效), AT_END(标签后jsp page内有效) and NESTED. NESTED(默认,标签内有效)
description变量描述

转载于:https://www.cnblogs.com/qingo00o/p/8989787.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值