Use of the HTML Tag library in struts (Part 1)

Source: Internet
Author: User
Tags tld
Struts provides a powerful HTML Tag library to help you build Web applications.
By Budi kurniawan
To make development easier and faster, Struts provides five tag libraries similar to other JSP tag libraries: HTML, Bean, logic, template, and nested .. The first part describes how to configure a struts application to use this tag Library and the first group of tags. The second part describes the second group of tags;

The HTML Tag library is mainly used to display HTML elements. If this method is not used, you must use HTML tags to specify them. On the surface, this tag library is very simple. But through in-depth research, we will find it very powerful. For example, it can generate JavaScript For key content in a specific input element of an HTML form, or generate scripts for client input validation. In addition, you can use it to handle errors through a line of code. However, before using this tag library for your struts application, you need to make some simple preparations.

Configure the application to use the tag Library
Before using the struts HTML Tag library, you need to configure a struts application in three steps.

1. register the tag library in the deployment descriptor (Web. xml file): <taglib>
<Taglib-Uri>/WEB-INF/struts-html.tld </taglib-Uri>
<Taglib-location>/WEB-INF/struts-html.tld </taglib-location>
</Taglib>

 
The above code tells the servlet container about the struts HTML Tag library and where to find the TLD file of the tag library.

2. Be sure to copy the struts-html.tld file to the WEB-INF directory. You don't have to worry about tag library files because they are already included in struts. Jar files.

3. Insert the following <taglib> specifier in each JSP page that uses the tag Library: <% @ taglib uri =/"/WEB-INF/struts-html.tld /"
Prefix =/"html/" %>
 

In addition, you can also use struts-html.tld files to learn the labels it supports and what attributes each label can carry.

It is usually easy to use a custom tag Library: Just follow the steps described above. However, some tag libraries contain principles that make it easier for us to use these tag libraries. Struts HTML Tag library is one of them. Some labels are simple and easy to use. However, some labels depend on other labels or other elements in the struts application.

I divided the tags in the HTML Tag library into two categories: simple and easy to use tags, which I call "independent tags ", the second type is a label that must be used through a form label. The second type of labels is called form-related labels. This article describes independent labels. In the second part of this series, I will describe the labels related to form.

The HTML Tag library contains several independent tags that are easy to use. The following describes several important labels.

<HTML> tag
<HTML> tags are the easiest tags in the HTML Tag library. It has two attributes: locale and XHTML, both of which are not required. The following code selects a JSP page using the <HTML> tag: <% @ taglib uri =/"/WEB-INF/struts-html.tld/" prefix =/"html/" %>
<HTML: HTML locale =/"True/">
<Head>
<Title> welcome </title>
</Head>

<Body>
Hello world!
</Body>
</Html: HTML>

Note that the <HTML: HTML> label is used instead of the standard <HTML> element. The first HTML indicates the prefix, and the second HTML indicates the tag itself. We also use the locale attribute. The JSP page is displayed as follows: <HTML lang =/"EN/">
<Head>
<Title> welcome </title>
</Head>
<Body>
Hello world!
</Body>
</Html>

Note: The Locale attribute in <HTML: HTML locale = "true"> is converted to Lang = "en" on the generated HTML page ". The generated result depends on the locale of the server on which the struts application is located. If you deploy the application to a server with different locale, you do not need to change the code. Locale is automatically adjusted.

<Base> tag
<Base> the tag displays an HTML element and uses an href attribute to indicate the absolute position of the contained JSP page. This label is valid only when it is embedded in a head label. For example, this JSP page: <% @ taglib uri =/"/WEB-INF/struts-html.tld/" prefix =/"html/" %>
<HTML: HTML locale =/"True/">
<Head>
<Title> welcome </title>
<HTML: base/>
</Head>
<Body>
Hello world!
</Body>
</Html: HTML>

Is converted to the following format: <HTML lang =/"EN/">
<Head>
<Title> welcome </title>
<Base href =/"http://www.domain.com/myStrutsApp/testing.jsp/">
</Head>
<Body>
Hello world!
</Body>
</Html>

 
label
A tag is used to generate an HTML tag. Its most important attributes are: page: the path of the image file to the module. a diagonal line must be included in the front.
· Height: The image height.
· Width: The image width.
· Alt: Text displayed if no image is found.

Example: <HTML: IMG page =/"/logo.gif/" Height =/"50 /"
Width =/"200/" alt =/"company logo/"/>

Will be converted to: Width =/"200/" alt =/"company logo/">

<Link> tag
<Link> the tag generates a hyperlink. For example, Tag: <HTML: link page =/"/index.html/"> click
Here

Will be converted to: <a href =/"/mystrutsapp6/index.html/"> click here </a>

<Errors> label
<Errors> the ease of use of tags often masks their real powerful functions. With a simple <HTML: errors/> tag, you can display a fully customized error message on a JSP page.

This tag checks the property set of the request object to find a reserved key. If it finds a reserved key, it assumes that the key is a string, A String Array (it contains the message keys found in messageresources of the module), or the type is Org. apache. struts. action. an object of actionerrors.

If the corresponding information exists in the Application resource, you can use the following optional message keys: · errors. header: the corresponding information is displayed in the separate list of error information.
· Errors. footer: the corresponding information is displayed after the separate list of error messages.
· Errors. Prefix: the corresponding information is displayed in the separate list of error messages.
· Errors. Suffix: the corresponding information is displayed after the separate list of error messages.

Struts HTML Tag library makes development easier and faster. To effectively use this tag library, you need to be familiar with the tags in the tag library. In the first part of this series of articles, you learned how to configure a struts application to use the tag library and have a general understanding of independent labels. In the second part, you will learn the form-related labels;

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.