Struts tag library

zhaozj2021-02-16  83

4. Struts Tag Library The Struts Tag Library used by the JSP window component consists of four types of tags: ? Bean tag: Use to manage bean  in the JSP page: Used to control the process in the JSP page ? HTML tag : Used to generate an HTML tag, display data in the form, programming the URL using the session ID ? Template Tag: Using Dynamic Template Tags Tags Include This tag library contains new beans, access beans, and The marker of its attribute. The STRUTS framework provides a variety of custom tags to process JavaBean in the JSP page. These tags are packaged in a normal tag library, define its tag library descriptor in file struts-bean.tld. The bean tag library defines the tag in four subcategories: ? Create and copy the BEAN's tag ? Script variable definition tag ? Bean translation mark ? Message International Tag 4.1.1 Bean replication tag can define a new bean, you can copy Existing beans can also replicate attributes from existing beans.

Mark is used:

? Define new string constants

? Copy existing beans to new defined bean objects

? Copy the properties of the existing bean to create new beans

?

Tag properties:

?

Attribute? Description

ID? New defined bean script variable name, must be set

TYPE? Defines the class that introduces script variables

Value? Assign a new object for script variables defined for id attributes

Name? The name of the target bean. If the value of the value is not set, this property must be set.

Property? Name property defines the bean's property name to define new beans

Scope of the SCOPE source bean. If there is no setting, the search range is from the page scope to the application scope

TOSCOPE? The role of target bean. If there is no setting, the default value is a page scope

For example: Define a bean:

Source bean is copied in the page scope, the request scope is another bean:

Scope = "Page" Toscope = "request" />

4.1.2 Defining the tag of scripting variables

Define and generate script variables from multiple resources, including cookies, request parameters, HTTP headers, and more. The property is as follows:

Attribute? Description

ID? Script variable and name of page scope attribute to be defined

Name? Cookie / header / parameter name

MULTIPLE? If this property sets any value, all matching cookies are accumulated and stored in a cookie [] (an array) type bean. If there is no setting, specifying the first value of the cookie will be used as the value of the cookie type.

Value? If there is no matching cookie or value, return this property specified by the default value.

E.g:

The script variable name is MyCookie, the name of the cookie for creating this property is UserName.

The script variable name is MyHeader, the name of the request header is accept-language.

The script variable name is MyPatameter, and the name of the request parameter it saves is also myParameter.

The tag will retrieve a resource response and introduce a script variable and a string type page scope property. This resource can be a page, an ActionForward or an external URL. versus

Differently, the resource response is stored in the bean of a page scope instead of writing to the output stream. The property is as follows:

Attribute? Description

ID? Script variable and name of page scope attribute to be defined

Page? An internal resource

Forward? An ActionForward

HREF? The full URL of the resource to be included

E.g:

The name of the script variable is MyInClude, and the response to retrieve is from the resource myjsp? X = 1.

Marking will retrieve resources in web applications and introduce a script variable and page scope attributes for inputStream or string types. If a problem occurs while retrieving resources, an exception is generated. The property is as follows: Attribute? Description

ID? Script variable and name of page scope attribute to be defined

Name? Resource relative path

INPUT? If this property does not exist, the type of resource is string

E.g:

The name of the script variable is MyResource, the name of the resource to retrieve is MyResource.xml.

4.1.3 Displaying the bean property? The tag library is defined.

Tags to deliver the properties of the bean to the packaged JSP page write. This tag is

Similar, the properties are as follows:

Attribute? Description

Name? The name of the bean to display by the property

Property? The name of the attribute to display. If this attribute class has java.beans.propertyEditor, getastext () or TOSTRING method is called

Scope? Bean's scope, if no settings, search range is from page to application scope

FILTER? If you set true, all special HTML characters in the property will be converted to the corresponding entity reference

IGNORE? If FALSE is set, a request time is abnormal when discovered attributes, otherwise returns NULL

E.g:

Filter = "true" />

MyBean's property MyProperty will be displayed, the scope is a request, and if any HTML special characters are found, it will be converted to the corresponding entity reference.

4.1.4 Message Marks and Internationalization The Strtus Framework supports internationalization and localization. Users define their own area in their computer, when the web application needs to output a message, it will reference a resource file, all messages in this file use the appropriate language. An application may provide a lot of resource files, each file provides messages written in different languages. If you do not find the resource file of the selected language, you will use the default resource file. Struts framework is used for internationalization

