分享

JSP自定义标签

 jerry_008 2017-04-05
1、JSP中标签分tag标签、tld标签
      tag标签本质就是一小段jsp,可以引入到页面中,实现一些简单功能;
      tld标签,内部实现又主要分为tag及function,通常开源框架提供的标签以tld为主,例如jstl的c、fmt,spring的form,Struts的html、logic等

2、tag标签使用
2.1 创建方式
在/WEB-INF/下创建tags目录,tag目录下可创建多个目录,目录下存在多个.tag文件
2.2 标签内容
 tag文件为JSP文件,可以有属性,把JSP页面当JavaBean文件一样使用。可定义属性attribute,有变量名(name),是否必需(required)等属性,相当于定义一个JavaBean的属性。在使用属性的值时,可通过get方法来访问或通过变量名的方式来访问。例如:
<%@ tag pageEncoding="UTF-8" %>
<%@ attribute name="id" required="true" rtexprvalue="true" %>
<%@ attribute name="webletID" required="true" rtexprvalue="true" %>
<B>JSP TAG ATTRIBUTE GET<B>
<div id="<%=getId() %>" style="border:solid 1px #ff0000">
Here, got the attribute value by get method:<%=getWebletID() %>
<br>Got the attribute value by variable:<%=webletID %>
</div>
<script>
</script>
2.3 引入方式
<%@ taglib prefix="ui" tagdir="/WEB-INF/tags" %> 
2.4  调用方式:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="mx" tagdir="/WEB-INF/tags"%>

3、tld标签使用
3.1 创建方式
     在/WEB-INF/下创建tlds目录,tld存在多个.tld文件
3.2 标签内容 
      tld为XML格式文件,在其中描述标签组件的类和属性等。
 <taglib>
   <tlib-version>1.0</tlib-version>
  <jsp-version>2.0</jsp-version>
   <short-name>Example TLD</short-name>
  <uri>http://struts./tags-html</uri>
<tag>
    <name>Hello</name>
    <tag-class>com.xxx.HelloTag</tag-class>
    <body-content>empty</body-content>
</tag> 
         <!--com.xxx.HelloTag需继承SimpleTagSupport,可以输出html页面--> 
<function>
   <description>自定义标签</description>
   <name>getPath</name>
   <function-class>com.xxx.Config</function-class>
   <function-signature>java.lang.String getPath()</function-signature>
   <example>${fns:getPath()}</example>
</function>
<!--com.xxx.Config不需继承,getPath要求static,只可输出数据-->
</taglib>
1.3 引入方式
<%@ taglib uri="http://struts./tags-html" prefix="html"%>
uri的内容在和tld中uri相对应
<%@ taglib prefix="fnc" uri="/WEB-INF/tlds/fns.tld" %>
 fns.tld在tlds目录下有定义
1.4  调用方式:
 <html>

  <head>
   
<title>A sample custom tag</title>
 
</head>
 
<body>
  
 <html:Hello/>

    ${fns:getPath()} 

  </body>
</html>

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多