jsp_tag 学习笔记

   学习地址:http://java.sun.com/developer/technicalArticles/xml/WebAppDev3/ 

最近要开发java的CMS 系统 只好逼着自己学了,很重要的一部分是自定义标签。研究了一下,还不算太难。

总述:包括 Bodyless Tags 和 Tags with a Body。

1. Develop the Tag Handler

Sample 1: Tag.java

public interface Tag {
   public final static int SKIP_BODY = 0;
   public final static int EVAL_BODY_INCLUDE = 1;
   public final static int SKIP_PAGE = 5;
   public final static int EVAL_PAGE = 6;

   void setPageContext(PageContext pageContext);
   void setParent(Tag parent);
   Tag getParent();
   int doStartTag() throws JspException;
   int doEndTag() throws JspException;
   void release();
}

实现接口或者继承一个已有的类TagSupport,

Sample 2: HelloTag.java

package tags;

import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

public class HelloTag implements Tag {
   private PageContext pageContext;
   private Tag parent;

   public HelloTag() {
      super();
   }

   public int doStartTag() throws JspException {
      try {
         pageContext.getOut().print(
         "This is my first tag!");
      } catch (IOException ioe) {
         throw new JspException("Error: 
         IOException while writing to client" 
         + ioe.getMessage());
      }
      return SKIP_BODY;
   }

   public int doEndTag() throws JspException {
      return SKIP_PAGE;
   }

   public void release() {
   }

   public void setPageContext(PageContext 
   pageContext) {
      this.pageContext = pageContext;
   }

   public void setParent(Tag parent) {
      this.parent = parent;
   }

   public Tag getParent() {
      return parent;
   }
}

2. Create a Tag Library Descriptor

Sample 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>first</shortname>
   <uri></uri>
   <info>A simple tab library for the 
   examples</info>

  <tag>
    <name>hello</name>
    <tagclass>tags.HelloTag</tagclass>
    <bodycontent>empty</bodycontent>
    <info>Say Hi</info>
  </tag>         
</taglib>
3. Test the Tag

The final step is to test the tag we have developed. In order to use the tag, we have to reference it, and this can be done in three ways:

  1. Reference the tag library descriptor of an unpacked tag library. For example:

     

    <@ taglib uri="/WEB-INF/jsp/mytaglib.tld" 
    prefix="first" %>
    

     

  2. Reference a JAR file containing a tag library. For example:

     

    <@ taglib uri="/WEB-INF/myJARfile.jar" 
    prefix='first" %>
    

     

  3. Define a reference to the tag library descriptor from the web-application descriptor (web.xml) and define a short name to reference the tag library from the JSP. To do this, open the file: c:/tomcat/webapps/examples/web-inf/web.xml and add the following lines before the end line, which is <web-app>:

        <taglib>
           <taglib-uri>mytags</taglib-uri>
           <taglib-location>/WEB-INF/jsp/
           mytaglib.tld</taglib-location>
        </taglib>
    

Now, write a JSP and use the first syntax. Sample 4 shows an example:

Sample 4: Hello.jsp

<%@ taglib uri="/WEB-INF/jsp/mytaglib.tld"
 prefix="first" %>
<HTML>
<HEAD>
<TITLE>Hello Tag</TITLE>
</HEAD>

<BODY bgcolor="#ffffcc">

<B>My first tag prints</B>:

<first:hello/>

</BODY>
</HTML>
先写这么多了 具体的看教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本篇笔记主要介绍JSP中的EL表达式和JSTL标签库。 一、EL表达式(Expression Language) EL表达式是JSP中一个非常重要的特性,它可以在JSP页面中方便地访问JavaBean中的属性和方法,同时还可以进行一些简单的运算操作。EL表达式的语法非常简单,使用${}表示。例如,我们可以通过${user.name}访问一个名为user的JavaBean中的name属性。 EL表达式支持许多运算符,如算数运算符(+、-、*、/、%)、比较运算符(>、<、>=、<=、==、!=)、逻辑运算符(&&、||、!)等等,还支持一些特殊的运算符,如“empty”用于判断一个对象是否为空,如${empty user}表示判断user对象是否为空。 二、JSTL标签库(JSP Standard Tag Library) JSTL是JSP标准标签库,提供了一组标签和函数,可以方便地实现一些常见的任务,如迭代、条件判断、格式化等等。JSTL标签库包含五个核心标签库: 1.核心标签库(Core Tag Library):提供了一些基本的标签,如if、forEach等等。 2.格式化标签库(Formatting Tag Library):提供了一些标签,用于格式化日期、数字等等。 3.SQL标签库(SQL Tag Library):提供了一些标签,用于执行SQL查询和更新操作。 4.XML标签库(XML Tag Library):提供了一些标签,用于处理XML文档。 5.函数标签库(Functions Tag Library):提供了一些函数,可以用于字符串处理、日期处理等等。 使用JSTL标签库需要在JSP页面中引入相应的标签库,例如: ``` <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> ``` 其中,prefix表示标签库的前缀,uri表示标签库的统一资源标识符(URI)。 使用JSTL标签库的语法和EL表达式类似,也是使用${}来引用JavaBean中的属性和方法,同时可以使用JSTL标签来实现一些复杂的逻辑。例如,可以使用<c:if>标签来实现条件判断,如: ``` <c:if test="${user.age > 18}"> <p>成年人</p> </c:if> ``` 以上就是本篇笔记的全部内容,EL表达式和JSTL标签库是JSP中非常重要的特性,掌握它们可以让我们更加方便地开发JSP应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值