JSP自定义标签入门1.0
程序员文章站
2022-06-08 23:09:57
...
JSP 自定义标签
自定义标签是用户定义的JSP语言元素。当JSP页面包含一个自定义标签时将被转化为servlet,标签转化为对被称为tag handler的对象的操作,即当servlet执行时Web container调用那些操作。JSP标签扩展可以让你创建新的标签并且可以直接插入到一个JSP页面。 JSP 2.0规范中引入Simple Tag Handlers来编写这些自定义标记。
可以继承SimpleTagSupport类并重写的doTag()方法来开发一个最简单的自定义标签。
接下来,我们想创建一个自定义标签叫作<ex:Hello>,标签格式为:
创建"Hello"标签
<ex:Hello />要创建自定义的JSP标签,你首先必须创建处理标签的Java类。所以,让我们创建一个HelloTag类,如下所示:
package Demo;
import java.io.IOException;
import java.io.StringWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class HelloTag extends SimpleTagSupport{
StringWriter sw = new StringWriter();
@Override
public void doTag() throws JspException, IOException {
JspWriter out = getJspContext().getOut();
out.print("你好林佳炎");
}
}
标签处理类创建好之后 在WEB-INF下创建tld文件
在 Eclipse 相应工程中右键单击 WEB-INF 目录弹出选项框,依次选:->New->Other->XML->XML File->next->要取的文件名.tld
->next->Create XML File from an XML schema file
->next->Select XML Catalog entry
选择 http://xmlns.jcp.org/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd 这一项,点击 Finish 即可。
以下是tld代码
<?xml version="1.0" encoding="UTF-8"?>
<j2ee:taglib version="2.0" xmlns:j2ee="http://java.sun.com/xml/ns/j2ee" xmlns:xml="http://www.w3.org/XML/1998/namespace" 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 ">
<j2ee:tlib-version>0.0</j2ee:tlib-version>
<j2ee:short-name>Example</j2ee:short-name>
<j2ee:tag>
<j2ee:name>Hello</j2ee:name>
<j2ee:tag-class>Demo.HelloTag</j2ee:tag-class>
<j2ee:body-content>empty</j2ee:body-content>
</j2ee:tag>
</j2ee:taglib>
接下来,我们就可以在JSP文件中使用Hello标签
<?xml version="1.0" encoding="UTF-8"?>
<j2ee:taglib version="2.0" xmlns:j2ee="http://java.sun.com/xml/ns/j2ee" xmlns:xml="http://www.w3.org/XML/1998/namespace" 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 ">
<j2ee:tlib-version>0.0</j2ee:tlib-version>
<j2ee:short-name>Example</j2ee:short-name>
<j2ee:tag>
<j2ee:name>Hello</j2ee:name>
<j2ee:tag-class>Demo.HelloTag</j2ee:tag-class>
<j2ee:body-content>empty</j2ee:body-content>
</j2ee:tag>
</j2ee:taglib>
输出结果为 你好林佳炎
访问标签体
你可以像标准标签库一样在标签中包含消息内容。如我们要在我们自定义的Hello中包含内容,格式如下:
<ex:Hello>
This is message body
</ex:Hello>
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;
public class HelloTag extends SimpleTagSupport {
StringWriter sw = new StringWriter();
public void doTag()
throws JspException, IOException
{
getJspBody().invoke(sw);
getJspContext().getOut().println(sw.toString());
}
}
接下来修改tld文件
<?xml version="1.0" encoding="UTF-8"?>
<j2ee:taglib version="2.0" xmlns:j2ee="http://java.sun.com/xml/ns/j2ee" xmlns:xml="http://www.w3.org/XML/1998/namespace" 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 ">
<j2ee:tlib-version>0.0</j2ee:tlib-version>
<j2ee:short-name>Example</j2ee:short-name>
<j2ee:tag>
<j2ee:name>Hello</j2ee:name>
<j2ee:tag-class>Demo.HelloTag</j2ee:tag-class>
<j2ee:body-content>scriptless</j2ee:body-content>
</j2ee:tag>
</j2ee:taglib>
把empty改为scriptless
现在我们可以在JSP使用修改后的标签,如下所示:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="ex" uri="/WEB-INF/hellotag.tld" %>
<!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>Insert title here</title>
</head>
<body>
<h1>666</h1>
<ex:Hello>
你好啊你好啊
</ex:Hello>
</body>
</html>
以上程序输出结果如下所示:
你好啊你好啊