java jsp自定义模板_jsp中自定义标签用法实例分析

本文实例讲述了jsp中自定义标签用法。分享给大家供大家参考。具体如下:

这里简单的写了一个自定义标签,自己定义标签的好处就是在jsp页面中可以使用自己定义的功能,完全与java代码分离

1. tld文件如下:

首先是要写×.tld文件,当项目随着服务器启动的时候,会检查项目中有没有*tld文件。

写的tld文件

xsi:schemalocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"

version="2.1">

1.0

apsliyuan

http://my.oschina.net/aps

hellowtag

cn.itcast.apsliyuan.tag.hellowttag

jsp

apsliyuantag

cn.itcast.apsliyuan.tag.apsliyuantag

jsp

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("


标签执行完毕了");

} catch (ioexception e) {

// todo auto-generated catch block

e.printstacktrace();

}

return eval_page;

}

}

3. jsp引入自己定义标签代码如下:

//jsp中引入自己定义的标签

pageencoding="utf-8"%>

index.jsp

现在的时间是:


我爱的人是:

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();

}

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 getvalues() {

if (values == null) {

return null;

}

return values.keys();

}

// private fields

private tag parent;

private hashtable 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程序设计有所帮助。

希望与广大网友互动??

点此进行留言吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值