简单的说明:
1,说明---jsp自定义标签;
HelloWorld:jsp自定义标签的使用流程;
QueryTag:带属性标签的定义以及连接数据库使用;
IteratorTag:带标签体的标签定义以及使用;
FragmentTag:jsp页面片段;
DynaAttributeTag:动态属性标签;
2,需要:ojdbc.jar;
.tld
SimpleTagSupport
1>创建web project-Taglib:
2>导入需要的jar包ojdbc14.jar;
3>项目中写了多个标签,以一个最基本的标签做例子,来说明流程:
建立HelloWorld.java:
package com.taglib;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class HelloWorld extends SimpleTagSupport {
/**
* 继承simpletagsupport,重写dotag();
*/
public void doTag() throws JspException, IOException {
getJspContext().getOut().write("hello" + new java.util.Date());
}
}
4>建立tld文件mytaglib.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 http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>JSTL 1.1 core library</description>
<display-name>JSTL core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>mytag</short-name>
<uri>http://www.crazyit.org/mytaglib</uri>
<tag>
<name>hello</name>
<tag-class>com.taglib.HelloWorld</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
5>建立taglib.jsp,要想使用自定义标签,注意以下配置(蓝色部分):
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="mytag" uri="http://www.crazyit.org/mytaglib" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!-- http://www.crazyit.org/mytaglib -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>标签体</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body bgcolor="#87CEFA">
<div>
<hr/>jsp自定义标签-第一个学习实例:
<mytag:hello/><br/>
</div>
</body>
6>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">
<welcome-file-list>
<welcome-file>taglib.jsp</welcome-file>
</welcome-file-list>
</web-app>
7>这样就可以运行,可以自己配置一遍,就可以大概了解这个运行过程,但是这是最简单的,关于复杂的标签,我们共同学习吧,最近在学习extreamcomponents.
8>关于项目中其他标签的具体建立,在附件中有具体的源码,在此不一一例举出来了。