Java Struts2 (iii)

Source: Internet
Author: User

, data manipulation in contextmap

Root list

 

element 1

Element 2

< p> element 3

element 4

element 5

Contextmap: map

Key

Value

Application

Map

Key

Value

Name

Test

Session

Map

Request

Map

attr

Map

1. Save data:

need to be familiar APIs for actioncontext and valuestack . The framework saves data for us.

1.1, using actioncontext to save data

1. 2.use valuestack to save data

A, how to obtain valuestack:

b,valuestack in the getroot () method:

C,compoundroot is what:

D, stack operation:

2.Fetch data: Label with Struts2 (OGNL expression ) in the JSP (most used)

Use OGNL expressions come and go, the struts2 OGNL expression must be written in the struts2 tag.

2.1, using s:property to fetch data

a , take the data in the Contextmap, use #

b, take contextmap inside valuestack object's attribute: Write property name directly

If you have a duplicate object attribute, you can OGNL expression, select the starting position for the lookup

Special Note: When s:property does not give the value property, the top object is taken by default.

Summary of Use of OGNL:

1. Take Root of the object in Properties , do not use # .

2. Take Contextmap in Key the value, to use # .

2.2 and Other methods of Valuestack:

a ,setValue method

in the get "John Doe" on JSP page

b ,set method

Detail questions:

What is the order of the elements in the root when the data is stored in the root as follows?

use <s:debug/> tags to view:

C ,findvalue: All we call on the Jsp is findvalue .

ii.changes to EL by Struts21, The problem of using EL inStruts2 :

Premise:

We should know that if we do not put data into the value stack ( root ) , then our action class defaults to the stack top of the value stack.

Problem:

we put the properties in the request domain, using the The EL expression was taken out. However, the properties placed in the application domain are not taken out using the EL expression.

2, on the EL problem analysis:

Analysis:

We know The EL expression looks for the attribute from the four domain objects in turn. The search range is from small to large. page Scope————>request scope————>sessionscope-- -->application Scope

However, the test found that after searching the request range, the search was not resumed, but instead returned to the top of the Stack object in the valuestack name the value of the property.

3,Struts2 in EL Search Order Change Summary:

EL expression: page scope————>request scope————>sessionscope ————>application Scope

OGNL expression: page Scope————>request scope————> Valuestack(in root) ————>contextmap ————>sessionscope————>application Scope

4,OGNL Special Instructions :

Note: You are aware of the following.

Third,OGNL with the use of other common label 1 ,iterator tags (very important)

2 ,OGNL projection (learn) 2. 1, using the filter conditions projection

2.2, projecting a specified property

3. Struts2The use of #,$,% symbols (Important)3.1 ,#

A, take contextmap key when used, such as <s:property value= "# Name "/>

b,used when creating a Map object in OGNL , for example:<s:radio list= "#{' male ': ' male ' , ' female ': ' female '} '/>

3.2,$

A, use EL expression in JSP , for example ${name}

b, in an XML configuration file, used when writing OGNL expressions, such as file name encoding when downloading files.

Struts.xml-->${@java. Net.URLEncoder.encode (FileName)}

3.3,%

in struts2, the Value property of some tags is a OGNL expression, such as <s:property value= "OGNL Expression"/>

There are also some tags, The Value property is a normal string, such as <s:textfield value= "username"/>, If you want to impose a normal string as a OGNL, you need to use %{} to set up the string.

For example <s:textfield value= "%{username}"/>. Of course in <s:property value= "%{ognl Expression}"/> can also be used, but not so.

4. Other labels 4.1,set label

4.2 ,action label

4.3 ,if label,ElseIf label else label

4.4,URLs , and a tags (useful)

iv. UI tags and themes for Struts21 , The benefits of UI tagging in Struts2

Automatic data Echo and error notification features

Simple style and typography with your own

2. Common attributes for form labels

Description: The value in the UI tag is typically a string.

Common Properties for 2.1,UI tags

2.2, about The use of CheckBoxList:

2.3, A small case of UI tagging, and model-driven analysis

3 ,UI label templates (subject) Default Theme in 3.1,struts2

Default Theme The name is XHTMLand is defined in the default properties file for struts :d efault.properties

3.2, changing the default theme

A.change the default theme for an element of a form: the theme property of the form element is used .

b. Change the form all topics: Use the theme property of the form label .

C, change the entire station all Forms topic: is in the struts.xml configuration file, overrides the original topic the setting.

v. Prevention of duplicate submission of forms (interceptors)1, review the previous solution:

2,Struts2 in the solution: 2.1, using redirects

Legacy problem: Can't resist back, then submit.

2.2 , using <s:token/> generating tokens with token interceptors

When you click Back, you will be prompted:

To configure the results view:

Legacy problem: This kind of solution, is to produce the error and then inform the user, you are wrong.

2.3, using <s:token/> generate token mate tokensession Interceptor

Java Struts2 (iii)

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.