Struts tag libraries
Windows component used by the JSP Tag Library struts formed by the four types of tags:
2. Bean tag: used to manage the bean in the JSP page
3. Logic tag: used to control flow in the JSP page
4. HTML tags: HTML tags used to generate, display data in a form, use the session ID to the URL programming
5. Template tag: common format using dynamic page template structure
2.1 Bean tag library contains tags that define a new bean, bean and its properties visit the tag. Struts framework provides a variety of custom tags used in JSP page processing JavaBean. These tags are packaged in a common tag library in the file struts-bean.tld defined in its tag library descriptor. Bean tag library will mark the four sub-categories defined:
Create and copy bean custom tag tag script variable
bean translated tag message tag international
2.1.1 Bean replication markers define a new bean, can copy an existing bean, bean can also copy from an existing property.
<bean:define> tag is used:
Define a new string constant to the existing bean definition to the new object copy an existing bean bean properties to create a new bean
<bean:define> tag attributes:
Id new definition of property described in the script variable name of the bean, you must set the Type definition of the class into the script variable id attribute Value is defined in the script variable is assigned a new object Name target bean name. If the value attribute is not set, this attribute must set the property Name property defines the bean's property name, used to define a new bean
Scope The scope of the source bean. If not set, the search scope from the page to the application scope toScope target bean scope. If not set, the default is page scope such as: the definition of a bean:
<bean:define"test" value="this is a test"/>
Sources in the page scope bean big Oh copied to another request scope bean:
<Bean: define "targetBean" name = "sourceBean"
scope = "page" toScope = "request" />
2.1.2 define the script variables defined tag from a variety of resources and generate script variables, these resources include the cookie, request parameters, HTTP headers and so on. Property as follows:
Property Description Id scripting variables and page scope attribute to define the name of Name cookie / header / parameter name of the multiple attribute set if any of this value, all matching the cookie will be accumulated and stored in a Cookie  (a array) in the type of bean. If not set, the first value of the specified cookie Cookie type as the value Value of cookie or, if no matching value, it returns the default value of this attribute is specified for example:
Script variable name is myCookie, the cookie is used to create this attribute is the name of userName.
Script variable name is myHeader, the name of the request header Accept-Language.
Script variable name is myPatameter, it kept the name request parameter is myParameter.
<bean:include> mark the response will be a resource to search, and the introduction of a script variable and the page scope attribute of type string. This resource can be a page, an ActionForward, or an external URL. And <jsp:include> difference is that the response resources are stored in a page scope bean, rather than written to the output stream. Property as follows:
Property Description Id scripting variables and page scope attribute to define the name of an internal resource Page forward a ActionForward Href resources to include the full URL
Script variable name is myInclude, to retrieve the response from the resource MyJsp? X = 1.
<bean:resource> tag will retrieve the web application resources, and the introduction of a script or a string type variable and InputStream page scope attribute. If the retrieval of resources was a problem, it will generate a request time exception. Property as follows:
Id scripting variables and page scope attribute to define the name of the relative path resources Input Name If this property does not exist, the type of resource is a string such as:
Script variable name is myResource, to retrieve the resource name is myResource.xml.
2.1.3 Display Bean tag library attributes defined <bean:write> tag, bean properties are used to package the JSP page delivered to the writer. The marking and <jsp:getProperty> similar properties as follows:
Property Description Name property to be displayed in the name of the bean property to display the name of the property. If this attribute class has java.beans.PropertyEditor, getAsText () or toString method is called Scope Bean scope, if not set, the search range from Filter page to the application scope if set true, all the special properties HTML character will be converted into the corresponding entity reference Ignore If set false, as found property will have a request time exception, otherwise returns null
<Bean: write name = "myBean" property = "myProperty" scope = "request"
filter = "true" />
myBean property myProperty will be shown, the scope for the request, If any HTML special characters will be converted into the corresponding entity reference.
2.1.4 marks and international news
strtus framework supports internationalization and localization. The user's computer in their definition of their own area where, When web application program needs to output a message, Ta Jiang references a resource file, in this file all reports using the appropriate language. An application may provide a lot of resource files, each document provides information written in different languages. If you do not find the selected language resource file, it will use the default resource file.
struts framework of the international support is to use <bean:message> tag, and the use of the data defined in the package java.util Locale and ResourceBundle classes to implement Java2 platform support for these tasks. Java.text.MessageFormat class definition of technology can support the message format. Using this feature, developers do not need to understand the details of these classes can be internationalized and set the message format.
With strtus internationalization and localization:
First step is to define the resource file name, this file contains the default language will appear in the proceedings of all information. The message of "key - value" is stored, as follows:
error.validation.location = The entered location is invalid
The files need to be stored in the class path, and its path to serve as transmitted to the ActionServlet initialization parameters are passed as parameters, the path of the format to meet the full standard Java class name specification. For example, if the resource file is stored in the WEB-INFclasses directory, file name is
ApplicationResources.properties, then the need to pass the parameter value is ApplicationResources. If the file in the WEB-INFclassescom est, then the parameter value should be com.test. ApplicationResources.
In order to achieve international, all of the resource file must be the basic resource files are stored in the directory where the. Basic resource file is included in the default regional language - local language news. If the basic resource file name is ApplicationResources.properties, then use the resources of other specific language name of the file should be ApplicationResources_xx.properties (xx is ISO codes, such as English is en). Therefore, these documents should contain the same keywords, but keyword value is a specific language.
Regional ActionServlet initialization parameter must be sent together with a true value, so that the user session will ActionServlet Action.LOCALE_KEY keywords in the user's computer to store a specific regional targets. Now you can run an international web site, which can be set according to the user's computer automatically to the appropriate regional language.
We can also use a specific string to replace part of the message, as the method with the same java.text.MessageFormat:
error.invalid.number = The number (0) is valid
We can replace the string (0) into any number we need. <bean:message> tag attribute as follows:
Property Description Key message resource file defined in the user's session keyword Locale object stored in the name of the property area. If not set, the default value is Action.LOCALE_KEY Bundle in the application context, the storage resource object attribute name. If not set this attribute, the default value is Action.MESSAGE_KEY arg0 arg1 the first replacement parameter value replaces the second parameter value arg2 arg3 the third parameter value replaces the fourth replacement value of such parameters: the resource file defines a message:
info.myKey = The numbers entered are (0), (1), (2), (3)
We can use the following message tag:
<bean:message key="info.myKey" arg0="5" arg1="6" arg2="7" arg3="8"/>
The message tag to the JSP page output will appear as: The numbers entered are 5,6,7,8
2.2 The logic of the logic tag library tags can be used to deal with the appearance of logic without using scriptlet. Struts logic tag library contains tags can conditionally generate output text object in the collection cycle in order to repeatedly produce an output text, and process control applications. It also provides a set of JSP page processing in the control tag. The tag is packaged in a file named struts-logic.tld marker package. Logic tag library defines tags to perform the following three functions:
Conditional logic repeated forward / redirect the response
2.2.1 The conditional logic
struts are three types of conditional logic. The following entities can be compared first with a constant size:
bean or bean request header parameters are listed below this type of marker:
Tagging <equal> constant and is defined if the same entity, return true <notEqual> if the constant is defined entities are not equal, return true <greaterEqual> constant greater than or equal if the entity is defined, return true <lessEqual> if constant less than or equal is defined entity, return true <lessThan> less than if the constant is defined entity, return true <greaterThan> constant greater than if the entity is defined, return true
All the tags of this type have the same attributes describe the Value property of the constant value to compare the Cookie HTTP cookie to compare to compare the name of the HTTP Header to the request header name of the parameter to the HTTP request parameters to compare the name of Name If you want to compare the bean or bean property, then the name of the property on behalf of bean property to compare the bean name of the property Scope Bean scope, if not specified scope, the scope of its search page to the application from procedures such as:
<logic:equal parameter="name" value="SomeName">
The entered name is SomeName
</ Logic: equal>
Judge called the "name" parameter if the request is "SomeName".
<logic:greaterThan name="bean" property="prop" scope="page" value="7">
The value of bean.Prop is greater than 7
</ Logic: greaterThan>
Determine the role of the page is there a domain name "bean" of the bean, it has a prop property, the property's value is greater than 7. If this property can be transformed into values on the numerical comparison, otherwise, for string comparison.
Second condition tag defines two tags:
Their function is to determine before the body tag in the calculation of specific projects exists. Tag attributes and attribute values determine the items to be checked.
Cookie cookie specified by this property will be checked for the existence Header attribute specified by this request headers will be checked whether there are parameter from the request parameters specified by this attribute will be to check for Name property if the property is not set, then have the property bean specified will be checked exists. If set, then the bean and bean properties will be checked for existence. property check-name attribute specifies the bean property specified in the Scope existence of a bean name, if specified, this is the bean scope. If you do not specify the scope, ranging from search page to the application scope. Role check the current user has confirmed the special role of the User whether the current inspection has confirmed that the user has a specific name for example:
<logic:notPresent name="bean" property="prop" scope="page">
The bean property bean.prop is present
</ Logic: notPresent>
Marked determine whether there is scope in the page called "bean" in the bean, this bean has a prop property.
The third condition marked more complex, these markers according to the results of inspection template matching tag body content. In other words, these tags determine whether a given project is a specific constant value of the substring:
These tags allow the JSP engine, a match is found or not found, the calculation of the main tag. Property as follows:
Property Description Cookie HTTP cookie to compare to the name of the Header to compare the HTTP header parameter to compare the name of the HTTP request parameters to the name of the Name property of the bean or bean comparison, this property is specified by the user bean name of the location of this property if the value set will be in the specified location (index value) to match the scope of the bean, if compared, this attribute specifies the bean scope. If you do not set this parameter, the search pages to the application from the scope of the bean property to compare the property name value to a constant value to compare for example:
<logic:match parameter="name" value="xyz" location="1">
The parameter name is a sub-string of the string xyz from index 1
</ Logic: match>
Check mark called "name" of the request parameter is "xyz" sub-string, but the substring must be "xyz" index position 1 start (that is sub-string must be "y" or "yz" ).
2.2.2 repeat markers in the logic tag library defines <logic:iterate> tag, it can be set according to a specific number of elements in the contents of the tag body to repeat the examination. 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:
Run time using expressions to return a set of attributes defined as the set of the set of bean, and use the name attribute specifies the name of the stored property.
Use the name attribute to define a bean, and use the property attribute to define a set of bean properties to return.
The current set of elements will be defined as a page scope bean. Property as follows, all of these attributes can use the run-time expressions.
Describe the collection, if not set attribute name attribute, it specifies the set to be repeated Id page scope bean and scripting variable name, which maintains a collection of elements of the handle of the current page scope JSP bean indexed name, it contains Upon completion of each repetition was set to repeat the current index of the maximum number of Name Length as a set of bean names, or a bean name, it is defined by the property attribute attribute is set to repeat the start position of the index Offset property as set Bean Scope name of the property if the specified bean name of the bean in the scope of this property. If not set, the search page to the application scope from the Type for the current page scope bean defined the type of example:
<Logic: iterate "currentInt"
collection = "<% = myList%>"
type = "java.lang.Integer"
offset = "1"
length = "2">
<% = Currentint%>
</ Logic: iterate>
Code from the list, repeat the first element of the two elements and can allow the current element as a page scope and type java.lang.Integer script variable to use. That is, if myList contains elements 1,2,3,4 and so on, the code will print 1 and 2.
2.2.3 forwarding and redirection tag forwarding tag
<logic:forward> tag can be forwarded to redirect to a specific response to the global ActionForward on. ActionForward determine the type of response is to use the PageContext forward or redirect using sendRedirect will respond. This tag is only one "name" attribute, used to specify the name of the global ActionForward, for example:
<logic:redirect> tag is an HTTP redirect to the implementation of a powerful tool. According to specify different properties, it can be achieved through a different redirect. It also allows developers to specify the redirection URL query parameters. Property as follows:
Forward mapping the resource attribute describes the relative path of the ActionForward Href resources, relative resources, the full path URL Page Name Map type page name, request, session or program name of the property, which contains additional large O redirection URL (if the property is not set property) on the "name - value" parameter. Map type of property or a bean name, which contains the same information (not set property attributes) Property Map type of bean name of the property. Bean's name from the name attribute. Scope If you specify the name of the bean, this property specifies the search scope bean. If not set, the search page to the application from the definition of the scope ParamID specific query string parameter name ParamName type the name of the bean, which contains the query parameter value (if not set paramProperty property); or a bean name, its properties (specified in paramProperty attribute) contains the query parameter value paramProperty string bean name of the property, which contains a query parameter ParamScope ParamName bean definition of the scope of the search at least use this tag to specify the forward, href or page an attribute to indicate what resources would be redirected to respond.
2.3 HTML tags
Struts HTML tags can be broadly divided into the following functions:
Display form elements and input control displays an error message display other HTML elements
2.3.1 display form elements and input controls
struts HTML form and to form the operation of the definition of ActionForm bean together. Form input field names and ActionForm bean property names defined in the corresponding. When you first display the form, the form input fields are from the ActionForm bean in the transplant, when the form is submitted, the request parameters will be transplanted to the ActionForm bean instance.
Property Description Onblur field lost focus Onchange field lost focus and the field value was changed by mouse click Ondblclick Onclick field is double click Onfocus field receives input focus Onkeydown field has the focus and there are key press
onkeypress field has the focus and there are key press and release the Onkeyup field has the focus and have the mouse button is released onmousedown onmousemove pointer field and click the mouse pointer to move within the field and in the field onmouseout mouse pointer control, but the pointer Move the mouse outside the element onmouseover pointer does not point to the field, but the pointer in the element the mouse pointer within the mobile Onmouseup field, and released the mouse button
<form> elements can be defined in other general attributes are:
Property Description Accesskey defined shortcut keys to access input fields Style style styleClass definition of the definition of input field enter the field of style sheet class Tabindex input field tab order
a) the form tag
tag is used to display HTML tags <html:form>, you can specify the name AcitonForm bean and its class name. If not set these properties, Jiu Xu Yao in the config file to specify the current input of ActionMapping Yi Indicates that the Which JSP page, and from the retrieved map bean names and classes. If the role specified in the ActionMapping does not find the specified domain name, it will create and store a new bean, or they will use to find the bean.
<form> tag can contain a variety of HTML input field corresponding to the sub-tag.
<html:form> tag attribute as follows:
<html:form action="validateEmploee.do" method="post">
</ Html: form>
Form associated with the operation of the path is validateEmployee, while the form data is passed through POST. For this form is, ActionForm bean and other information, such as the bean name of the type, scope, are operated from a form specified in the retrieved ActionMapping:
<form-bean name="empForm" type="com.example.EmployeeForm"/>
<Action path = "/ validateEmployee"
type = "com.example.ValidateExampleAction"
name = "empForm"
scope = "request"
input = "/ employeeInput.jsp">
<forward name="success" path="/employeeOutput.jsp">
If the configuration file contains the information and the request URI of the *. do is mapped to the ActionServlet, ActionForm bean associated with the form the name, type and scope are empForm, com.example.EmployeeForm and request. These attributes can also be <html:form> tag attributes using the definition shown.
The following tags must be nested markup in <html:form>
b) button, and unmark
<html:button> tag displays a button control; <html:cancel> tag to display a Cancel button. Property as follows:
Property Description Property defined in the form is submitted to the server when the request parameters to return the name of the button on the tag Value
c) reset and submit tag
<html:reset> and <html:submit> markers were able to display HTML reset button and submit button.
d) marking text and text area
<html:text> and <html:textarea> tags are HTML text boxes and text areas, properties are as follows:
Property described in the definition of property when the form is submitted the request parameters sent back to the server name, or text element to determine the current value of the bean property name Name attribute is the name of the query bean, which determines the text boxes and text areas value. If not set, will use the embedded form of the name of the ActionForm associated
<html:text> tag has the following attributes:
Property Description Maxlength to enter the maximum number of characters
Size of the text box size (number of characters)
<html:textarea> tag-specific attributes are as follows:
Property description text area rows Rows Cols number of columns the text area
e) check box and check box labeled
<html:checkbox> tag can display check boxes control. <html:multibox> tag can display HTML check box control, check box in the transmission request object name used getParameterValues () call will return a string array. Property as follows:
Property Description Name Bean's name, its properties will be used to determine the status check to select Display. If not set, will use the embedded form-related ActionFrom bean name. Property name check box, check box in determining whether to select the status display of the bean property name. In the case of check boxes, this property must be an array. Value When the check box is selected to return to the server request parameter values such as:
<html:checkbox property="married" value="Y"/>
A check box called married, when the form is submitted back to a "Y".
f) file tag
<html:file> tag can display HTML file control. Property as follows:
Property Description Name Bean's name, its attributes will determine the control to display the contents of the file. If not set, will use the embedded form of the name of the ActionForm bean related to this property defines the property when the form is submitted when the request parameters sent back to the server name, and used to determine the file control display of bean properties Accept the name of the server can handle the content type set. It will also client browser dialog file type filter optional markings on the Value button, the button to browse the local file system files
g) radio button labeled
<html:radio> tag is used to display HTML radio button control, the property is as follows:
Property Description Name Bean's name, its properties will be used to determine whether the radio button to select the status display. If not set, will use the embedded form-related ActionFrom bean name. property when the form is submitted when the request parameters sent back to the server name, and used to determine whether radio button is selected to display the bean name of the property
Value When the radio button is selected, the value returned to the server
h) hide tag
<html:hidden> hidden input tag to display HTML elements, attributes are as follows:
Property Description Name Bean's name, its properties will be used to determine the current value of hidden elements. If not set, will use the embedded form-related ActionFrom bean name. property defines the time when the form is submitted the request parameters sent back to the server name, and the hidden element is used to determine the current value of the bean name of the property Value used to initialize the value of hidden input element
i) password tag
<html:password> HTML tag to display the password control, property as follows:
Description of maxlength attribute to enter the maximum number of characters Name Bean's name, its attributes will be used to determine the password element of the current value. If not set, will use the embedded form-related ActionFrom bean name. property defines the time when the form is submitted the request parameters sent back to the server name and password elements used to determine the current value of the bean property name of the redisplay at the show this field, if the corresponding bean properties have been set up data This property determines whether to display the contents of password fields in size Size
<html:select> tag can display HTML select control, and property are as follows:
Describe the multiple properties that control the choice whether to allow multi-select Name Bean's name, which set its properties. If not set, will use the embedded form-related ActionFrom bean name. property defines the time when the form is submitted the request parameters sent back to the server name, and the need to determine which option is selected the bean name of the property Size can also show the number of options that need to be elected Value The options
k) option tag (nested within this element requires a mark in <html:select>)
<html:option> tag used to display the HTML option element set, attributes are as follows:
Set of attributes describing collection Bean name, this collection is stored in a scope attribute. The number of options and the same number of elements in the collection. Property attributes to define the options used by the bean property values, while labelProperty property used in the definition of option tag attribute labelName bean stored in a scope is used to specify the bean, this bean is a collection of string, to define <html:option> element tags (if not the same sign and value) labelProperty when used in conjunction with the collection attribute is used to define the scope stored in a bean, this bean will return a string collection, can be used to write <html:option> elements into attributes Name value is specified if this is the only attribute, it defines the scope stored in a bean, this bean will return a string collection, can be used to write the <html : option> element of the value of this property property property property used in conjunction with the collection, the definition of the value of each option to show the independence of bean's name property. If it is not shared with the collection attribute, this attribute defines the name attribute specified by the bean property name (if name attribute), or defines a ActionForm bean, this bean will return a collection value of the options to write us Some examples of what this tag:
<Html: option collection = "optionCollection" property = "optionValue"
labelProperty = "optionLabel" />
Assume the role of markers in a domain name optionCollection a collection, it contains a number of independent properties with optionValue bean, each attribute value as an option. Sign of each option by the bean's optionLabel property attributes defined.
<html:option name="optionValues" labelName="optionLabels"/>
Tag optionValues role on behalf of a domain stored in a bean, it is a string collection, can be used to write the option value, optionLabels role on behalf of a domain stored in a bean, it is also a string collection of options can be used to write the symbol.
2.3.2. Displays an error message labeled
<html:errors> tag to combine with ActionErrors to display the error message. This marked the first region from the current resource file to read information keyword errors.header, and then displays the message text. It goes on in the ActionErrors object (usually as a request parameter which is stored in the Action.ERROR_KEY keyword below) in the loop, read the news of a single ActionError object keyword, resource files from the current region to read and format the appropriate message and display them. It then reads and errors.footer keywords corresponding to the message and displayed.
Property can be filtered by defining the property to display the message, the value of the property should be stored in ActionError object ActionErrors object corresponding to the keywords. Property as follows:
Property Description Bundle that the application scope attribute names, it contains information resources, their default values Acion.MESSAGE_KEY Locale that the session scope attribute name, which stores the user currently logged on regional information. The default value is Action.ERROR_KEY
Name that request attribute name, which stores ActionErrors object. The default value is Action.ERROR_KEY property of this attribute specifies the ActionErrors object stored in each individual keyword ActionError object, it can filter messages example:
Show the collection of all the errors.
Show keywords stored in the missing.name error.
2.3.3. Other HTML tags
struts HTML tag also defines the following tag to display other HTML elements:
<html:html>: display HTML elements
<html:img>: Show image tag
<html:link>: Display HTML link or anchor
<html:rewrite>: no anchor tag to create URI
Details of these tags, please refer to the document struts.
2.4. Template tag dynamic template is modular powerful WEB page layout tool. Struts template tag library custom tags to define dynamic templates.
2.4.1. Insert tags
<template:insert> tags to the JSP page in the application to insert dynamic template. This tag is only a template property used to define the template JSP page. To be inserted into the template page is marked to specify the number of <template:put>, and these markers are defined as <template:insert> tag main content.
2.4.2. To place markers
<template:put> tag is <template:insert> marked for internal use, used to specify the resources inserted into the template. Property as follows:
Describe the content attribute defines the content to be inserted, such as a JSP file or an HTML file direct if this is set to true, the content attribute specifies the content will be displayed directly in the JSP rather than as a Name that contains file name to insert the contents of the Role if Setting this attribute, only the legitimate user has a specific role in the current can be carried out when the content of the insert.
2.4.3. Were marked using the template JSP page to retrieve the <template:put> <template:get> tag tag inserted into the JSP page resource. Property as follows:
Name property description marked by the <template:put> insert the name of the content of Role If you set this property only in the current legal only when the user has a specific role in the search for content
2.4.4. Use the template tags begin with a template JSP page, it will be all the web pages:
<% @ Taglib uri = "/ template" prefix = "template"%>
<title> </ title>
<table width="100%" height="100%">
We will name this file template.jsp. This document uses <template:get> tag to get tagged by the JSP page using <template:put> available content, and content in an HTML table is displayed. These three elements are the title, content, and footer. Typical contents of JSP is like this:
<% @ Taglib uri = "/ template" prefix = "/ template"%>
<template:put name="header" content="header.html"/>
<template:put name="content" content="employeeList.jsp"/>
<template:put name="footer" content="footer.html"/>
</ Template: insert>
This application JSP pages <template: insert tags to define the template, and then use <template:put> tag specific content into the template specified resource name JSP page. If we have hundreds of pages the same layout, but suddenly want to change this template, we only need to change template.jsp file.