JSTL自定义标签

来源:互联网 发布:注会考试网络培训哪好 编辑:程序博客网 时间:2024/06/18 05:49
package tag;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.JspWriter;import javax.servlet.jsp.PageContext;import javax.servlet.jsp.tagext.SimpleTagSupport;public class HelloTag extends SimpleTagSupport {private String str;private int count;public String getStr() {return str;}public void setStr(String str) {this.str = str;}public int getCount() {return count;}public void setCount(int count) {this.count = count;}@Overridepublic void doTag() throws JspException, IOException {PageContext ctx = (PageContext)getJspContext();JspWriter out = ctx.getOut();for(int i=0;i<count;i++){out.println(str+"<br/>");}}}

package tag;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;import javax.servlet.jsp.JspException;import javax.servlet.jsp.JspWriter;import javax.servlet.jsp.PageContext;import javax.servlet.jsp.tagext.SimpleTagSupport;/** * 自定义JSTL标签,用来输出服务器时间 * 时间有默认格式,也可以指定一个格式 */public class SysdateTag extends SimpleTagSupport {//时间格式有默认值,可以通过set方法修改其值private String format = "yyyy/MM/dd HH:mm:ss";public String getFormat() {return format;}public void setFormat(String format) {this.format = format;}@Overridepublic void doTag() throws JspException, IOException {//创建服务器时间Date date = new Date();SimpleDateFormat sdf = new SimpleDateFormat(format);String now = sdf.format(date);/** * 将时间输出给浏览器,该方法声明返回JspContext,该方法的 * 实际返回的是PageContext * PageContext extends JspContext *///强转为子类型PageContext ctx = (PageContext) getJspContext();JspWriter out = ctx.getOut();out.println(now);//此流不能关闭,因为JSP上还有其他标签要使用这个流}}

dui对应的tld文件,且放在WEB-INF目录下

<?xml version="1.0" encoding="UTF-8" ?><taglib xmlns="http://java.sun.com/xml/ns/javaee"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"    version="2.1"><description>WQ的标签库</description><display-name>WQ</display-name><tlib-version>1.1</tlib-version><short-name>c3</short-name><uri>/jsp1/tag</uri><tag>  <description>根据指定的次数打印文本</description>  <name>pt</name>  <tag-class>tag.HelloTag</tag-class>  <body-content>empty</body-content>  <attribute>    <description>Content</description>    <name>str</name>    <required>false</required>    <rtexprvalue>false</rtexprvalue>  </attribute>  <attribute>    <description>Count</description>    <name>count</name>    <required>false</required>    <rtexprvalue>false</rtexprvalue>  </attribute></tag></taglib>    
<?xml version="1.0" encoding="UTF-8" ?><taglib xmlns="http://java.sun.com/xml/ns/javaee"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"    version="2.1">      <description>这是我自己的标签库</description>  <display-name>My Tag</display-name>  <tlib-version>3.1</tlib-version>  <short-name>s</short-name>  <uri>/WQ-tags</uri>       <tag>   <description>用来输出当前服务器的时间,并且时间的格式可以任意指定</description>   <name>sysdate</name>   <tag-class>tag.SysdateTag</tag-class>   <!-- 声明该标签可以包含哪些内容  该例为但标签,没有内容 -->   <body-content>empty</body-content>   <attribute>   <description>用来设置时间的格式</description>   <name>format</name>   <!-- 是否必须给这个属性赋值 -->   <required>false</required>   <!-- 是否可以用EL给此属性赋值 -->   <rtexprvalue>true</rtexprvalue>   <type>java.lang.String</type>   </attribute>  </tag>     </taglib>  

jsp测试文件:

<%@page pageEncoding="utf-8" contentType="text/html; charset=utf-8" %><%@taglib uri="/jsp1/tag" prefix="c3" %><%@taglib uri="/WQ-tags" prefix="s"%><!doctype html><html>  <head>    <meta charset="utf-8" />    <title>自定义标签</title>  </head>  <body>    <c3:pt count="10" str="Hello JSTL"/>    <br><hr>    <s:sysdate />    <br><hr>    <s:sysdate format="yyyy'年'MM'月'dd'日' HH'时'mm'分'ss'秒'"/>    <br><hr>  </body></html>
输出结果:

Hello JSTL
Hello JSTL
Hello JSTL
Hello JSTL
Hello JSTL
Hello JSTL
Hello JSTL
Hello JSTL
Hello JSTL
Hello JSTL


2017/05/06 15:44:27 

2017年05月06日 15时44分27秒 







注意:内容可以参考标准标签库中的c.tld文件的内容,在使用JSTL标签前必须要写相应的指令-----taglib





标签的运行原理:

容器依据JSP页面中的uri找到tld文件(依据标签中的<c3:pt>pt这个名字找到标签类tag.HelloTag。接下来实例化该标签,同时属性值赋给参数,调用doTag方法


0 0
原创粉丝点击