自定义JSP标签:(必须扩展javax.servlet.jsp.TagSupport或javax.servlet.jsp.BodyTagSupport)
1。步骤:
**创建标签的处理类
**创建标签的描述文件
**在JSP文件中引入标签
2。创建标签处理类
(1)JSP Tag API
方法:doStartTag():在JSP容器遇到标签的起始标志时调用,返回一个整数:Tag.SKIP_BODY,Tag.EVAL_BODY_INCLUDE
doEndTag():返回一个整数:Tag.SKIP_PAGE,Tag.EVAL_PAGE
setValue(String k,Object o)
getValue(String k)
removeValue(String k)
setPageContext(PageContext pc):设置PageContext对象,在doStartTag()和doEndTag()之前调用
setParent(Tag t):设置了该标签的上层标签的处理类,在doStartTag()和doEndTag()之前调用
getParent():返回上层标签的处理类
PageContext类提供了保存和访问web应用的共乡数据的方法setAttribute(String name,Object o,int scope)和getAttribute(String name,int scope) (scope的值类似PageContext.PAGE_SCOPE)
(2)<prefix:Mytag attribute="value">
...
</prefix:Mytag>
标签处理类就应该把这个属性作为成员变量,如:
private int attribute;
public void setAttribute(int value)
{
this.attribute=value;
}
public void getAttribute()
{
return attribute;
}
(3)
**创建静态文本文件(以key/value形式存放):
message.properties 文件:
hello.title = helloapp
hello.hello = Hello
login.title = helloapp
login.user = User Name
login.password = Password
login.submit = Submit
**在web应用启动时装载静态文本:
public void init(ServletConfig config)
throws ServletException {
super.init(config);
Properties ps=new Properties();
Properties ps_ch=new Properties();
try{
ServletContext context=config.getServletContext();
InputStream in=context.getResourceAsStream("/WEB-INF/messageresource.properties");
ps.load(in);
InputStream in_ch=context.getResourceAsStream("/WEB-INF/messageresource_ch.properties");
ps_ch.load(in_ch);
in.close();
in_ch.close();
context.setAttribute("ps",ps);
context.setAttribute("ps_ch",ps_ch);
}catch(Exception e){
e.printStackTrace();
}
}
*web.xml
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>mypack.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/dispatcher</url-pattern>
</servlet-mapping>
**创建MessageTag.java
package mypack;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.http.HttpSession;
import java.util.*;
import java.io.*;
public class MessageTag extends TagSupport
{
private String key=null;
public MessageTag() {
}
public String getKey(){
return this.key;
}
public void setKey(String key){
this.key=key;
}
// Method called when the closing hello tag is encountered
public int doEndTag() throws JspException {
try {
Properties ps=(Properties)pageContext.getAttribute("ps",pageContext.APPLICATION_SCOPE);
Properties ps_ch=(Properties)pageContext.getAttribute("ps_ch",pageContext.APPLICATION_SCOPE);
HttpSession session=pageContext.getSession();
String language=(String)session.getAttribute("language");
String message=null;
if(language!=null && language.equals("Chinese")){
message=(String)ps_ch.get(key);
message=new String(message.getBytes("ISO-8859-1"),"GB2312");
}
else
message=(String)ps.get(key);
pageContext.getOut().print(message);
}
catch (Exception e) {
throw new JspTagException(e.getMessage());
}
// We want to return SKIP_BODY because this Tag does not support
// a Tag Body
return SKIP_BODY;
}
public void release() {
super.release();
}
}
3.创建标签库描述文件:(mytaglib.tld)
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"
http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<!-- a tag library descriptor -->
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>mytaglib</shortname>
<uri>/mytaglib</uri>
<tag>
<name>message</name>
<tagclass>mypack.MessageTag</tagclass>
<bodycontent>empty</bodycontent>
<info>produce message by key</info>
<attribute>
<name>key</name>
<required>true</required>
</attribute>
</tag>
</taglib>
在web.xml加入:
<taglib>
<taglib-uri>/mytaglib</taglib-uri>
<taglib-location>/WEB-INF/mytaglib.tld</taglib-location>
</taglib>
4. 应用标签
<%@ taglib uri="/mytaglib" prefix="mm"%>
<mm:message key="hello.title"/>