码迷,mamicode.com
首页 > Web开发 > 详细

Jsp:taglib实现

时间:2014-05-04 19:31:11      阅读:441      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   tar   

web应用的结构:

          web.xml

          classes   diegoyun OutputTag.class

    WEB-INF  src    diegoyun OutputTag.java

mytag       tlds   diego.tld

    tag.jsp

细节:

web.xml:

bubuko.com,布布扣
1 <?xml version="1.0" encoding="GBK"?>
2 <web-app xmlns="http://java.sun.com/xml/ns/javaee"
3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
5     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
6     version="3.0">
7 </web-app>
bubuko.com,布布扣

OutputTag.java:

bubuko.com,布布扣
 1 package diegoyun;
 2 import javax.servlet.jsp.JspException;
 3 import javax.servlet.jsp.JspWriter;
 4 import javax.servlet.jsp.tagext.TagSupport;
 5 
 6 public class OutputTag extends TagSupport
 7 {
 8  private String name=null; 
 9  public void setName(String name)
10  {
11   this.name = name;
12  }
13  
14  public int doStartTag() throws JspException{
15   try
16   {
17    JspWriter out = pageContext.getOut();
18    out.print("Hello! " + name);
19   }
20   catch (Exception e)
21   {   
22    throw new JspException(e);
23   }
24   return EVAL_PAGE;
25  }
26 }
bubuko.com,布布扣

diego.tld:

bubuko.com,布布扣
 1 <?xml version="1.0" encoding="GBK" ?>
 2 
 3 <taglib xmlns="http://java.sun.com/xml/ns/j2ee"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"
 6     version="2.0">
 7  <tlib-version>1.0</tlib-version>
 8  <short-name>diego</short-name>  
 9  <!--OutputTag-->
10  <tag>
11   <name>out</name>
12   <tag-class>diegoyun.OutputTag</tag-class>
13   <body-content>empty</body-content>
14   <attribute>
15    <name>name</name>
16    <required>false</required>
17    <rtexprvalue>false</rtexprvalue>
18   </attribute>
19  </tag>
20 </taglib>
bubuko.com,布布扣

tag.jsp:

bubuko.com,布布扣
 1 <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
 2 
 3 <%@ taglib uri="/WEB-INF/tlds/diego.tld" prefix="diego"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 5     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 6 <html xmlns="http://www.w3.org/1999/xhtml">
 7 <body>
 8 Test Tag:
 9 <diego:out name="diegoyun"/>
10 </body>
11 </html>
bubuko.com,布布扣

 

Jsp:taglib实现,布布扣,bubuko.com

Jsp:taglib实现

标签:style   blog   class   code   java   tar   

原文地址:http://www.cnblogs.com/wanghui390/p/3706319.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!