Stack of values in STRUTS2

Source: Internet
Author: User
first, what is the value stack

Simply put: The value stack is a lightweight, memory-centric data center that corresponds to each request object.

One of the most exciting features in STRUTS2 is the introduction of value stacks, where the data is managed uniformly, for action, Result, Interceptor, and other parts of Struts2, so that the data is centrally managed without clutter and greatly facilitates program writing.

Another exciting feature about value stacks in Struts2 is that in most cases you don't have to care about the value stack, you don't have to worry about where it is, you just need to get the data you need. In other words, you can implicitly use the value stack.

Of course, if you write a more complex function such as custom result or interceptor, you still need to display the access value stack, so you still need to know the value stack.

second, what is the value stack capable of

Simply put, the value stack can be thread-safe to provide a common data access service for each request.

When a request arrives, STRUTS2 creates a new value stack for each request, that is, the value stack and the request are one by one corresponding, different requests, the value stack is not the same, and the value stack encapsulates a request for all the relevant data that needs to be manipulated.

It is because of the value stack and the correspondence of the request that the value stack guarantees the thread-safe provision of a common data access service for each request.

third, what is the value stack

In fact, until now, we have been talking about "value stacks", which is actually not accurate enough. Why is it. Because in Struts2, the value stack has the broad and narrow points:

1: Narrow Value stack

Usually refers to the implementation of the Com.opensymphony.xwork2.util.ValueStack interface object, is currently the Com.opensymphony.xwork2.ognl.OgnlValueStack object.

The narrow value stack is primarily used to access the values and results required by the dynamic El (Expression language) operation, although the Ognlvaluestack object is primarily used to support OGNL (object graph navigation language) operations.

The narrow value stack contains data that some OGNL can access, typically such as:
An instance of the action so that the value of the property in the action instance can be accessed through OGNL
The value of the OGNL expression operation can be set to the value stack, which can actively access the value stack object and forcibly set
OGNL the intermediate variables produced by the expression, such as when using Struts2 tags later, using loop tags, there will naturally be cyclic variables, which are stored in the value stack

2: Generalized value stacks

Usually refers to the Actioncontext object, Actioncontext is the context in which the action runs, and each actioncontext is a basic container that contains the data that the action needs to run, such as request parameters, sessions, and so on.

Actioncontext is thread-safe, with each thread having a separate actioncontext so you don't have to worry about the thread-safety of values in the value stack.

Actioncontext contains a lot of values, typically such as:
Request parameters: The parameters in the requests, note that the data here is copied from the request object, so here the data changes are not affected by the request object inside the value of the parameter
Request attribute: A property in the requested, which is actually a map that holds the property data of the requested object, the data and the attribute of the requested object are connected
Session attribute: The attribute in the conversation, which is actually a map that holds the property data of the session object, the data and the attribute of the conversation object are connected
Application's attribute: The attribute in the application, which is actually a map that holds the attribute data of the Application object, the data and the attribute of the applied object are connected
Value stack: That is, the narrow value stack, actioncontext with value stack as the root of the OGNL access, in short, ognl in the case without special indication, access to the value of the stack of data
attr: Get values in all attribute ranges, search page, request, session, and application in turn.

As we have already learned that xwork is irrelevant to the web, the action does not have to rely on any web container to interact with the Servlet's API, but the action needs to have access to the data of the Web application, not just the value of the request parameter. It is often necessary to get some data for the request or session directly in the action, which can now be obtained through Actioncontext.

3: About Broad and narrow

You will see that in the Actioncontext is actually contains a narrow value stack, it is for this reason, plus actioncontext also contains other data, so the actioncontext called the generalized value stack.

In the future, when it comes to value stacks, there is no specific case, most of which is referred to as the generalized value stack, which is all about getting values from the value stack anyway. Of course, there is one case except that when using OGNL on a page, the default is to value from value STATCK without a special identifier.

Iv. Basic use of Actioncontext


Before learning the basics of value stacks, let's look at how the value stack is used in the program.

1: How to get

There are two basic ways to get actioncontext, and if you can't get to actioninvocation, you can use Actioncontext a static GetContext method directly, You will be able to access the current Actioncontext, as shown in the following example:

Actioncontext CTX = Actioncontext.getcontext ();  
If you can get to the actioninvocation, such as inside the interceptor, custom result inside, etc., can be obtained through actioninvocation to Actioncontext, the example is as follows:

Actioncontext CTX = Actioninvocation.getinvocationcontext ();  

2: After getting, how to use

