Vietnam version
Today, I am going to discuss how to create custom JSP tag-libs. Because custom JSP tags are reused in many projects, creating custom JSP tags is always necessary. For examples: creating a tag-lib to show a product information in e-commerce projects, book information in a bookstore.
In this post, we are going to create a tag-lib which shows the information of a book. The book's information includes title, edition version, ISBN, authors, cover image, the number of pages, publisher,...
There are three components that you have to take care if you want to create a new tag-lib:
- a *.tld file
- Java classes (handler)
- some JSP files
TLD File
Firstly, *.tld file will declare tags and its attributes. So, how many tags and attributes? In above example, there are two tags: book and author tag. We are going to view the detail of these tags.
- book tag:
- title: the title of book (for example Introduce to Algorithms,..)
- edition: the edition number
- ISBN: the ISBN of book
- cover: the cover image (URL)
- numberOfPage: total pages of book
- publisherName: the name of publisher
- publisherURL: the URL or website address of a publisher.
- author tag:
- name: the name of the author
- institute: the work address of the author
- url: the person website of the author.
Following is the detail of *.tld file. That file named bookstore.tld and stored in WEB-INF folder.
bookstore.tld is a XML file. It defines the information of tags and their attributes. We are going explain some lines (Or direct here to get more details):
- Line 3rd: Define tag-lib tag with xml-namespace (xmlns), schema reference attribute.
- Line 4th: The version information of this file.
- Line 5th: The short name of tag-lib.
- Line 6th: The url of tag-lib. This element is used when JSP files include it
- Line 7th-67th: Define book tag
- Line 8th: The name of book tag ("book")
- Line 9th: The full class name of Java class. This class will handle data of this tag
- Line 10th: The type of content. In this case is JSP (Value set is {empty, JSP, scriptless, tagdependent}).
- Line 12nd-17th: Define title attribute of book tag.
- Line 14th: The name of attribute ("title")
- Line 15th: Wheather this attribute is necessary or not
- Line 16th: Runtime Value
- Line 17th: The data type of this attribute
- There are many properties and attributes, but I only care about the necessary things.
Handle classes
Now we will consider the handle classes. Each tag has a hanlde class, for example: book tag has BookTag class(com.blogspot.chingovan.taglib.bs.BookTag) and author tag has AuthorTag class (com.blogspot.chingovan.taglib.bs.AuthorTag). Let's consider BookTag class;
BookTag class inherits from BaseBookTag class.
We will consider two class. BaseBookTag class inherits from IncludeTag class (com.liferay.taglib.util.IncludeTag). This class has been declared some fields and utility methods. We only need to add the custom fields into BaseBookTag class. In this case, we added title, edition, ISBN, cover, numberOfPage, publisherNameand publisherURL field. Besides, we have three static fields. _ATTRIBUTE_NAMESPACE field is the unique name space, _END_PAGE and _START_PAGE are the paths which point the special JSP file.
Method doStartTag is fired at the first time and we have to register the namespace of this tag by call setAttributeNamespace(_ATTRIBUTE_NAMESPACE); Following are the setter and getter methods. Method getEndPage and getStartPage return the path to the start and end page. And the last, method setAttributes is used to register the value of fields.
JSP Files
Each tag has some JSP files like start.jsp, end.jsp, init.jsp,... Usually, init.jsp is used to declare, get the value of request object and include another. For example:
We will explain some lines:
- Line 10th: we include the default library of Liferay framework
- Line 13rd-34th: we get the value of the fields. Those values are used by another JSP fields.
Now let's consider start.jsp file.
At the first line, we will include the init.jsp file. Then, we can use the values which have passed from our applications. end.jsp file similar to start.jsp file.
Summary
Now, you have had your tag. The using this tag similar to the another tag (for examples: aui:select tags, liferay-ui:search-container tag,...) You can see my example here.
This is the entire portlet. Thanks for reading and good luck.
Không có nhận xét nào:
Đăng nhận xét