Tags, as well as the Locale and ResourceBundle classes defined in the Java.util packet to implement the Java2 platform support for these tasks. Java.Text.MessageFormat class definitions can support messages. With this feature, developers do not need to understand the details of these classes to internationalize and set messages.

Internationalization and localization with STRTUS:

The first step To define the name of the resource file, this file will contain all messages that will appear in the program with the default language. These messages are stored in the form of "keyword-value" as follows:

Error.validation.Location = The Entered Location Is Invalid

This file needs to be stored under the path of the class, and its path is to transmit to the ActionServlet as a parameter as a parameter, and the path format should comply with the standard naming specification for the full Java class. For example, if the resource file is stored in the web-inf / class directory, the file name is ApplicationResources.properties, then the parameter value that needs to be passed is ApplicationResources. If the file is in web-inf / classs / com / test, the parameter value should be com.test. ApplicationResources.

In order to achieve internationalization, all resource files must be stored in the directory where basic resource files are located. The basic resource file contains messages written in the local language in the default regional language - local language. If the name of the basic resource file is ApplicationResources.Properties, the name of the resource file written in other specific languages ​​should be ApplicationResources_xx.properties (xx is ISO code, such as English is en). So these files should contain the same keyword, but the value of the keyword is written in a particular language. The area initialization parameters of the ActionServlet must be transmitted with a TRUE value so that ActionServlet stores a region object of a particular user computer under an action.locale_key keyword in the user session. You can now run an international Web site that automatically displays in accordance with the area on the user's computer.

We can also use a specific string to replace some messages, just like Java.Text.MessageFormat:

Error.invalid.number = the number {0} is valid

We can replace the string {0} to any number we need.

The tag properties are as follows:

Attribute? Description

KEY? Define message key in resource file

Locale? The property name of the area object stored in the user session. If no setting is set, the default is action.locale_key

Bundle? In the application context, store the name of the properties of the resource object. If this property is not set, the default value is action.Message_Key

Arg0? First replacement parameter value

ARG1? Second replacement parameter value

ARG2? The third replacement parameter value

ARG3? Fourth replacement parameter value

For example: a message defined in the resource file:

Info.mykey = the number in {0}, {1}, {2}, {3}

We can use the following message tag:

• This information mark output to the JSP page will appear as: The Numbers Entered Area 5, 6, 7, 8

4.2 Logical Tag Logic Library The tag can be used to process appearance logic without the need to use scripTlet. The Struts Logic Tag library contains tags that can be conditionally generated, looped in an object collection, repeatedly generating output text, and application process control. It also provides a set of tags that handle flow control in the JSP page. These tags are encapsulated in the file named Struts-Logic.TLD. The mark defined by the logical tag library can perform the following three functions: ? Conditional logic ? Repeat ? Forward / Redirect Response 4.2.1 Conditional Logic Struts has three types of conditional logic. The first category can compare the following entities and a constant size: ? Cookie? Request parameters ? Bean or bean parameters ? Request header? The following list is listed: Mark?

• Returns true if the constant is equal to the defined entity

• Returns true if the constant is not equal to the defined entity

• Returns true if the constant is greater than or equal to the defined entity

• Returns true if the constant is less than or equal to the defined entity

• Returns true if the constant is less than the defined entity

• Returns true if the constant is greater than the defined entity

All marks of this class have the same attribute attribute? Describe the Value? To compare the constant value cookie? To make a comparison HTTP cookie name Header? To compare the name Parameter? To compare HTTP Request parameter Name Name? If you want to perform a BEAN or bean property, this property represents the name of the bean's name Property to compare the name Scope "scope of the SCOPE® BEAN, if no scope is specified, it Search range is from page to the application, for example:

??????? The Entered Name Is Somename determines whether the value of the request parameter named "Name" is "Somename".

The value of bean.prop is Greater Than 7

It is determined whether there is a bean named "bean" in the scope of the page, which has a PROP attribute, whether the value of this property is greater than 7. If this property can be converted to a value, the numerical comparison is performed, otherwise the string is compared. The second category tag defines two tags: ?

?

Their function is to determine if a specific item is present before calculating the marker. The attributes and attribute values ​​of the tag determine the item to be checked.

Attribute? Description

Cookie? The cookie specified by this property will be checked.

HEADER? The request header specified by this property will be checked.

PARAMETER? The request parameter specified by this property will be checked if

Name? If there is no Property property set, the bean specified by this property will be checked. If set, the bean and bean properties will be checked.

PROPERTY? Check if there is a specified property in the bean specified by the Name property

Scope? If the name of the bean is specified, this is the scope of the bean. If you do not specify a scope, the search range is from the page to the application scope.

