jsp中自定义标签用法实例分析
程序员文章站
2024-01-14 08:40:10
本文实例讲述了jsp中自定义标签用法。分享给大家供大家参考。具体如下:
这里简单的写了一个自定义标签,自己定义标签的好处就是在jsp页面中可以使用自己定义的功能,完全与j...
本文实例讲述了jsp中自定义标签用法。分享给大家供大家参考。具体如下:
这里简单的写了一个自定义标签,自己定义标签的好处就是在jsp页面中可以使用自己定义的功能,完全与java代码分离
1. tld文件如下:
首先是要写×.tld文件,当项目随着服务器启动的时候,会检查项目中有没有*tld文件。
写的tld文件
<?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"> <!-- 定义版本 --> <tlib-version>1.0</tlib-version> <!-- 定义名字 --> <short-name>apsliyuan</short-name> <!-- 定义uri --> <uri>http://my.oschina.net/aps</uri> <!-- 定义自己的类 --> <tag> <!-- name 就是在jsp中显示的标签名字,<aps:hellowtag/> --> <name>hellowtag</name> <!-- 自己写的标签类的完整路径 --> <tag-class>cn.itcast.apsliyuan.tag.hellowttag</tag-class> <!-- jsp中主题中是不是要显示内容,有四个属性, 如果为empty的话。在jsp页面中标签就不能定义自己的内容了,否则会报 servlet.service() for servlet jsp threw exception org.apache.jasper.jasperexception: /index.jsp(12,8) according to tld, tag aps:hellowtag must be empty, but is not 异常 --> <body-content>jsp</body-content> </tag> <!-- 这里没有写属性,有时间补上 --> <tag> <name>apsliyuantag</name> <tag-class>cn.itcast.apsliyuan.tag.apsliyuantag</tag-class> <body-content>jsp</body-content> </tag> </taglib>
2. 自定义标签类java代码如下:
//自定义标签的类 package cn.itcast.apsliyuan.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.tagext.tagsupport; public class hellowttag extends tagsupport{ /** * */ private static final long serialversionuid = 1781703371130382609l; @override public int dostarttag() throws jspexception { // todo auto-generated method stub jspwriter out = pagecontext.getout(); simpledateformat format=new simpledateformat("yyyy-mm-dd hh:mm:ss"); try { out.print(format.format(new date())); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } return eval_body_include; } @override public int doendtag() throws jspexception { // todo auto-generated method stub jspwriter out = pagecontext.getout(); try { out.print("<br/> <hr/>标签执行完毕了"); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } return eval_page; } }
3. jsp引入自己定义标签代码如下:
//jsp中引入自己定义的标签 <%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <%@taglib uri="http://my.oschina.net/aps" prefix="aps" %> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>index.jsp</title> </head> <body> 现在的时间是:<aps:hellowtag> </aps:hellowtag><br/> <hr/> 我爱的人是: <aps:apsliyuantag/> </body> </html>
4. tagsupport代码如下:
//看看tagsupport中的源代码, 这个是适配器模式 public class tagsupport implements iterationtag, serializable { public static final tag findancestorwithclass(tag from, // tck signature test fails with generics @suppresswarnings("unchecked") class klass) { boolean isinterface = false; if (from == null || klass == null || (!tag.class.isassignablefrom(klass) && !(isinterface = klass.isinterface()))) { return null; } for (;;) { tag tag = from.getparent(); if (tag == null) { return null; } if ((isinterface && klass.isinstance(tag)) || klass.isassignablefrom(tag.getclass())) return tag; else from = tag; } } /** * default constructor, all subclasses are required to define only * a public constructor with the same signature, and to call the * superclass constructor. * * this constructor is called by the code generated by the jsp * translator. */ public tagsupport() { } /** * default processing of the start tag, returning skip_body. * * @return skip_body * @throws jspexception if an error occurs while processing this tag * * @see tag#dostarttag() */ public int dostarttag() throws jspexception { return skip_body; } /** * default processing of the end tag returning eval_page. * * @return eval_page * @throws jspexception if an error occurs while processing this tag * * @see tag#doendtag() */ public int doendtag() throws jspexception { return eval_page; } /** * default processing for a body. * * @return skip_body * @throws jspexception if an error occurs while processing this tag * * @see iterationtag#doafterbody() */ public int doafterbody() throws jspexception { return skip_body; } // actions related to body evaluation /** * release state. * * @see tag#release() */ public void release() { parent = null; id = null; if( values != null ) { values.clear(); } values = null; } /** * set the nesting tag of this tag. * * @param t the parent tag. * @see tag#setparent(tag) */ public void setparent(tag t) { parent = t; } /** * the tag instance most closely enclosing this tag instance. * @see tag#getparent() * * @return the parent tag instance or null */ public tag getparent() { return parent; } /** * set the id attribute for this tag. * * @param id the string for the id. */ public void setid(string id) { this.id = id; } /** * the value of the id attribute of this tag; or null. * * @return the value of the id attribute, or null */ public string getid() { return id; } /** * set the page context. * * @param pagecontext the pagecontext. * @see tag#setpagecontext */ public void setpagecontext(pagecontext pagecontext) { this.pagecontext = pagecontext; } /** * associate a value with a string key. * * @param k the key string. * @param o the value to associate. */ public void setvalue(string k, object o) { if (values == null) { values = new hashtable<string, object>(); } values.put(k, o); } /** * get a the value associated with a key. * * @param k the string key. * @return the value associated with the key, or null. */ public object getvalue(string k) { if (values == null) { return null; } else { return values.get(k); } } /** * remove a value associated with a key. * * @param k the string key. */ public void removevalue(string k) { if (values != null) { values.remove(k); } } /** * enumerate the keys for the values kept by this tag handler. * * @return an enumeration of all the keys for the values set, * or null or an empty enumeration if no values have been set. */ public enumeration<string> getvalues() { if (values == null) { return null; } return values.keys(); } // private fields private tag parent; private hashtable<string, object> values; /** * the value of the id attribute of this tag; or null. */ protected string id; // protected fields /** * the pagecontext. */ protected pagecontext pagecontext; }
dostarttag的返回值
在dostarttag返回的值决定的body部分的数据如何显示。
两个返回值:
0 – skip_body – 常量。不显示body。
1-evan_body_include ;包含body部分的数据,正常显示。
3:在doendtag也有两个返回值
决定后面的页面部分是否显示:
skip_page : 不再显示后面的页面部分。
eval_page : 显示后面的page部分。
希望本文所述对大家的jsp程序设计有所帮助。
上一篇: Python中的类(一)
下一篇: 万能数据库连接程序