JSP 自定义标签
2013-11-29 15:12
351 查看
1) 标签库和实际开发
标签库是非常重要的技术,对于JSP初学者和普通开发人员,能够自己开发标签库的机会很少,但是如果希望能够成为web方面的高级软件开发者或者想开发比较通用的框架,就需要大量的开发自定义的标签,所有的MVC框架,包括Struts2,SpringMVC以及JSF等都提供了非常丰富的自定义标签。
2)JSP2中开发自定义标签的步骤
1.开发自定义标签处理类
2.建立一个*.tld文件,对应一个自定义的标签库,每个标签库对应多个标签。
3.在JSP中使用自定义的标签。
3) 开发自定义标签处理类
新建一个继承javax.servlet.jsp.tagext.Simple.SimpleTagSupport的类HelloWorldTag
package formsdemo;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
/**
*
* @author HZ08744
* @category 标签处理类,继承父类SimpleTagSupport
*/
public class HelloWorldTag extends SimpleTagSupport{
@Override
public void doTag() throws JspException, IOException {
// 向页面输入hello
getJspContext().getOut().write("Hello");
}
}
如果标签类包含属性,每个属性都应该有getter和setter方法。
4)建立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">
<description>A tag library exercising SimpleTag handlers.</description>
<tlib-version>1.0</tlib-version>
<short-name>examples</short-name>
<uri>/demotag</uri>
<description>
A simple tab library for the examples
</description>
<tag>
<!-- 定义标签名 -->
<name>helloWorld</name>
<!-- 定义标签处理类 -->
<tag-class>formsdemo.HelloWorldTag</tag-class>
<!-- 定义标签体为空 -->
<body-content>empty</body-content>
</tag>
</taglib>
TLD文件语法:
1.taglib下面的三个属性
tlib-version:指定标签库的版本
short-name:指定标签库的短名
URI:这个属性非常的重要,标签库的唯一标志,在jsp经常使用这个属性来引入标签库
2.tag下面的几个属性
name:标签的名称
tag-class:标签处理类(全路径)
body-content:标签体的内容,一般都是empty。
5)使用标签库
建立helloWorld.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib uri="/demotag" prefix="mytag"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body bgcolor="#ffffc0">
<h2>下面显示的是自定义标签中的内容</h2>
<!-- 使用标签 ,其中mytag是标签前缀,根据taglib的编译指令,
mytag前缀将由http://www.crazyit.org/mytaglib的标签库处理 -->
<mytag:helloWorld/><BR>
</body>
</html>
可以使用自定义的标签<mytag:helloWorld/>
标签库是非常重要的技术,对于JSP初学者和普通开发人员,能够自己开发标签库的机会很少,但是如果希望能够成为web方面的高级软件开发者或者想开发比较通用的框架,就需要大量的开发自定义的标签,所有的MVC框架,包括Struts2,SpringMVC以及JSF等都提供了非常丰富的自定义标签。
2)JSP2中开发自定义标签的步骤
1.开发自定义标签处理类
2.建立一个*.tld文件,对应一个自定义的标签库,每个标签库对应多个标签。
3.在JSP中使用自定义的标签。
3) 开发自定义标签处理类
新建一个继承javax.servlet.jsp.tagext.Simple.SimpleTagSupport的类HelloWorldTag
package formsdemo;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
/**
*
* @author HZ08744
* @category 标签处理类,继承父类SimpleTagSupport
*/
public class HelloWorldTag extends SimpleTagSupport{
@Override
public void doTag() throws JspException, IOException {
// 向页面输入hello
getJspContext().getOut().write("Hello");
}
}
如果标签类包含属性,每个属性都应该有getter和setter方法。
4)建立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">
<description>A tag library exercising SimpleTag handlers.</description>
<tlib-version>1.0</tlib-version>
<short-name>examples</short-name>
<uri>/demotag</uri>
<description>
A simple tab library for the examples
</description>
<tag>
<!-- 定义标签名 -->
<name>helloWorld</name>
<!-- 定义标签处理类 -->
<tag-class>formsdemo.HelloWorldTag</tag-class>
<!-- 定义标签体为空 -->
<body-content>empty</body-content>
</tag>
</taglib>
TLD文件语法:
1.taglib下面的三个属性
tlib-version:指定标签库的版本
short-name:指定标签库的短名
URI:这个属性非常的重要,标签库的唯一标志,在jsp经常使用这个属性来引入标签库
2.tag下面的几个属性
name:标签的名称
tag-class:标签处理类(全路径)
body-content:标签体的内容,一般都是empty。
5)使用标签库
建立helloWorld.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib uri="/demotag" prefix="mytag"%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body bgcolor="#ffffc0">
<h2>下面显示的是自定义标签中的内容</h2>
<!-- 使用标签 ,其中mytag是标签前缀,根据taglib的编译指令,
mytag前缀将由http://www.crazyit.org/mytaglib的标签库处理 -->
<mytag:helloWorld/><BR>
</body>
</html>
可以使用自定义的标签<mytag:helloWorld/>