struts--Course Note 3

Source: Internet
Author: User

Get SERVLETAPI:

The first way:

Putting the attribute req in the request field, it is assumed that GetContext () obtains the request domain space, but is not actually
Actioncontext.getcontext (). Put ("req", "Req_value");
Put attribute ses in session field
Actioncontext.getcontext (). GetSession (). put ("ses", "ses_value");
Put the properties app in the Application field
Actioncontext.getcontext (). Getapplication (). Put ("app", "App_value");

The second way:

Get the real HttpServletRequest
HttpServletRequest request = Servletactioncontext.getrequest ();
Put attributes into request
Request.setattribute ("req", "Req_value");
Putting attributes into the session
Request.getsession (). SetAttribute ("ses", "ses_value");
Putting attributes into application
Servletactioncontext.getservletcontext (). SetAttribute ("app", "App_value");
The Third Way:

public class Loginaction implements Requestaware, Sessionaware, Applicationaware

Implement these three interfaces

Summary: The first method is simple, suitable for only the corresponding field to put values, the second is complex, but can get more properties in the corresponding domain, the third is not recommended, because the implementation of the interface, easy to lead to injection programming.

OGNL and Value stacks:

1. Introduction:

OGNL is an acronym for Object-graph Navigation language, a powerful expression language and a third-party open source project.

Struts2 by using OGNL simple and consistent expression syntax, you can access any property of an object, invoke the object's method, traverse the entire object's structure diagram, and implement the function of field type conversion.

To import a struts label using OGNL: <%@ taglib uri= "/struts-tags" prefix= "s"%>

2. Features:

In contrast to other expression languages, it provides rich functionality:

(1) Support for object method invocation, such as Xxx.sayhello ()

(2) Support class static method invocation and constant access, the format of the expression is: @[email protected] or @[email protected] ()

However, access to a static method needs to be turned on by modifying the constant struts.ognl.allowStaticMethodAccess to true in Struts.xml.

(3) You can manipulate the collection object

(4) You can create objects directly

3. Detailed Description:

The framework uses a standard naming context to evaluate OGNL expressions. The top level object dealing with OGNL is a map (usually referred as a context Map or context).

OGNL has a notion of there being a root (or default) object within the context. In expression, the properties of the root object can is referenced without any special "marker" notion.