Role? Check if the user currently confirmed is a special role

User? Check if the user currently confirmed has a specific name

E.g:

The Bean Property Bean.prop is present

The tag determines whether there is a bean named "bean" in the page scope, this bean has a PROP property.

The third type of condition mark is more complicated, and these markers check the contents of the mark according to the results of the template match. In other words, these tags determine if the value of a specified item is a substring of a particular constant:

?

?

These tags allow the JSP engine to calculate the marker main body when the matching or no discovery is found. The property is as follows:

Attribute? Description

Cookie? The name of HTTP cookie to be compared

HEADER? The name of the HTTP header to be compared

PARAMETER? The name of the HTTP request parameter to be compared

Name? To compare the properties of the bean or bean, this property is the name of the user specified bean

Location? If the value of this property is set, you will match this specified location (index value)

Scope? If BEAN is compared, this property specifies the scope of the bean. If this parameter is not set, the search range is from the page to the application scope

PROPERTY? To make a comparison bean attribute name

Value? To make a constant value

E.g:

????? The parameter name is a sub-string of the string xyz from index 1

The mark checks if the request parameter named "name" is a sub-string of "XYZ", but the sub-string must start from the "XYZ" index position 1 (that is, the sub-string must be "y" or "yz" ).

4.2.2 Repeat marker

Defined in the logical tag library

Tag, it can repeat the content of the marker according to the number of elements in a particular set. The type of collection can be java.util.iterator, java.util.collection

Java.util.map or an array. There are three ways to define this collection:

? Use run time expressions to return a collection of property collections

 • Define the collection as a bean and use the Name property to specify the name of the storage attribute.

? Use the Name property to define a bean and use the Property property to define a BEAN property that returns a collection.

The collection of current elements is defined as a bean of a page scope. The attributes are as follows, all of these properties use runtime expressions.

Attribute? Description

COLLECTION? If there is no NAME attribute, it specifies the name of the set ID "page scope bean bean and script variable to be repeated, which saves the handle of current elements in the collection.

The name of the indexed "page scope JSP bean, which contains the current index of the collection after repeated completion

LENGTH? The maximum number of repetitions

Name? As a name of the collection, a BEAN name, it is a collection of properties defined by the Property property.

OFFSET? Repeating the index of the start position

Property? As the name of the BEAN attribute

Scope? If the bean name is specified, this property sets the BEAN's scope. If there is no setting, search range from page to application scope

TYPE? Type for the currently defined page scope bean

E.g:

Collection = "<% = myList%>"

TYPE = "java.lang.integer"

OFFSET = "1"

Length = "2">

??? <% = currentint%>

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

The code will use the first element in the list to repeat two elements and allow the current element to use the script variable of the page scope and the Java.lang.integer type. That is, if MYLIST contains elements 1, 2, 3, 4, etc., the code will print 1 and 2.

4.2.3 Forwarding and Redirecting Tags

Forward mark

The tag can forward the response to the redirect to a specific global ActionForward. The type of ActionForward determines whether to use the pageContext forward response or use SendRedirect to redirect the response. This marker has only one "name" property to specify the name of the global ActionForward, for example:

Redirect tag

The tag is a powerful tool that can perform HTTP redirection. According to the specified different properties, it can achieve redirection in different ways. It also allows developers to specify query parameters for redirect URLs. The property is as follows:

Attribute? Description

FORWARD? Map the ActionForward for the resource relative path

HREF? Complete URL of Resources

Page? Resource relative path

Name? Map type page name, request, session, or program name of the program, which contains the Name-Value parameter to be added to the redirect URL (if the Property property is not set). Or a bean name with the MAP type attribute, which contain the same information (no provosty attribute)

The name of the bean property of the Property® Map type. The name of the bean is specified by the Name property.

Scope? If the name of the bean is specified, this property specifies the scope of the Search Bean. If there is no setting, search range from page to application scope

PARAMID? Define the name of the specific query parameter

The name of the paramname? String type, which contains the value of the query parameter (if the paramproperty property is not set); or a bean name, its properties (specified in the paramproperty property) contains the query parameter value

The name of the paramproperty? String bean property contains the value of the query parameter

PARAMSCOPE? Paramname defined bean search range

At least one property in Forward, HREF, or PAGE is to be specified when using this tag to indicate which resource will be redirected.

4.3 HTML tag

The Struts HTML tag can be roughly divided into the following functions:

? Display form elements and input controls

? Display error message

? Show other HTML elements

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

New Post(0)