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 变量描述

JSP自定义tag控件标签的更多相关文章

  1. Jsp 自定义tag标签

    1转自:https://blog.csdn.net/yusimiao/article/details/46835617 Jsp自定义tag标签 自定义tag标签的好处 程序员可以自定一些特定功能的标记 ...

  2. 如何自定义MVC控件?

    今天公司要写学习总结,想着想着还是先写一篇关于MVC内部什么东东的博客整理整理再发表吧,一举两得. 之前写过了路由.过滤器等.今天就研究一下怎么自定义MVC控件吧. 本人技术小菜,不喜勿喷.....( ...

  3. 安卓自定义组合控件--toolbar

    最近在学习安卓APP的开发,用到了toolbar这个控件, 最开始使用时include layout这种方法,不过感觉封装性不好,就又改成了自定义组合控件的方式. 使用的工具为android stud ...

  4. Android自定义控件之自定义组合控件

    前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原理(一).自定义属性Android自定义控件之自定义属性(二).今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发 ...

  5. C# DataGridView自定义分页控件

    好些日子不仔细写C#代码了,现在主要是Java项目,C#.Net相关项目不多了,有点手生了,以下代码不足之处望各位提出建议和批评. 近日闲来无事想研究一下自定义控件,虽然之前也看过,那也仅限于皮毛,粗 ...

  6. C#Winform VScrollBar+Pannel自定义列表控件(原)

    该控件的主要实现思路是用的objective-c中的自定义控件思路,主视图中放子视图 效果图 (窗体调用代码) public partial class Form1 : RibbonForm { Li ...

  7. (九)ASP.NET自定义用户控件(2)

    http://www.cnblogs.com/SkySoot/archive/2012/09/04/2670678.html 用户控件 在 .NET 里,可以通过两种方式把自己的控件插入到 Web 窗 ...

  8. Android自定义组合控件详细示例 (附完整源码)

    在我们平时的Android开发中,有时候原生的控件无法满足我们的需求,或者经常用到几个控件组合在一起来使用.这个时候,我们就可以根据自己的需求创建自定义的控件了,一般通过继承View或其子类来实现. ...

  9. 【Android开发日记】之入门篇(十四)——Button控件+自定义Button控件

        好久不见,又是一个新的学期开始了,为什么我感觉好惆怅啊!这一周也发生了不少事情,节假日放了三天的假(好久没有这么悠闲过了),实习公司那边被组长半强制性的要求去解决一个后台登陆的问题,结果就是把 ...

随机推荐

  1. String在方法中的传递方式(调用外部方法给String变量赋值时,未得到预期结果)

    示例: public class StringTraining { public static void changeStr(String str){ str = "137878" ...

  2. 二、React中的更新

    创建更新的方式:ReactDOM.render||hydrate 初次渲染,然后是setState.forceUpdate 一.React.render() 步骤:创建ReactRoot.创建Fibe ...

  3. maven 配置Hibernate

    1.首先在maven中添加jar包依赖 <dependency> <groupId>org.hibernate</groupId> <artifactId&g ...

  4. Java框架:spring框架整合hibernate框架的xml配置(使用注解的方式)

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  5. 【extjs6学习笔记】1.11 初始: config

    Ext JS有一个名为config的功能. 该配置允许您使用默认值声明公共属性,这些属性将被其他类成员完全封装. 通过config声明的属性将自动获取get()和set()方法,如果类没有定义这些方法 ...

  6. crontab配置shell实现后台进程不间断运行

    检测get_report_no.php进程是否一直在运行 #!/bin/bash PROC=`ps -ef |grep get_report_no.php|grep -v grep|wc -l` if ...

  7. HTML、CSS、JS、JQ速查笔记

      一.HTML  1.编写html文件 a.格式 <!DOCTYPE html> <html> <head> <title>标题</title& ...

  8. [Git] Create a new repository on the command line

    echo "# xxx" >> README.md git init git add README.md git commit -m "first commi ...

  9. Android(java)学习笔记76:Handler用法总结 和 秒表案例

    一.Handler的定义: Handler主要接收子线程发送的数据, 并用此数据配合主线程更新UI,用来跟UI主线程交互用.比如可以用handler发送一个message,然后在handler的线程中 ...

  10. Mybatis- 配置

    主配置文件 properties 第一种 <properties> <property name="jdbc.driver" value="com.my ...