Only introduce the use of HTML tag libraries in strut (first part)

xiaoxiao2021-03-06  79

Struts provides a powerful HTML tag library to help you build a web application.

BY Budi Kurniawan

In order to make it easier, develop, Struts provides five label libraries similar to other JSP tag libraries, namely: HTML, Bean, Logic, Template and Nested. . The first part tells how to configure a Struts application to use this tag library and introduce the first group of labels; the second part tells the second set of labels;?

The HTML tag library is mainly used to display HTML elements. If you don't have this method, you must use the HTML tag to specify them. From the surface, this label library is simple. But through in-depth research, we will find it very powerful. For example, it can place a key content to generate JavaScript in a specific input element of an HTML form, or enter a verification generated script for the client. Moreover, you can use it to handle errors through a line of code. However, before you use this tag library for your Struts application, you need to do some simple preparations.

Configure the application to use the tag library to configure a Struts application through three steps before using the Struts HTML tag library.

1. Register the label library in the deployment descriptor (web.xml file):

/Web-inf/struts-html.tld

/Web-inf/struts-html.tld

The above code told the Servlet Container about the situation of the Struts HTML tag library, and where to find the TLD file of the label library.

2. Confirm that the struts-html.tld file is copied to the web-inf directory. You don't have to worry about label library class files because they are already included in the struts.jar file.

3. Insert the following in each JSP page that uses the label library

indicator:

<% @ Taglib Uri = "/ Web-INF / STRUTS-HTML.TLD"

Prefix = "html"%>

In addition, you can also use struts-html.tld files to learn what it supports the label and which properties can be taken.

It is usually easy to use a custom label library: as long as you follow the steps described above. However, some tag libraries contain some principles that make us not so easy when using these labels. The Struts HTML tag library is one of them. Some tags are simple, easy to use; however, some labels depend on other tags or other elements in the Struts application.

I am roughly divided into two categories in the HTML tag library: simple, easy to use tags, I call them "independent tags", and the second class is the label that must be used through a FORM label. I simply be simply called the second class of labels related to Form. This article mainly tells the independent label, and I will tell the form-related label in this series of articles.

The HTML tag library contains several easy-to-use independent labels. Here, there are several important labels.

The tag tag is the easiest label in the HTML tag library. It has two properties: Locale and XHTML, both of which are not required. The following code is selected from a JSP page using the label:

<% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%>

Welcome

Hello World!

Note that we have no standard elements, but used

label. The first HTML refers to the prefix, the second is the label itself. And we also used the local property. The JSP page will be displayed:

Welcome

Hello World!

note,

The local locale property is converted into lang = "en" in the generated HTML page. The resulting result depends on the locale of the server located at the Struts application. If you deploy an application to a different locale server, you don't need to change the code. Locale will automatically adjust. The tag tag displays an HTML element that represents the absolute position of the included JSP page with an HREF property. It is only valid when this tag is embedded in a Head tag portion. For example, this JSP page:

<% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%>

Welcome

Hello World!

Convert to the following form:

Welcome

Hello World!

Label tags are used to generate an HTML tag. Its most important properties are:

· Page: The image file is relative to the path of the module; there must be a slash.

· Height: The height of the image.

· Width: The width of the image.

· Alt: If the image is not found, the text is displayed.

E.g:

Will convert to:

> Tags> Tags Generate a hyperlink. For example, tag:

Click

Here

Will convert to:

Click

Here

label

The easy usability of the label usually masks its true powerful function. Through a simple

Tags, you can display full-defined error messages on a JSP page.

This tag checks for the property collection of the Request object to find a reserved key. If it finds a reserved key, it assumes that this key is a string, or a string array (which contains Message Keys in the module's MessageResources, or the type ORG.Apache.Struts.Action.ActionerRRRUTS.Action.Actionerror Object.

If there is a corresponding information in the application resource, you can use the following optional Message Keys:

Errors.Header: The corresponding information is displayed before the individual list of the error message.

· Errors.footer: The corresponding information is displayed after the individual list of the error message.

· Errors.prefix: The corresponding information is displayed before the individual list of the error message.

· ERRORS.SUFFIX: The corresponding information is displayed after the individual list of the error message.

• The Struts HTML tag library will make development easier and fast. In order to effectively use this tag library, you need to be familiar with the label in the label library. In the first part of this series, you learned how to configure a Struts application to use the label library and have an overall understanding of the independent tag. In the second part, you will learn the labels related to Form;

?

转载请注明原文地址:https://www.9cbs.com/read-121742.html

New Post(0)