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

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

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

 <%@ tag language="java" pageEncoding="UTF-8"%>
<%@ include file="/webpage/include/taglib.jsp"%>
<%@ attribute name="id" type="java.lang.String" required="true"%>
<%@ attribute name="url" type="java.lang.String" required="true"%>
<%@ attribute name="title" type="java.lang.String" required="true"%>
<%@ attribute name="width" type="java.lang.String" required="false"%>
<%@ attribute name="height" type="java.lang.String" required="false"%>
<%@ attribute name="target" type="java.lang.String" required="false"%>
<%@ attribute name="label" type="java.lang.String" required="false"%>
<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>
</button>
<%-- 使用方法: 1.将本tag写在查询的form之前;2.传入table的id和controller的url --%>
<script type="text/javascript">
$(document).ready(function() {
$('#${id} thead tr th input.i-checks').on('ifChecked', function(event){ //ifCreated 事件应该在插件初始化之前绑定
$('#${id} tbody tr td input.i-checks').iCheck('check');
}); $('#${id} thead tr th input.i-checks').on('ifUnchecked', function(event){ //ifCreated 事件应该在插件初始化之前绑定
$('#${id} tbody tr td input.i-checks').iCheck('uncheck');
}); }); function edit(){ var size = $("#${id} tbody tr td input.i-checks:checked").size();
if(size == 0 ){
top.layer.alert('请至少选择一条数据!', {icon: 0, title:'警告'});
return;
} if(size > 1 ){
top.layer.alert('只能选择一条数据!', {icon: 0, title:'警告'});
return;
}
var id = $("#${id} tbody tr td input.i-checks:checkbox:checked").attr("id");
openDialog("修改"+'${title}',"${url}?id="+id,"${width==null?'100%':width}", "${height==null?'100%':height}","${target}");
}
</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-icon for tools
large-icon for tools
description 标签作用描述
example informal description of how the tag is used

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

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

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

name-given 变量名(标签使用时的变量名)
name-from-attribute Specifies 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.
alias A locally scoped variable which will store the variable's value. Used only with name-from-attribute.
variable-class 变量类.默认是java.lang.String.
declare Indicates 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 变量描述

最新文章

  1. div 模拟&lt;select&gt;事件
  2. Linux Hackers/Suspicious Account Detection
  3. JS获取阴历阳历和星期
  4. Oracle PL/SQL中的循环处理(sql for循环)
  5. centos下安装mycat
  6. JS之类型转换
  7. Java实战之02Hibernate-04多表映射
  8. 正确lua简单的扩展,可以加速相关C++数据。
  9. Android checkCallingPermission()方法返回值问题
  10. Oracle 12C 新特性之 恢复表
  11. swift中单例类的创建
  12. [转]OpenStreetMap/Google/百度/Bing瓦片地图服务(TMS)
  13. 2 Modals of necessity
  14. Java接口自动化测试之TestNG学习(二)
  15. Linux实践:文件破解
  16. web前端----JavaScript的DOM(一)
  17. 20145127《java程序设计》第二次实验
  18. 某DP题目1
  19. python学习(二十四) 字符串格式化
  20. [Clr via C#读书笔记]Cp8方法

热门文章

  1. Teradata 认证系列 - 2. Teradata数据库总览
  2. python之re模块和正则表达式
  3. echarts的title和legend重合解决(各种小细节)
  4. C#实现正则表达式
  5. linux 命令——13 less(转)
  6. Extjs4.1+desktop+SSH2 定义程序入口
  7. Android(java)学习笔记93:为什么局部内部类只能访问外部类中的 final型的常量
  8. 【BZOJ1059】[ZJOI2007] 矩阵游戏(匈牙利算法)
  9. Go - 环境安装
  10. python_20_列表