I,
When writing a JSP page, in struts2, the s tag is used. First, the tag is introduced:
<% @ Taglib prefix = "S" uri = "/Struts-tags" %>
II,
The struts2 tag and 1 are completely different.
Struts2 labels are classified into two categories: Non-UI labels and UI flags. struts1 divides the flags library into HTML, tiles, logic, and bean by function.
The following describes the specific tag of strut2:
1. UI
Ui labels can be divided into Form UI and form UI. The form UI is basically the same as Struts 1.x, which is used to encapsulate HTML form elements. However, Struts 2.0 adds several controls that we often use in projects, such as datepicker, doubleselect, timepicker, and optiontransferselect. Because many of these labels are often used and there are many parameters, it is not easy to describe them in detail in an article.
The following describes the usage of UI labels.
Form:
<S: Form Action = "examplesubmit" method = "Post" enctype = "multipart/form-Data">
<S: Submit/>
<S: reset/>
</S: Form> A form that can upload files.
Textfield:
<S: textfield
Label = "name :"
Name = "name"
Tooltip = "enter your name here"/>
Datepicker:
<S: datepicker
Tooltip = "select your Birthday"
Label = "Birthday"
Name = "Birthday"/>
Textarea:
<S: textarea
Tooltip = "Enter your remart"
Label = "Remarks"
Name = "remart"
Cols = "20"
Rows = "3"/>
Select:
<S: select
Tooltip = "choose user_type"
Label = ""
List = "# {'free': 'free', 'vip ': 'Bill'}" value = "# {'free': 'free '}"
Name = "bean. user_type"
Emptyoption = "true"
Headerkey = "NONE"
Headervalue = "NONE"/>
<S: select
Tooltip = "choose user_type"
Label = ""
List = "# {'free': 'free', 'vip ': 'Bill'}" value = "# {'free': 'free '}"
Name = "bean. user_type"
Emptyoption = "true"
Headerkey = "NONE"
Headervalue = "NONE"/>
<S: select
List = "venderlist"
Listkey = "ID"
Listvalue = "name"
Value = "% {profile. companyName }"
Name = "companyName" cssclass = "sel_style_w_180"/>
Very useful
Checkboxlist:
<S: checkboxlist
Tooltip = "choose your friends"
Label = "friend"
List = "{'Patrick ', 'jason', 'Jay ', 'toby', 'rene '}"
Name = "friends"/>
Checkbox:
<S: checkbox
Tooltip = "confirmed that your are over 18"
Label = "Age"
Name = "legalage"
Value = "18"/>
File:
<S: File
Tooltip = "upload your picture"
Label = "picture"
Name = "picture"/>
A:
<S: a href = "getp. jsp"> hyperlink submission </s: A>
Date:
<S: Date name = "ad_end_time" format = "yyyy-mm-dd"/>
2. Non-UI
If, elseif, and else descriptions:
Execute basic conditional flow.
Parameters:
The name must be of the default type description. Note: "test" is a Boolean expression that determines whether the content is displayed. The else flag does not have this parameter ID. No object/string is used to identify the element ID. The following is an example of the ID attribute of HTML in the UI and form:
<% @ Page C %>
<% @ Taglib prefix = "S" uri = "/Struts-tags" %>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Title> condition flow </title>
</Head>
<Body>
<H3> condition flow <! --
TIPS:
You can use # parameters. name [0] to obtain the value of name in the request. However, when I implement the include example,
No matter whether I use the param flag to assign any value to name, # parameters does not contain any value, so # parameters. Name is also null.
The reason is:
When the include flag is used, # parameters in the contained page obtains the Request Parameters in the page.
Therefore, you must manually call request. getparameter ("name ").
-->
<S: iterator value = "linklist" status = "Bean">
<Tr>
<TD class = "data_tab_tdcl">
<S: property value = "# bean. index + 1"/> </TD>
<TD class = "data_tab_tdcl"> <s: property value = "link_title"/> </TD>
<TD class = "data_tab_tdcl"> <s: property value = "link_url"/> </TD>
<TD class = "data_tab_tdcl">
<S: If test = "link_type = 1">
Text
</S: If>
<S: elseif test = "link_type = 2">
Image
</S: elseif>
<S: else>
-----
</S: else>
</TD>
</Body>
</Html>
Example 1 condition. jsp
Iterator description:
Used to traverse a collection (Java. util. Collection) or enumerated value (Java. util. iterator ).
Parameters:
The name must be of the default type description status no string if this parameter is set, an iteratorstatus instance will be pushed to the iteratable data source for each traversal stack value no object/string to be traversed, or the ID of the object to be put into the new list (list). No object/string is used to identify the ID of the element. The following is an example of the ID attribute of HTML in the UI and form:
<% @ Page C %>
<% @ Page import = "Java. util. List" %>
<% @ Page import = "Java. util. arraylist" %>
<% @ Taglib prefix = "S" uri = "/Struts-tags" %>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<%
List list = new arraylist ();
List. Add ("Max ");
List. Add ("Scott ");
List. Add ("Jeffry ");
List. Add ("Joe ");
List. Add ("Kelvin ");
Request. setattribute ("names", list );
%>
<HTML>
<Head>
<Title> iterator </title>
</Head>
<Body>
<H3> names: <! --
1. the null property element is used to obtain the value of the current iterator.
2. Set status to stuts. In iterator, you can use # stuts to get the iteratorstatus object. The iteratorstatus class contains the current sequence number, for example, whether the first or last one is an odd number. This information is very useful when we format it.
-->
<Ol>
<S: iterator value = "# request. Names" status = "stuts">
<S: If test = "# stuts. Odd = true">
<Li> White <s: property/> </LI>
</S: If>
<S: else>
<Li style = "background-color: Gray"> <s: property/> </LI>
</S: else>
</S: iterator>
</OL>
</Body>
</Html>
- Example 2 iterator. jsp
I18n description:
Load the resource package to the value stack. It allows the text flag to access information of any resource package, not only the resource package associated with the current action.
Parameters:
The default type description value must be the class path of the Object/string resource package (for example, Com. xxxx. Resources. appmsg) ID. No object/string is used to identify the element ID. The following is an example of the ID attribute of HTML in the UI and form:
Helloworld = Hello wrold!
Example 3 classes/applicationmessages. Properties<% @ Page C %>
<% @ Taglib prefix = "S" uri = "/Struts-tags" %>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Title> internationization </title>
</Head>
<Body>
<H3>
<S: i18n name = "applicationmessages">
<S: Text name = "helloworld"/>
</S: i18n>
</H3>
</Body>
</Html>
Example 3 i18n. jsp
Include description:
Contains the output of a servlet (servlet or JSP page ).
Parameters:
The name must be of the default type. The value is the JSP or servletid to be included by string. No object/string is used to identify the ID of the element. The following is an example of the ID attribute of HTML in the UI and form:
<% @ Page C %>
<% @ Taglib prefix = "S" uri = "/Struts-tags" %>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Title> iterator </title>
</Head>
<Body>
<H3> interator page <S: Include value = "/condition. jsp">
<S: Param name = "name"> max </S: param>
</S: Include>
<H3> i18n <S: Include value = "/i18n. jsp"/>
</Body>
</Html>
Example 4 include. jsp
Param description:
Provide parameters for other labels, such as include and bean labels. the parameter name attribute is optional. If provided, the component method addparameter (string, object) is called. If not provided, the outer Nested Tag must implement the unnamedparametric interface (such as texttag ).
There are two methods to provide value: the value attribute or the text in the middle of the tag. Let's take a look at the differences:
<Param name = "color"> blue </param> <! -- (A) -->
<Param name = "color" value = "blue"/> <! -- (B) -->
(A) The parameter value is put into statck in string format.
(B) The value is stored in statck.
Parameters:
Name required default type description name no string parameter name value no stringvalue expression ID no object/string is used to identify the element ID. The following is an example of the ID attribute of HTML in the UI and form:
See example 4.
Set description:
The Set tag assigns a variable a value within a specific range. If you want to assign a complex expression to a variable, it is used every time you access the variable instead of a complex expression. It is useful in two cases: complex expressions are time-consuming (Performance Improvement) or hard to understand (code readability is improved ).
Parameters:
Name required default type description name is string variable name scope no string variable scope, can be application, session, request, page, or action. value no object/string will be assigned to the variable value ID no object/string to identify the element ID. The following is an example of the ID attribute of HTML in the UI and form:
See example 1.
Text description:
Tags of international information are supported. The Internationalized information must be placed in a resource bundle with the same name as the current action. If no message is found, the TAG body is treated as the default message. If no tag body exists, the name of the message will be used as the default message.
Parameters:
The name must be of the default type. The description name is the name ID of the string resource attribute. No object/string is used to identify the element ID. The following is an example of the ID attribute of HTML in the UI and form:
See example 3.
URL description:
This tag is used to create a URL. You can use the "Param" tag to provide the request parameter.
When the value of includeparams is 'all' or 'get', the parameters defined in the param tag have priority, that is, they will overwrite the values of other parameters with the same name. Parameter: omitted
Example:
<% @ Page C %>
<% @ Taglib prefix = "S" uri = "/Struts-tags" %>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Title> URL </title>
</Head>
<Body>
<H3> URL <A href = '<s: URL value = "/i18n. jsp"/>'> i18n </a>
<S: URL id = "url" value = "/condition. jsp">
<S: Param name = "name"> max </S: param>
</S: URL>
<S: a href = "% {URL}"> If/elseif/else </s: A>
</Body>
</Html>
Example 5 URL. jsp
Property description:
Obtain the attribute of 'value'. If the value is not provided, it is the element at the top of the stack by default.
Parameters:
Name required default type description default no string if the attribute is null, the default value escape no truebooelean is escape htmlvalue no value to be displayed on the top of the stack Object ID no object/string is used to identify the element id. The following is an example of the ID attribute of HTML in the UI and form:
See example 2.