References to other objects is marked with a pound sign ( # ).

The framework sets the OGNL context to is our actioncontext, and the value of stack to is the OGNL root object. (The value of the stack is a set of several objects, but to OGNL it appears to bes a single object.)

Along with the value stack, the framework places other objects in the Actioncontext, including Maps representing the Appli cation, session, and request contexts. These objects coexist in the Actioncontext,

Alongside the value stack (our OGNL root).

      

4. Creation of value stacks:

After the user submits an action request, two objects are created immediately: the action instance and the value stack object. The value stack valuestack in Struts2 is an interface whose implementation class is Ognlvaluestack.

The Ognlvaluestack class contains two key member variables: root and context.

(1) Root:

The bottom of root is a stack that encapsulates the ArrayList, and access to the value stack Valuestack is the access to the object in root.

(2) Context:

The type of the context is map<string,object> When you create a valuestack, you initialize root, and when you initialize root, you create a default context, This default context (MAP) immediately stores the valuestack (in fact, the address),

Therefore, the context is a member variable of Valuestack, but at the same time Valuestack belongs to the context, this situation.

5. Get the value stack:

(1) The method of trouble:

When an action request arrives, not only will an action instance be created, but a Valuestack object will also be created to hold the relevant data during the action run. So the life cycle of the action instance and the Valuestack object is the same, in order to guarantee this,

The valuestack is put into the request's domain property by means of the setattribute () method, and the key of the property is stored in the Servletactioncontext as a constant. So, here's how to get Valuestack:

Valuestack valueStack1 = (valuestack) servletactioncontext.getrequest (). getattribute (Servletactioncontext.struts_ Valuestack_key);
(2) Simple method:

Because the context in Valuestack needs to be accessed frequently, Struts2 takes an alias Actioncontext for this context, so. The following methods for obtaining Valuestack are also available:

Valuestack ValueStack2 = Actioncontext.getcontext (). Getvaluestack ();

6. Operation of the value stack:

The operation of the value stack is divided into the value stack to put the data and read the data from the value stack. Putting data into the value stack is divided into display put and implicitly put.

(1) To display the data in root:

Student student1 = new Student ("Zhang San", 23);
Student Student2 = new Student ("John Doe", 24);
Student Student3 = new Student ("Harry", 25);
Student student4 = new Student ("Zhao Liu", 26);
Student student5 = new Student ("Ruan Seven", 27);
Put an object directly into the value stack (Nameless object)
Actioncontext.getcontext (). Getvaluestack (). push (STUDENT1);
Put the object directly into the root of the value stack (Nameless object)
Actioncontext.getcontext (). Getvaluestack (). Getroot (). push (Student2);
Put a well-known object into the value stack: via map
map<string,object> map = new hashmap<string,object> ();
Map.put ("Student3", Student3);
Actioncontext.getcontext (). Getvaluestack (). push (map);
Use the Set method of the value stack to place the named object
Actioncontext.getcontext (). Getvaluestack (). Set ("Student4", student4);
Use root as ArrayList to add objects to it (Nameless objects)
Actioncontext.getcontext (). Getvaluestack (). Getroot (). Add (STUDENT5);
Actioncontext.getcontext (). Getvaluestack (). Getroot (). Add (0,STUDENT5);

When the front end gets data:

Name = ${name}<br>
Name = <s:property value= "name"/><br>
Age = ${age}<br>
Age = <s:property value= ' age '/><br>
Student3 = <s:property value= "Student3"/>
Student4 = <s:property value= "Student4"/>

Summary: Because root is the ArrayList type, in the value stack directly into the object, only the index, no key, there is no name, so this method is called put the Nameless object.

In this case, when you get a property, such as name, name = <s:property value= "name"/><br>, it only extracts the Name property of the top object of the stack.

To put a well-known object in the value stack, it is done through a map, which can be divided into two ways:

(1) Manually create a Map object, add the object and object name to it, and then put the map into the value stack, this way to create a map object, you will find a map in the value stack

(2) using the Set method of the value stack, put in the object and object name, this way first determine whether there is already a map created by set in the value stack (regardless of manually created map). If not created, a map is automatically created and values are placed into it;

If you already have a map, put the value directly in the map.

In this case, you can get the object by the object name, i.e. Student3 = <s:property value= "Student3"/> Further, you can get the properties in this object, Student3-name = <s: Property value= "Student3.name"/><br>

The debug structure diagram (which is displayed by adding <s:debug/>):

        

Note: It does not correspond to the code, but it can be found from the value stack to obtain data in two cases, one is the stack top attribute method, one is a map with the famous object method.

(2) implicitly putting the data into root:

Once the value stack has been created, the created Xxxaction object is first placed directly on top of the stack of values. After that, the data submitted by the front-end form is implicitly passed in to the corresponding xxxaction space.

        

In the action receive request parameter is a property driven mode

        

In the action receive request parameter is a domain-driven way, in this way, to get the name and age property, it is necessary: name = ${student.name} or name = <s:property value= "Student.name"/>. But be aware that:

The name of the object that receives the data in this domain-driven manner cannot be the same as the name of the named object in the map above, otherwise it will be overwritten. That is, the set-out map, the name of the named object in the manually created map, and the name of the object in the domain-driven mode

Will cover each other.

(3) Add data to the context display:

Student student1 = new Student ("Zhang San", 23);
Put data directly into the context
Actioncontext.getcontext (). Put ("Student1", student1);
Put data into session and application
Actioncontext.getcontext (). Getapplication (). Put ("Other", "App_value");
Actioncontext.getcontext (). GetSession (). Put ("some", "ses_value");
Actioncontext.getcontext (). Getapplication (). Put ("some", "App_value"

As an item in the context.

When the front end gets data:

Student1 = <s:property value= "Student1"/><br>
#session. Some = <s:property value= "#session. Some"/><br>
#application. Some = <s:property value= "#application. Other"/><br>
#attr. Some = <s:property value= "#attr. Some"/><br>
#attr. Other = <s:property value= "#attr. Other"/><br>

Where attr is looking for the corresponding attribute in the following order:

        

(4) implicitly inserting data into the context:

The data submitted by the front-end form is implicitly propagated to the parameters in the context, regardless of whether the corresponding attribute exists in the xxxaction.

In addition, the created Xxxaction object will not only be placed on top of the stack, but will also be placed in the action in the context.

        

When the front end gets data:

#parameters. Name = <s:property value= "#parameters. Name"/><br>
#parameters. Age = <s:property value= ' #parameters. Age '/><br>
Name = <s:property value= "name"/><br>
Age = <s:property value= ' age '/><br>
#action. Name = <s:property value= "#action. Name"/><br>
#action. Age = <s:property value= ' #action. Age '/><br>

(5) Load order of data in root:

Actioncontext.getcontext (). Put ("some", "context_value");
Actioncontext.getcontext (). Getvaluestack (). Set ("Some", "root_value");

The front end gets the data, and when the data is fetched without the # number, first looks for the data in the value stack, if it does not exist, finds it from the context and, if there is one, returns the value in the context.

some = <s:property value= "some"/>

(6) Load order of data in Request:

Actioncontext.getcontext (). Put ("some", "context_value");
Actioncontext.getcontext (). Getvaluestack (). Set ("Some", "root_value");
Servletactioncontext.getrequest (). SetAttribute ("Some", "req_value");
The front end gets the data, and when it gets the request data with #request, it finds the value in the request, in the value stack, and in the context, and returns with a value.

#request. Some = <s:property value= "#request. some"/>

That explains the problem. (at the top of the page = =) Think of GetContext () to get the request domain space, but actually not)

7. OGNL for set operations (mainly including: list and map creation and traversal, collection element judgment, set projection, set filtering):

<br>--------List--------<br>
<!--variables defined by the set tag are placed in the context--
<s:set name= "Names" value= "{' Zs ', ' ls ', ' ww '}"/>
<s:iterator value= "#names" > <!--automatically puts objects of the current iteration on top of the stack--
<s:property/><br> <!--If S:property does not give Value property values, the top element of the value stack is automatically output--
</s:iterator>
<br>--------Map--------<br>
<s:set name= "Maps" value= "#{' mobile ': ' 1234567 ', ' QQ ': ' 7654321 '}"/>
<s:iterator value= "#maps" >
<s:property/><br>
</s:iterator>
<s:iterator value= "#maps" >
<s:property value= "Entry"/><br>
</s:iterator>
<s:iterator value= "#maps" >
<s:property value= "key"/> = <s:property value= "value"/><br>
</s:iterator>
<br>--------in vs. in--------<br>
<s:property value= "' Zs ' in #names"/><br>
<s:property value= "' Zs ' not in #names"/><br>
<br>--------Create three student objects--------<br>
<s:bean name= "com.tongji.beans.Student" id= "Student3" > <!--the Student will be placed in context--
<s:param name= "name" value= "' Zhang San '"/>
<s:param name= "Age" value= "all"/>
</s:bean>
<s:bean name= "com.tongji.beans.Student" id= "Student4" > <!--the Student will be placed in context--
<s:param name= "name" value= "' John Doe '"/>
<s:param name= "age" value= "/>"
</s:bean>
<s:bean name= "com.tongji.beans.Student" id= "student5" > <!--the Student will be placed in context--
<s:param name= "name" value= "' Harry '"/>
<s:param name= "age" value= "/>"
</s:bean>
<br>--------Define these three student objects as a list--------<br>
<s:set name= "Students" value= "{#student3, #student4, #student5}"/>
<br>--------Define the name of these three student objects as a list, which is the projection--------<br>
<s:set name= "Studentnames" value= "students. {Name} "/>
<s:iterator value= "#studentNames" >
<s:property/><br>
</s:iterator>
<br>--------Inquiries: All students age greater than 23--------<br>
<s:iterator value= "#students. {? #this. age>23} ">
<s:property/><br>
</s:iterator>
<br>--------Inquiry: First student with age greater than 23--------<br>
<s:iterator value= "#students. {^ #this. age>23} ">
<s:property/><br>
</s:iterator>
<br>--------query: Last student with age greater than 23--------<br>
<s:iterator value= "#students. {$ #this. age>23} ">
<s:property/><br>
</s:iterator>

struts--Course Note 3

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.