Struts2 advanced (5), struts2 advanced

Source: Internet
Author: User

Struts2 advanced (5), struts2 advanced
I. Introduction to OGNL

The abbreviation of OGNL (Object-Graph Navigation Language.

Special features of OGNL expressions:

> You can obtain the attributes of an object and call methods of an object.

> Access static methods and static attributes.

> Access value Stack and Stack Context

> Ability to operate collection objects

> Supports value assignment, operation, and field type conversion.

Ii. OGNL access Value Stack, Stack Context, and set 1. OGNL access Value Stack (Value Stack)

(1). the value stack can be understood as a Map storing data, which mainly stores the instance of the current Action.

(2) After struts2 receives the request, it creates an Action instance to process the request.

(3). Action saves the data read from db to the attribute defined in Action.

(4). Struts2 saves the Action to the value stack for GONL access.

(5) After the Action is processed and the jsp page information is returned, you can use the <s: debug/> label to view the information in the value stack.

To view the information in the value stack, you must introduce tablib on the jsp page. The example is as follows:

<% @ Taglib uri = "/struts-tags" prefix = "s" %>

<S: property value = "username">

2. OGNL access Stack Context

In the preceding example, ognl is used to access data in Action. How can I use ognl to access request, session, and Application?

The "#" symbol must be added to access Stack Context. For example, the userName in the Access session is as follows:

<S: property value = "# session. userName"/>

3. OGNL access set

The set is often returned in the Action. How can this problem be accessed?

On the jsp page, you can use the following code to access the collection:

(1). Access the element whose subscript is 0 in the list set.

<S: property value = "userList [0]"/>

(2) access the attributes of the first element object in the List set, such as the value of user. userName:

<S: property value = "userList [0]. userName"/>

(3). Get the size of the List set

<S: property value = "userList. size ()"/>

(4). Check whether the List set is empty.

<S: property value = "userList. isEmpty ()">

(5). Obtain the value whose key is m1 in the map set.

<S: property value = "userMap ['m1 ']"/>

<S: property value = "userMap. m1"/>

(6). Get the map key set

<S: property value = "userMap. keys"/>

(7). Obtain the set of map values

<S: property value = "userMap. values"/>

(8). Obtain the number of map value pairs

<S: property value = "userMap. size ()"/>

(9). Obtain whether the map set is empty

<S: property value = "userMap. isEmpty"/>

(10). Get the set. Because the set is unordered, it is first converted to an array.

<S: property value = "userSet. toArray () [0]"/>

Iii. Struts2 tag

The tags provided by struts2 can be divided into the following categories"

1. General labels

(1). Data tag

(2). control tags

2. UI tag

3. Ajax tag

1. Common tags (1). Data tags

Before using the tag provided by struts2, you must introduce the taglib command on the jsp page:

<% @ Taglib uri = "/struts-tags" prefix = "s" %>

<S: property value = "username"> tag:

Value: used to obtain the value of the OGNL expression.

Default: If the returned value of the property is null, enter the result.

EscapeHtml: whether to escape html. The default value is true,

<S: debug/>

View value Stack and Stack Context

<S: date name = "nowDate" fromat = "yyyy-MM-dd">: used to format and output a date.

Name: The formatted value. It must be set.

Format: format used for date display: for example, "yyyy-MM-dd HH: mm: ss"

<S: set var = "name" value = "user. userName" scope = "session"/>

Evaluate the set expression and assign the result to a variable in a specific scope.

Var: variable name, value: the value set to the variable, scope: the life cycle of the variable.

<S: url value = "http://www.cnblogs.com/izhongwei" var = "url"/>

Used to generate a url. The main attributes include:

Var: the url name. If this attribute is specified, the url tag will not generate a string on the page.

Action: The name of the Action to be accessed.

Value: the target to be accessed. if action is not specified, value is used as the url address value.

<S: a> used to generate html <a> tag <s: a href = "$ {# url}"> hyperlink </s: a>

<S: param>: Add parameter settings for other labels.

<S: url value = "http://www.cnblogs.com/izhongwei" var = "url">

<S: param name = "str" value = "accp"> </s: param>

</S: url>

<S: a href = "% {# url}"> hyperlink </s: a>

The generated html code is: <a href = "http://www.cnblogs.com/izhongwei? Str = "accp" "> hyperlink </a>

<S: include>: Action tag similar to jsp <jsp: include>

<S: include value = "include. jsp"/>

(2). control tags

<S: if>, <s: elseif>, <s: else>

<S: iterator> mainly used to loop a set:

1. value: the set accessed cyclically

2. var: variable name, used to reference the set elements of the current circular access.

3. status: returns information about the current loop.

Count: the number of objects contained in the set.

Index: The index of this item is circulating.

Even: whether the retrieved object is in the even index position of the list.

Odd: whether the retrieved object is in the odd index position of the list.

<S: iterator var = "user" value = "userList" status = "status">

<S: if test = "# status. even"> <tr bgcolor = "yellow"> </s: if>

</S: iterator>

2. UI tag

The struts2 UI tag can generate an inaccessible style by setting an inaccessible topic. Themes include simple, xhtml, css_xhtml, and ajax;

You can configure and set the topic format in struts. xml:

<Constant name = "struts. ui. theme" value = "simple">

<S: form>: corresponds to <form>:

Attributes: name, action, method

<S: textfield> <input type = "text"> single-line text box in html

Attribute: name, value, maxlength, readonly

<S: textarea> corresponds to <textarea> in html, that is, the multi-line text box.

Attributes: name, value, cols, rows, readonly

<S: submit> corresponding to <input type = "submit"> in html, its basic attribute is value.

<S: select> drop-down list

Attribute: name (name of the drop-down list), list (Set of the drop-down list), and listKey (value Attribute of the production drop-down list)

ListValue (generate the text displayed in the drop-down list)

<S: doubleselect>: used to generate two linked drop-down boxes.

3. Ajax tag

Struts2 provides a complete Ajax solution, datetimepicker date space tag example

 

Related Article

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.