Develop custom tags using the simpletagsupport provided by JSP 2

Source: Internet
Author: User
Tags html page tld

Custom tag library is not JSP 2, JSP version 1.1 has added a custom tag library specification, custom tag library is a very good performance layer component technology. By using a custom tag library, you can encapsulate complex functionality in a simple label.

Why use a custom label? The main purpose is to replace the ugly JSP script. Inserting a JSP script into an HTML page has the following disadvantages:

JSP scripts are ugly and difficult to read.

The JSP script and the HTML code are mixed, the maintenance cost is high.

Embedding JSP scripts in HTML pages makes it difficult for artists to participate in development.

For the above three points, we need a label that can be used on a page that has the same syntax as an HTML tag, but with the ability to complete a JSP script--a label that is a JSP custom label.

The development of custom tag libraries in the JSP1.1 specification is complex, and the JSP 2 specification simplifies the development of tag libraries, and the development of tag libraries in JSP 2 requires only the following steps:

Develop custom label processing classes;

Create a *.tld file, each *.tld file corresponding to a tag library, each tag library corresponding to multiple tags;

Use custom labels in your JSP files.

Developing Custom Label Classes

Tag Library and actual development

Tag libraries are a very important technology, and often beginners and ordinary developers have little chance of developing their own tag libraries, but if you want to be a senior programmer, or want to develop a generic framework, you need to develop custom tags. All the MVC frameworks, such as Struts 2, SPRINGMVC, JSF, all provide rich custom tags.

When we use a simple label on a JSP page, the bottom layer is actually supported by the tag processing class, which enables the team to better collaborate and develop (allowing the artist to participate in the development of JSP pages) by encapsulating complex functions using simple tags.

The early JSP custom label class development process is slightly more complex, but JSP 2 has simplified the process, as long as the custom tag class must inherit a parent class: Javax.servlet.jsp.tagext.SimpleTagSupport, in addition, JSP Custom Label classes also have the following requirements.

If the Label class contains attributes, each property has a corresponding getter and setter method.

Rewrite the Dotag () method, which is responsible for generating page content.

The following is a simple custom label that is responsible for outputting HelloWorld on the page.

// 标签处理类,继承 SimpleTagSupport 父类
public class HelloWorldTag extends SimpleTagSupport
{
   // 重写 doTag 方法,该方法在标签结束生成页面内容
   public void doTag()throws JspException,
     IOException
   {
     // 获取页面输出流,并输出字符串
     getJspContext().getOut().write("Hello World");
   }
}

The above label processing class is very simple, it inherits the Simpletagsupport parent class, and overrides the Dotag () method, while the Dotag () method is responsible for outputting the page content. The label has no attributes, so there is no need to provide a setter and getter method.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.