Download nhạc từ các trang nhạc lớn của Việt Nam

30 Tháng Ba 2006
@ 06:00
(Tác giả:Phạm Đức Hải)

<taglib:tutorial lesson="2">

In this part of the tutorial, we will create a Tag that accepts and displays a parameter.

1 Lesson 2, Your first parameterised Tag

    In this part of the tutorial, we will create a Tag that accepts and displays a parameter. We will continue to use the setup previously defined.

    As you might have noticed in lesson 1, even a simple Tag requires us to write a number of methods that we will be using over and over again. The natural thing to do would be to make a generic Tag that we can extend throughout this tutorial. But surprise! Sun has already done so for us. In the javax.servlet.jsp.tagext package, there is a class called TagSupport that looks like in figure 1 below:


    Figure 1: The TagSupport class.

    Apart from the methods that is required by the Tag interface, the TagSupport class holds some utility methods that we can take advantage of. Amongst these are:

    • findAncestorsWithClass() will go one step further of the getParent() method and traverse all parent Tags to this one to find the closest occurrence of a certain Tag.

    • getId() and setId() gives us ready-to-go methods for setting a parameter called Id for all Tags that extends the TagSupport class. Perfect for simple single parameter type Tags.

    We will now leave the TagSupport to get on with the tutorial. If you want to learn more or get a deeper understanding about the TagSupport, read the JSP API or the JSP 1.1 Specification.

2 Creating the Hello Tag

    We will now create a Tag called 'HelloTag' that extends the TagSupport class as shown in Figure 2 below.


    Figure 2: The HelloTag extends the TagSupport class.

    1. In the '/WEB-INF/classes/com/acme/tag/' directory, create a new class called 'HelloTag.java' with the following content:


      package com.acme.tag;


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


      public class HelloTag extends TagSupport
      {
      private String name="";

      Listing 1: Starting of the Tag.

      As you can see in the code given above, we declare that our HelloTag will extend the TagSupport class. The variable name above will hold an optional name that will be used later in the Tag.

    2. Add the constructor:


      public HelloTag()
      {
      super();
      }

      Listing 2: The constructor.

    3. Add the following method:


      public void setName(String name)
      {
      this.name=name;
      }

      Listing 3: Adding the setName method.

      The JSP container calls the method above if a name is submitted to the Tag. How then will the JSP container know what to set? Well, well go into that below.

    4. Now add the following method:


      public int doEndTag() throws javax.servlet.jsp.JspTagException
      {
      try
      {
      pageContext.getOut().write("Hello "+name+"!");
      }
      catch(java.io.IOException e)
      {
      throw new JspTagException("IO Error: " + e.getMessage());
      }
      return EVAL_PAGE;
      }
      }

      Listing 4: Overriding the doEndTag method.

      The method above will use the PageContext to get a JspWriter class. It will then use this Writer to write a message to the encapsuling JSP page.

      As the SupportTag has implemented a doStartTag() method that returns SKIP_BODY, we do not need to implement that or any other method that we don't want to override.

    5. Compile your Tag

    By now, your HelloTag.java should look like this

3 Writing the descriptor

    With the new Tag in place, we should add a descriptor of our new Tag to the Tags descriptor we created in the first lesson.

    1. Open your 'taglib.tld' from your '/WEB-INF' directory with your favourite editor.

    2. Add the following description for your new Tag:


      <tag>
      <name>hello</name>
      <tagclass>com.acme.tag.HelloTag</tagclass>
      <bodycontent>empty</bodycontent>
      <info>A Hello Tag</info>
      <attribute>
      <name>name</name>
      <required>false</required>
      <rtexprvalue>false</rtexprvalue>
      </attribute>
      </tag>

      Listing 5: Adding a Tag descriptor for the Tag.

    This will tell our JSP container that the new tag can accept an attribute called Name and that the Tags body should be empty. The <rtexprvalue> that is set to false will tell the Container that the name attribute will not be evaluated at runtime (i.e. can not be a dynamic value).

    Your taglib.tld should now look like this.

4 Creating a presentation

    In order to test our new Tag, we need to write a JSP page that uses it.

    1. Create a new file called 'hello.jsp' in your '/taglib-tutorial-web/' directory with the following content:


      <%@ taglib uri="mytags" prefix="mt" %>
      <HTML>
      <HEAD>
      <TITLE>Hello!</TITLE>
      </HEAD>
      <BODY BGCOLOR="#FFFFFF">
      <HR>
      <mt:hello name="foo"/>
      <HR>
      </BODY>
      </HTML>

      Listing 6: A sample JSP page.

      In the page above, we first tell the Container a reference to our Taglib descriptor. We then use the HelloTag by using its defined name ('hello') with a name attribute of "foo".

    2. Make sure that you store your file as 'hello.jsp' in your '/taglib-tutorial-web/' directory.

    By now, your 'hello.jsp' page should look like this.

