Common struts2 labels

Source: Internet
Author: User

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>

Detailed description of iterator subscript!

<S: iterator id = "OBJ" value = "list" status = "Index">

 

 

<! --- Output the attributes of the current element -->

<S: property value = "property"/>

 

<! --- Output the index of the current iteration element -->

<S: property value = "# index. Index"/>

 

<! --- Outputs several elements of the current iteration -->

<S: property value = "# index. Count"/>

 

<! --- Returns whether the index of the current iteration element is an odd number. -->

<S: property value = "# index. Odd"/>

 

<! --- Returns whether the index of the current iteration element is an even number. -->

<S: property value = "# index. Event"/>

 

<! --- Returns whether the current element is the first -->

<S: property value = "# index. First"/>

 

<! --- Return whether the current element is the last -->

<S: property value = "# index. Last"/>

 

<! --- Returns the subscript of the current element -->

<S: property value = "# index. getcount ()"/>

 

 

 

 

 

1. Use of common labels in struts2 page development
1. How to pass values to an action:
<Input name = "username" type = "text" class = "input6" size = "15">
A. the username attribute must provide the corresponding attributes in the accessed action and have the set/get method.
B. You can provide the corresponding attribute in the action that is not accessed. Use request. getparameter ("username"); 1. 2. display the tag property to output the specified value:

<S: property value = "username"/> when an action returns to a specified page, you can use this tag to display the property information in the action. (Note that username must exist in action with the get method ).

. The <s: textfield> value passed to the object is used to pass the value from the page to the object in the action:
<S: Text [color = Blue] [/color] field name = "user. username" id = "username"/>
The tag can be directly transferred to the object in the action on the page. (Note: there must be a user object in the action and a set/get method .)

. Judgment <s: If> </S: If> is used to determine the specified data on the page.
<S: If test = "username = admin"> .... </S: If>
<S: else> .... </S: else>
This label is used to determine the specified data, but the specified data must exist in the action.

1. 5. Iteration <s: iterator> is used to traverse the list, MAP, arraylist, and other sets cyclically.
<S: iterator value = "userlist" id = "user" status = "U">
<S: property value = "username"/> </a>
</S: iterator>
This label is used to loop the elements in the userlist set and display the specified element values in the set using property.

1.6.url address label. <s: URL> is used to generate a URL address. You can use the <s: param> sub-element specified by the URL tag to send request parameters to the URL address.
<S: URL action = "">
<S: Param name = "" value = ""> </S: param>
</S: URL>
Here, action is the request address, and the param sub-element is the parameter after the address.

. Hyperlink <a href> is generally used together with the <s: URL> label to include multiple parameters.
<A href ="
<S: URL action = "">
<S: Param name = "" value = ""> </S: param>
<S: Param name = "" value = ""> </S: param>
<S: Param name = "" value = ""> </S: param>
</S: URL> ">
Hyperlink </a>

1.8.set label, used to put a value within the specified range. For example, application and session.
<S: Set Name = "user" value = "username" Scope = "request"/>
Put the user value within the request range.

2. Common labels used in struts2 page development:
1. 1. Use <input name = "username" type = "text" class = "" size = "15">
Values are transmitted to the action. The action must have corresponding attributes and provide the set/get method.
(When the number is small, you can use request. getparameter ("username ");)

. Use the property tag to display the data sent from the action on the page. The data to be displayed is obtained from the action.

. Pass the value to the object in the action and use the textfield tag.

. Determine whether to use the <s: If> label for the specified value on the page. The judgment data is obtained from the action.

. Use the <s: iterator> label for the data displayed in the cyclic list on the page. The list data is obtained from the action.

1.6.you can use <a href> and <s: URL> for URL addresses and hyperlinks. We recommend that you use <a href> and <s: URL> when transferring multiple values.

 

 

 

Radio

 

My summary is: struts2's tag: Radio.
For example, for our gender selection, there is a radio tag in struts2, but let's look at it again;
1. It has a list property. How can we use it?
2. First of all, we must have a default value on the interface. How can this be done?
Solve the problem now:
Create a bean class for sex:
Package com. lxit. Main. system. Action;
Import java. util. hashtable;
Import java. util. Map;

Public class sexs {
Public Map <string, string> getsexs (){
Map <string, string> sexs = new hashtable <string, string> (2 );
Sexs. Put ("male", "male ");
Sexs. Put ("female", "female"); // (Key, value) value = "'female" here is the key,
Return sexs;
}
}
Then, on the interface:

<S: bean id = "sexs" name = "com. lxit. Main. system. Action. sexs"/>
<S: Radio list = "# sexs. sexs "value =" 'female "name =" student. sex "label =" % {gettext ('addstu. sex ')} "/>

Note: <1>. list = "# sexs. sexs "First: sexs, which is the bean ID, second sexs, which is the return sexs in the bean class above. otherwise. the code you carefully written will disappoint you...

<2>. value = "'femal'": the default value on the interface is 'femal'. The value in the map is displayed on the interface; in the background, the map contains the key. this is a bit unreasonable. so pay attention. after I found out, the key and value in the put above are all Chinese characters. "male, female"
Name = "student. Sex" is the field label = "student, sex" in my action class. I used a *. properties file.
In this way, you can work under your normal project.

 

This implementation is relatively simple: <s: Radio name = "student. sex "list =" % {# {'male': 'male', 'female ': female} "value =" male "label =" gender "> </S: Radio>

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.