jsp自定义标签的应用实例源码



jsp自定义标签的应用实例源码。

什么是jsp自定义标签
1.用户自定义的Java语言元素,实质是运行一个或两个接口的JavaBean.
2.非常紧密的和JSP的表示逻辑联系在一起,具有与普通JavaBean相同的业务逻辑处理能力.
3.一个JSP页面转变为servlet时,其间的用户自定义标签转化为操作一个称为标签hander的对象.
4.可操默认对象,处理表单数据,访问数据库及其他企业级服务.

CustomTag.java源文件:

package com.cn.customtag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class CustomTag extends BodyTagSupport{
//声明表示标签属性的成员变量
private String head=null;
private String img=null;
private String wid=null;
private String hei=null;

public void setHead(String head) {
this.head = head;
}
public void setHei(String hei) {
this.hei = hei;
}
public void setImg(String img) {
this.img = img;
}
public void setWid(String wid) {
this.wid = wid;
}
//覆盖doEndTag方法
public int doEndTag() throws JspException {
JspWriter jspWriter = pageContext.getOut();
//获得图片的目录
String imgDri = ((HttpServletRequest)pageContext.getRequest()).getContextPath()+”/img”;
//获取开始标签和结束标签之间的数据
String mess = getBodyContent().getString().trim();
try {
//在页面中产生HTML的img标签
jspWriter.println(“<img src=\”"+imgDri+img+”\”wid=\”"+wid+”\” hei=\”"+hei
+”\”align=\”left\”>”+”<H”+head+”>”+mess+”</H”+head+”>”);
} catch (IOException e) {
System.out.println(e);
}
return super.doEndTag();
}
// 覆盖doStartTag方法
public int doStartTag() throws JspException {
try {
//获得head的值
int head1 = new Integer(head).intValue();
//判断head1的值不在0和6之间时
if(!(head1>0&&head1<7)){
System.out.println(“head1必须在0到6之间”);
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.doStartTag();
}
// 覆盖release方法
public void release() {
head=null;
img=null;
wid=null;
hei=null;
}

}

customtag.tld


<?xml version=”1.0″ encoding=”UTF-8″ ?>
<taglib xmlns=”http://java.sun.com/xml/ns/j2ee”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd”
version=”2.0″>

<!– 自定义一个标签库 –>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>tags</short-name>
<uri>custom.tag</uri>
<description>JSP custom tags</description>
<tag>
<name>time</name>
<tagclass>com.cn.customtag.CustomTag</tagclass>
<bodycontent>JSP</bodycontent>
<description>This is my JSP custom tag</description>
<attribute>
<name>head</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>img</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>wid</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>hei</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>

sp_custom.jsp文件:

<%@ page language=”java” import=”java.util.*” pageEncoding=”utf-8″%>
<%@ taglib uri =”custom.tag” prefix=”tags”%>

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>

<title>My JSP ‘sp_custom.jsp’ starting page</title>

</head>
<body>
<h2 align=”center”>自定义标签的使用</h2>
<tags:time img=”/shijian.gif” head=”1″ wid=”250″ hei=”150″>营业时间!欢迎光临!</tags:time>

</body>
</html>

web.xml文件:

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app version=”2.5″
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-app_2_5.xsd”>
<jsp-config>
<taglib>
<taglib-uri>custom.tag</taglib-uri>
<taglib-location>customtag.tld</taglib-location>
</taglib>
</jsp-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>