5 Using your new Tag

    Its now time to test your new tag.

    1. Open the URL 'http://localhost/taglib/hello.jsp' in a normal web browser.

    Hopefully the result looks like this

    Continue with lesson 3, "Writing a BodyTag".

Copyright © 2005 IronFlare AB

Chuyên mục: Java

Referred by:
"java blog""code" (www.google.com.vn) [Referral]
ví dụ taglib (www.google.com.vn) [Referral]
get directory java content ubuntu (www.google.com.vn) [Referral]
TagSupport class (www.google.com.vn) [Referral]
Taglib .tag (www.google.com.vn) [Referral]
import taglib.tld (www.google.com.vn) [Referral]
taglib parameterised (www.google.com.br) [Referral]
learn to build taglib with java (www.google.com.vn) [Referral]
hello.jsp (www.google.com.vn) [Referral]
hello taglib (www.google.com.vn) [Referral]
"taglib tutorial" (www.google.com.vn) [Referral]
lời bài hát leave getout (www.google.com.vn) [Referral]
taglib in jsp with parameter (www.google.com.vn) [Referral]
taglib in jsp with parametter (www.google.com.vn) [Referral]
blog jsp (www.google.com.vn) [Referral]
jsp taglib + download (www.google.com.vn) [Referral]
how to import javax.servlet (www.google.com.vn) [Referral]
Taglib (www.google.com.vn) [Referral]
jsp: use taglib (www.google.com.vn) [Referral]
how to built a web browser with java (www.google.com.vn) [Referral]
TagSupport taglib (www.google.com.vn) [Referral]
simple taglib sample (www.google.com.vn) [Referral]
"TAG attribute" (www.google.com.vn) [Referral]
build taglib (www.google.com.vn) [Referral]
taglib 2 (www.google.ca) [Referral]
taglib jsp (www.google.com.vn) [Referral]
taglib + jsp (www.google.com.vn) [Referral]
tutorial taglib java (www.google.com.vn) [Referral]
ant build taglib (www.google.lt) [Referral]
"taglib java" (www.google.com.vn) [Referral]
taglib (www.google.com.vn) [Referral]
create taglib (www.google.com.vn) [Referral]
taglib (www.google.com.vn) [Referral]
taglib trong jsp (www.google.com.vn) [Referral]
"taglib"+"TagSupport"+"download" (www.google.com) [Referral]
tag trong JSP (www.google.com.vn) [Referral]
how import taglib (www.google.com.vn) [Referral]
SupportTag source code (www.google.com) [Referral]
extend jsp taglib (www.google.com.vn) [Referral]
first taglib (www.google.com.vn) [Referral]
taglib,jsp (www.google.com.vn) [Referral]
first <taglib> (www.google.com) [Referral]
add taglib jsp (www.google.com.vn) [Referral]
su dung taglib,JSP (www.google.com.vn) [Referral]
use taglib, JSP (www.google.com.vn) [Referral]
ajax taglib (www.google.com.vn) [Referral]
su dung taglib ,JSP (www.google.com.vn) [Referral]
ajax taglib (www.google.com.vn) [Referral]
TagSupport (www.google.com.vn) [Referral]
EMPTY TAG jsp (www.google.com.vn) [Referral]
taglib (www.google.com.vn) [Referral]
vi du ve taglib (www.google.com.vn) [Referral]
blog buid (www.google.com.vn) [Referral]
build botnet with java (www.google.com.vn) [Referral]
taglib demo + source code (www.google.com.vn) [Referral]
taglib (www.google.com.vn) [Referral]
hold me (www.google.com.vn) [Referral]
the package implements the tag interface java.servelet. (www.google.com.vn) [Referral]
tại taglib trong jsp (www.google.com.vn) [Referral]
FirstTaglib example with jsp (www.google.com) [Referral]
how to create taglib (www.google.com.vn) [Referral]
taglib (www.google.com.vn) [Referral]
taglib (www.google.com.vn) [Referral]
taglib jsp tutorial (www.google.com.vn) [Referral]
how to creat taglib + java (www.google.com.vn) [Referral]
"ví dụ taglib" (www.google.com.vn) [Referral]
java taglib tutorial (www.google.com.vn) [Referral]
build taglib include image (www.google.com.vn) [Referral]
import taglib (www.google.com.vn) [Referral]
taglib sample (www.google.com.vn) [Referral]
built taglib (www.google.com.vn) [Referral]
head + taglib (www.google.com.vn) [Referral]
package + "taglib" (www.google.com.vn) [Referral]

Họ tên
E-mail
(sẽ hiển thị gravatar theo email của bạn)
Trang chủ
Bộ gõ Tắt TELEX VNI
Ý kiến (Không dùng HTML)  

Nhập mã kiểm tra :(chống xì-pum ý mà):