自定义标签库
来源:互联网 发布:皮鞋销售软件 编辑:程序博客网 时间:2024/06/05 20:17
自定义标签处理类一般都继承TagSupport或BodyTagSupport
1.先看一个普通的最简单的自定义标签程序。
A:创建标签的处理类
B:创建标签库描述文件
C.在JSP文件中引入标签库,然后插入标签
jsp里有两种引用方式:
<%@ taglib uri="/WEB-INF/hello.tld" prefix="aa" %>或者
<%@ taglib uri="/hellotag" prefix="aa" %>
输出结果为:hello
2.自定义标签库引用资源文件
A:首先在应用程序根目录下建立一个资源文件比如叫:helloworld.properties,内容为:
B:建立一个Servlet,在启动时加载该Servlet,在init方法里读取helloworld.properties文件内容。
C:创建标签库的处理类:
D:创建自定义标签的描述文件
E:jsp里引用
1.先看一个普通的最简单的自定义标签程序。
A:创建标签的处理类
- package com.test.tag;
- import java.io.IOException;
- import javax.servlet.jsp.JspException;
- import javax.servlet.jsp.tagext.Tag;
- import javax.servlet.jsp.tagext.TagSupport;
- @SuppressWarnings("serial")
- public class HelloTag extends TagSupport {
- //doStartTag返回的都是BODY相关,doEndTag返回的都是PAGE相关
- @Override
- public int doStartTag() throws JspException {
- try {
- //通过PageContext对象的getOut()方法获得JspWriter对象输出内容
- this.pageContext.getOut().write("hello");
- } catch (IOException e) {
- e.printStackTrace();
- }
- return Tag.EVAL_BODY_INCLUDE;
- }
- }
package com.test.tag;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.Tag;import javax.servlet.jsp.tagext.TagSupport;@SuppressWarnings("serial")public class HelloTag extends TagSupport {//doStartTag返回的都是BODY相关,doEndTag返回的都是PAGE相关@Overridepublic int doStartTag() throws JspException {try {//通过PageContext对象的getOut()方法获得JspWriter对象输出内容this.pageContext.getOut().write("hello");} catch (IOException e) {e.printStackTrace();}return Tag.EVAL_BODY_INCLUDE;}}
B:创建标签库描述文件
- <?xml version="1.0" encoding="ISO-8859-1" ?>
- <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
- <taglib>
- <tlib-version>1.0</tlib-version>
- <jsp-version>1.2</jsp-version>
- <short-name>hello taglib test</short-name>
- <uri>/hellotag</uri>
- <tag>
- <!--Tag名字 -->
- <name>hello</name>
- <!-- 该Tag对应的处理类 -->
- <tag-class>com.test.tag.HelloTag</tag-class>
- <!-- 该 Tag的body内容为空 -->
- <body-content>empty</body-content>
- </tag>
- </taglib>
<?xml version="1.0" encoding="ISO-8859-1" ?><!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"><taglib><tlib-version>1.0</tlib-version><jsp-version>1.2</jsp-version><short-name>hello taglib test</short-name><uri>/hellotag</uri><tag> <!--Tag名字 --><name>hello</name><!-- 该Tag对应的处理类 --><tag-class>com.test.tag.HelloTag</tag-class><!-- 该 Tag的body内容为空 --><body-content>empty</body-content></tag></taglib>
C.在JSP文件中引入标签库,然后插入标签
jsp里有两种引用方式:
<%@ taglib uri="/WEB-INF/hello.tld" prefix="aa" %>或者
<%@ taglib uri="/hellotag" prefix="aa" %>
- <%@ page language="java" pageEncoding="UTF-8"%>
- <%@ taglib uri="/hellotag" prefix="aa" %>
- <html>
- <body>
- <aa:hello/>
- </body>
- </html>
<%@ page language="java" pageEncoding="UTF-8"%><%@ taglib uri="/hellotag" prefix="aa" %><html> <body> <aa:hello/> </body></html>
输出结果为:hello
2.自定义标签库引用资源文件
A:首先在应用程序根目录下建立一个资源文件比如叫:helloworld.properties,内容为:
- hello.title=tagTitle
- hello.content=tagContent
hello.title=tagTitlehello.content=tagContent
B:建立一个Servlet,在启动时加载该Servlet,在init方法里读取helloworld.properties文件内容。
- package com.test.tag;
- import java.io.InputStream;
- import java.util.Properties;
- import javax.servlet.ServletConfig;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- @SuppressWarnings("serial")
- public class TagServlet extends HttpServlet {
- public void init() throws ServletException {
- System.out.println("init not param");
- }
- //init(ServletConfig config)存在,则init()方法则不会执行。如果init(ServletConfig config)不存在,则init()方法会执行
- @Override
- public void init(ServletConfig config) throws ServletException {
- try{
- System.out.println("init have param");
- Properties p = new Properties();
- //通过ServletContext读取资源文件
- ServletContext context = config.getServletContext();
- InputStream in = context.getResourceAsStream("/helloworld.properties");
- p.load(in);
- in.close();
- //设置属性key-value到context中
- context.setAttribute("prop", p);
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }
package com.test.tag;import java.io.InputStream;import java.util.Properties;import javax.servlet.ServletConfig;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;@SuppressWarnings("serial")public class TagServlet extends HttpServlet {public void init() throws ServletException {System.out.println("init not param");} //init(ServletConfig config)存在,则init()方法则不会执行。如果init(ServletConfig config)不存在,则init()方法会执行@Overridepublic void init(ServletConfig config) throws ServletException {try{System.out.println("init have param");Properties p = new Properties();//通过ServletContext读取资源文件ServletContext context = config.getServletContext();InputStream in = context.getResourceAsStream("/helloworld.properties");p.load(in);in.close();//设置属性key-value到context中context.setAttribute("prop", p);}catch(Exception e){e.printStackTrace();}}}
C:创建标签库的处理类:
- package com.test.tag;
- import java.util.Properties;
- import javax.servlet.jsp.JspException;
- import javax.servlet.jsp.PageContext;
- import javax.servlet.jsp.tagext.Tag;
- import javax.servlet.jsp.tagext.TagSupport;
- @SuppressWarnings("serial")
- public class HelloTagByResource extends TagSupport {
- private String title;
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- //在doStartTag和doEndTag里写都可以。不要误认为:"遇到标签的起始标记时,还没有解析到其属性title,所以放在doStartTag里写会报错"
- @SuppressWarnings("static-access")
- @Override
- public int doStartTag() throws JspException {
- try{
- // Properties p = (Properties)this.pageContext.getAttribute("ps",this.pageContext.APPLICATION_SCOPE);
- Properties p = (Properties)this.pageContext.getAttribute("ps",PageContext.APPLICATION_SCOPE);
- String str = p.getProperty(title);
- this.pageContext.getOut().print(str);
- }catch(Exception e){
- e.printStackTrace();
- }
- return Tag.EVAL_BODY_INCLUDE;
- }
- // @Override
- // public int doEndTag() throws JspException {
- // try{
- //// Properties p = (Properties)this.pageContext.getAttribute("ps",this.pageContext.APPLICATION_SCOPE);
- // Properties p = (Properties)this.pageContext.getAttribute("ps",PageContext.APPLICATION_SCOPE);
- // String str = p.getProperty(title);
- // this.pageContext.getOut().print(str);
- // }catch(Exception e){
- // e.printStackTrace();
- // }
- // return Tag.EVAL_PAGE;
- // }
- }
package com.test.tag;import java.util.Properties;import javax.servlet.jsp.JspException;import javax.servlet.jsp.PageContext;import javax.servlet.jsp.tagext.Tag;import javax.servlet.jsp.tagext.TagSupport;@SuppressWarnings("serial")public class HelloTagByResource extends TagSupport {private String title;public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}//在doStartTag和doEndTag里写都可以。不要误认为:"遇到标签的起始标记时,还没有解析到其属性title,所以放在doStartTag里写会报错"@SuppressWarnings("static-access")@Overridepublic int doStartTag() throws JspException {try{//Properties p = (Properties)this.pageContext.getAttribute("ps",this.pageContext.APPLICATION_SCOPE);Properties p = (Properties)this.pageContext.getAttribute("ps",PageContext.APPLICATION_SCOPE);String str = p.getProperty(title);this.pageContext.getOut().print(str);}catch(Exception e){e.printStackTrace();}return Tag.EVAL_BODY_INCLUDE;}//@Override//public int doEndTag() throws JspException {//try{////Properties p = (Properties)this.pageContext.getAttribute("ps",this.pageContext.APPLICATION_SCOPE);//Properties p = (Properties)this.pageContext.getAttribute("ps",PageContext.APPLICATION_SCOPE);//String str = p.getProperty(title);//this.pageContext.getOut().print(str);//}catch(Exception e){//e.printStackTrace();//}//return Tag.EVAL_PAGE;//}}
D:创建自定义标签的描述文件
- <?xml version="1.0" encoding="ISO-8859-1" ?>
- <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
- <taglib>
- <tlib-version>1.0</tlib-version>
- <jsp-version>1.2</jsp-version>
- <short-name>hello taglib test</short-name>
- <uri>/hellotag</uri>
- <tag>
- <!--Tag名字 -->
- <name>hello</name>
- <!-- 该Tag对应的处理类 -->
- <tag-class>com.test.tag.HelloTag</tag-class>
- <!-- 该 Tag的body内容为空 -->
- <body-content>empty</body-content>
- </tag>
- <tag>
- <!--Tag名字 -->
- <name>message</name>
- <!-- 该Tag对应的处理类 -->
- <tag-class>com.test.tag.HelloTagByResource</tag-class>
- <!-- 该 Tag的body内容为空 -->
- <body-content>empty</body-content>
- <!-- 属性定义 -->
- <attribute>
- <name>title</name>
- <required>true</required>
- </attribute>
- </tag>
- </taglib>
<?xml version="1.0" encoding="ISO-8859-1" ?><!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"><taglib><tlib-version>1.0</tlib-version><jsp-version>1.2</jsp-version><short-name>hello taglib test</short-name><uri>/hellotag</uri><tag> <!--Tag名字 --><name>hello</name><!-- 该Tag对应的处理类 --><tag-class>com.test.tag.HelloTag</tag-class><!-- 该 Tag的body内容为空 --><body-content>empty</body-content></tag><tag> <!--Tag名字 --><name>message</name><!-- 该Tag对应的处理类 --><tag-class>com.test.tag.HelloTagByResource</tag-class><!-- 该 Tag的body内容为空 --><body-content>empty</body-content><!-- 属性定义 --><attribute><name>title</name><required>true</required></attribute></tag></taglib>
E:jsp里引用
- <%@ page language="java" pageEncoding="UTF-8"%>
- <%@ taglib uri="/hellotag" prefix="aa" %>
- <html>
- <body>
- <aa:message title="hello.title"/><br/>
- <aa:message title="hello.content"/>
- </body>
- </html>
- 自定义标签库 循环标签
- JSTL标签库,自定义标签
- JSP 自定义标签库
- 自定义标签库
- 自定义jstl标签库
- Jsp 自定义标签库
- 自定义标签库
- 自定义标签库
- 自定义标签库开发
- 自定义标签库开发
- 自定义标签库开发
- 自定义标签库开发
- JSP自定义标签库
- 自定义标签库-demo
- 打包自定义标签库
- 自定义标签库
- JSP自定义标签库
- JSP自定义标签库
- 解决php中Cannot send session cache limiter 的问题的方法
- Find table of Invoice History
- gmtime ()函数
- C#之Xml文件特定节点的读取
- localtime ()函数
- 自定义标签库
- 将屏幕坐标转换成客户区坐标
- Flex中数据绑定
- mingoe --实用的联系人管理网站
- Microsoft Visual C++ Guide(在window下用vc编译wxWidget)
- 在shell中给当前的时间增加30秒的例子。
- ASP.NET Master Page中的title
- P1201高低位交换解题报告
- 万能makefile