The main function of Actioncontext is to store the data in a typical way: get (String key): Gets the corresponding value from the Actioncontext current storage space according to key put (string key, Object value) : Store value in actioncontext storage space map<string,object> getapplication (): Returns the value stored in ServletContext map<string,object > getsession (): Returns the value stored in HttpSession map<string,object> getcontextmap (): Returns the value of the current context store Map<string,object > getparameters (): Returns the parameters stored inside the HttpServletRequest object, Valuestack getvaluestack (): Gets the value stack of OGNL

For the GetXXX method, there is a corresponding setxxx method, here will not go into the details, please refer to the STRUTS2 API documentation. v. Basic use of Valuestack

In the previous section, I saw that Valuestack is included in Actioncontext, Valuestack is also used to store objects, but it is mainly accessed through OGNL expressions, that is, in Struts2, it is mainly accessed by tags.

Valuestack has a feature that if there are multiple objects in the value stack that are accessed, and the same attributes appear in multiple objects simultaneously, the value stack looks for the first matching object in the order from the top of the stack to the bottom of the stack.

1: How to get

Can be obtained directly by the Getvaluestack () method of the Actioncontext object

2: How to use

Valuestack main functions are also used to store data, typical method is as follows: Object findvalue (String expr): According to the expression in the value stack, in the default access order to get the value of the expression corresponding to void SetValue (String expr, object value): Sets the value in the values stack according to an expression, in the default order of Access Object Peek (): Gets the top-level object in the value stack, does not modify the value of the Stack object Pop (): Gets the top-level object in the value stack and removes the object from the value stack with void push (object O): Adds the object to the value stack object and sets it as the top-level object

3: application Example

In the previous example, the Welcome page displays the account number that is filled in from the login page and passed to the background of the data, if you want to modify the Welcome page to display the account data, but the front from the login page to fill in and pass to the background of the data do not need to change, then how to implement it.

First of all, to modify the value of the result page display, it is necessary to modify the value before the result processing, otherwise it is meaningless to change after the result processing is finished. Therefore, you can choose Preresultlistener technology, in which the value is modified, and then the result processing.

On the other hand, the Welcome page is tagged to get the data of the account and show it, that is, the value is the source is the value stack, therefore, in the Preresultlistener is to modify the values in the stack.

Well, after you know what to do, take a concrete look at the example.

(1) First to implement the Preresultlistener, in the inside of the value stack to modify the values, examples are as follows:

public class Mypreresult implements preresultlistener{  
.    public void Beforeresult (actioninvocation actioninvocation, String result) {  
.        System.out.println ("now handles the function before result execution, result=" +result);  
Actioninvocation.getinvocationcontext (        ). Getvaluestack (). SetValue ("Account", "Modified");  
The.    }  
06.}  
(2) Implement the Preresultlistener, also need to register before running, here choose to register the Listener in action, the example is as follows:

public class Helloworldaction extends Actionsupport {  
.    Private String account;  
Password.    private String;  
.    private String Submitflag;  
Public    String Execute () throws Exception {  
.        This.businessexecute ();        actioncontext C = actioncontext.getcontext ();        Mypreresult PR = new Mypreresult ();        c.getactioninvocation (). Addpreresultlistener (PR);        return "Towelcome";  
One.    }  
.    /** 
.     * An example method that represents a method that can perform business logic processing 
.     * *  
.    public void Businessexecute () {  
.        SYSTEM.OUT.PRINTLN ("user input parameter = = = =" + "account=" +account+ ", password=" +password+ ", submitflag=" +submitflag ");  
.    The Getter/setter method that corresponds to the attribute is omitted by  
19.}  

(3) The corresponding Struts.xml is relatively simple, the example is as follows:

01.<package name= "HelloWorld"  extends= "Struts-default" >  
.        <action name= "helloworldaction" class= "Cn.javass.action.action.HelloWorldAction" >  
.            <result name= "Towelcome" >/s2impl/welcome.jsp</result>  
.        </action>  
.    </package>  

(4) The login page is not changed, examples are as follows:

01.<form action= "/helloworld/helloworldaction.action" method= "POST" >  
.    <input type= "hidden" name= "Submitflag" value= "Login"/>  
.    Account: <input type= "text" name= "accounts" ><br>    Password: <input type= "password" name= "password" ><br>  
.    <input type= "Submit" value= "Submission" >  
06.</form>  
(5) Look at the Welcome page again, there are no changes, examples are as follows:

<%@ taglib prefix= "s" uri= "/struts-tags"%>  
02. Welcome to <s:property value= "account"/> Friends visit  
(6) To run the test to see, the Welcome page is displayed should be modified values, as shown in the following figure:



As a rule, it is not a lot of opportunities to use valuestack directly because the values in the value stack are pressed into the Struts2, and the access value stack is mostly through the OGNL expression in the tag.


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.