Understanding of value stacks in Struts2

Source: Internet
Author: User
Tags type null

I. Preliminary knowledge-memory allocation of the program
The memory used by a program compiled by C + + is divided into the following sections
1. Stack (stack)-Automatically allocated by the compiler to release, store the function parameter value, local variable value and so on. Its
Operations are similar to stacks in data structures.
2, heap area (heap)-Generally by the programmer assigned to release, if the programmer does not release, the end of the program may be the OS back
Inbox Note that it is not the same as the heap in the data structure, the distribution is similar to the list, hehe.
3, Global Zone (Static)-, the storage of global variables and static variables is placed in a block, initialized
Global variables and static variables in an area, uninitialized global variables and uninitialized static variables in adjacent
An area. -released by the system after the program is finished.
4, literal constant area-the constant string is put here. Released by the system after the program is finished
5. Program code area-binary code that holds the function body.


In a nutshell, a complete Java program will run with the following memory areas:

1. Register: JVM internal virtual register, access speed is very fast, the program is not controllable.

2, stacks (different virtual machines have different definitions of JVM stacks and local method stacks): the values for saving local variables are: 1. Save the value of the base data type; 2. Save the reference variable , which is a reference (pointer) to the heap-area object . It can also be used to save the frame when the method is loaded.

(1), the stack memory is thread-private, his life cycle and the same thread.

(2), data sharing in the stack.

(3), storing basic types of variable data, local variables, and references to objects, but the objects themselves are not stored in the stack, but are stored in the heap (new objects) or in a constant pool (string constant objects are stored in a constant pool.) )

3. Heap: used to store dynamically generated data, such as new objects, arrays. Note the objects that are created contain only the member variables that belong to them, and do not include member methods. Because objects of the same class have their own member variables stored in their own heap, they share the methods of the class, and not every object is created to copy the member method one at a time.

(1), the Java heap is a piece of memory that is shared by all threads and is created when the virtual machine is started.

(2), Java Heap is the main area of garbage collection, so often referred to as "GC" heap, garbage collection is now generally based on generational collection, so Java heap can also be subdivided into: the new generation, the old age; subdivision is Eden Space, from Survivor space, to Survivor space.

4. Method Zone: the information, constants, static variables, and the immediate compiler compiled code used to store the loaded classes (the role of Static in Java is to explain the variable, the method, whether the code block belongs to a class or an instance).

5, Chang: The JVM maintains a constant pool for each loaded type, and the constant pool is an ordered set of constants used by this type. Includes direct constants (basic types, String) and symbolic references to other types, methods, and fields (1). The data in the pool is accessed through the same index as the array. Because a constant pool contains symbolic references to other types, methods, and fields for one type, Chang plays a central role in the dynamic linking of Java. A constant pool exists in the method area.

(1), storing string constants and basic type variables, such as String str= "www"; In fact, "www" is in the constant pool inside.

(2), Chang is in the method area instead of in heap memory.

(3), Java 8 basic types (Byte, short, Integer, Long, Character, Boolean, Float, Double), except for Float and double, the other six have implemented a constant pool, but they are only in the A constant pool is used only if it is greater than or equal to 128 and less than or equal to 127. A constant pool is not used if it is greater than 127 or less than 128, so objects are created directly in heap memory.



1. A Java file, as long as there is a main entry method, we think this is a Java program, can be compiled and run separately.

2. Whether it is a variable of a normal type or a variable of a reference type (commonly known as an instance), it can be used as a local variable, and they can all appear in the stack. Only the normal type of variable in the stack directly save its corresponding value, and the reference type of the variable is a pointer to the heap area, through this pointer, you can find this instance in the heap corresponding to the object. As a result, a normal type variable occupies only one chunk of memory in the stack, whereas a reference type variable takes up a chunk of memory in the stack area and the heap area.

Basic Types andthe wrapper class for the base type. The basic types are:byte, Short,Char,int,Long,Boolean. The basic types of wrapper classes are:Byte, Short,Character,Integer,Long,Boolean. Note case sensitive. The difference between the two is that the basic type is the normal variable in the program, and the basic type of wrapper class is the class, which is the reference variable in the program. Therefore, they are stored in memory in different places: the base type is stored in the stack, and the base type wrapper class is stored in the heap


Second, Java divides the memory into two kinds, one is called the stack memory, is called the heap memory

some basic types of variables and object reference variables defined in the function are allocated in the stack memory of the function. When a variable is defined in a block of code, Java allocates a memory space for the variable in the stack, and when the scope of the variable is exceeded, Java automatically frees the memory space allocated for that variable, which can be used immediately by another.

heap memory is used to hold objects and arrays created by new . The memory allocated in the heap is managed by the Java Virtual Machine automatic garbage collector. After creating an array or an object in the heap, you can also define a special variable in the stack that is equal to the array or the first address of the object in the heap memory, and this particular variable in the stack becomes the reference variable of the array or object. You can then use the reference variable in the stack memory in your program to access the array or object in the heap, which is the equivalent of an alias, or codename, of an array or object.

A reference variable is a normal variable that is defined when memory is allocated in the stack, and the reference variable is released in the program run to the extraterritorial scope. The array and object itself is allocated in the heap, and even if the program runs beyond the block of code that uses the new generation of arrays and objects, the heap memory that the array and the object itself occupies will not be freed, and arrays and objects become garbage, no longer used, but still occupy memory when no reference variable points to it. is released by the garbage collector at a later indeterminate time. This is also the main reason for the memory of Java comparison, in fact, the variables in the stack point to the heap memory variables, this is the pointer in Java!

A comparison of memory allocation strategies and heap and stack in Java
1 Memory allocation policy
According to the compiling principle, there are three kinds of strategies for memory allocation when the program runs, which are static, stacked, and stacked.
   Static Storage AllocationThis means that at compile time it is possible to determine the storage space requirements for each data target at run time, so that they can be allocated a fixed amount of memory at compile time. This allocation policy requires that the existence of mutable data structures (such as variable groups) not be allowed in the program code, and that nested or recursive structures are not allowed to appear. Because they all cause the compiler to not be able to calculate the exact storage space requirements.
A stack storage allocation can also be called dynamic storage allocation, which is implemented by a stack-like run stack. In contrast to static storage allocation, in a stack storage scenario, the requirements for the data area are completely unknown at compile time, only to be known when running, but when the rules are entered into a program module You must know the size of the data area required by the program module to allocate memory for it. As with the stack we are familiar with in the data structure, the stack storage allocation is distributed according to the principle of advanced post-out.
Static storage allocation requires that the storage requirements of all variables be known at compile time, and that the stack storage allocation requires that all storage requirements be known at the entrance of the process, while the heap storage allocation is specifically responsible for the memory allocation of the data structures that the storage requirements cannot be determined at compile-time or at runtime module entrances. such as variable-length strings and object instances. The heap consists of large chunks of available or free blocks, and the memory in the heap can be allocated and freed in any order.
2 comparison of heaps and stacks
The above definition is summarized from the compiling principle of the textbook, in addition to static storage allocation, it is very inflexible and difficult to understand, the following aside static storage allocation, centralized comparison heap and stack:
From the function and function of heaps and stacks to the popular comparison, A heap is primarily used to store objects, and stacks are primarily used to execute programs.This difference is mainly due to the characteristics of the heap and stack:
In programming, for example, C + +, all method calls are made through stacks, all local variables, and formal parameters that allocate memory space from the stack. It's actually not an assignment, just up the top of the stack, like a conveyor belt in the factory (conveyor belt), stack pointer will automatically guide you to where you put things, All you have to do is put things down. When you exit a function, you can destroy the contents of the stack by modifying the stack pointer. This mode is the fastest, of course, to run the program. It is important to note that when allocating a data area for a program module that is about to be called, The size of this data area should be known in advance, but it is said that although the allocation is carried out at the time of the program running, the size of the allocation is determined, unchanged, and the "size of how much" is determined at compile time, not at runtime.

Heap is when the application is running to request the operating system to allocate its own memory, because of the memory allocation from the operating system management, so the allocation and destruction of time, so the efficiency of the heap is very low. But the advantage of the heap is that the compiler does not have to know how much storage space to allocate from the heap. It is also not necessary to know how long the stored data will stay in the heap, so there is greater flexibility in storing the data with the heap. In fact, object-oriented polymorphism, heap memory allocation is essential, because the required storage space for polymorphic variables can only be determined after the object is created at run time. In C + +, when you require an object to be created, you only need to compile the relevant code with the new command. When you execute the code, the data is saved automatically in the heap. Of course, to achieve this flexibility, there will inevitably be a price: it takes longer to allocate storage space in the heap! This is the reason why we just said that the efficiency is low, it seems comrade Lenin said good, people's advantages are often human shortcomings, People's shortcomings are often

《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》

1. Valuestack

Valuestack in the Chinese version of the "Struts2 in Layman's" a book "Value stack." Its own data structure is a stack, the user can put some objects (also known as beans) into the value stack, and then use a dynamic expression to read the bean properties, or some other operation of the bean. Because there may be more than one bean in the value stack, the value stack attempts to read the value sequentially using a dynamic expression in the order of the Bean's stack, until the value is successfully read. In Struts2, the default value stack implementation is Ognlvaluestack, which uses the OGNL dynamic expression language to read the value by default.

During Struts2 execution of a request, Struts2 automatically puts the current action object into the value stack. In this way, when rendering the JSP, the code in the JSP uses the OGNL expression in the <s:property value= "..."/>, which directly acts on the Action object, which makes it easy to read the properties of the action.

How to get the value stack:
    • In a custom interceptor, use the Actioninvocation.getstack () method (Actioninvocation is the method parameter of the Interceptor).

    • In the action class, let the interceptor inject Valuestack or use Actioncontext.getcontext (). Getvaluestack () to the value stack (Actioncontext.getcontext () is a static method). Note: The way Actioncontext assigns a context is thread-based, and if you use this method, make sure it does not go wrong.

    • In JSP, the data in the value stack can be obtained directly by using the tag, and the value stack itself is not usually obtained.

How to store objects in the value stack:
    • Struts2 automatically deposit action: previously mentioned, STRUTS2 will automatically deposit the current action object into the value stack during a request.

    • The Modeldriveninterceptor will deposit the action's Model property: If you use the Modeldriveninterceptor provided by STRUTS2, it will take the action object's Getmodel () Method gets the object into the value stack. At this point, the bottom of the value stack is the action class, followed by the model.

    • Store a value stack in a custom interceptor: Call the Valuestack.put (Object object) method after you get the value stack object.

    • The value stack is stored in the action class: The Valuestack.put (Object object) method is called after the value stack object is obtained.

    • Store value Stack in JSP: Label <s:push value= "..." ></s:push> is specifically used to put the specified value into the value stack in a JSP, but value is placed in the value stack only within the s:push tag, that is, the program runs to The </s:push> tab moves value out of the value stack. In addition, there are some tags such as <s:iterator/> because of the function of the need to put some objects into the value stack.

Let the value stack execute an expression to get the value:
    • In a custom interceptor, after getting the value stack, use Valuestack.findvalue (...). and other methods.

    • In the action class, after you get the value stack, use Valuestack.findvlaue (...). and other methods.

    • In JSP, some tag properties are directly executed on the value stack, such as the Value property of <s:property/>. OGNL expression. If the label's properties are not directly executing the OGNL expression, you need to surround the expression with "%{}" so that the Struts2 is executed with the OGNL expression. Please refer to the full official documentation for which tags are directly executed OGNL and which are not.

Skip the top element of the stack in the JSP to access the second layer directly:
    • In JSPs, expressions such as [0], [1] are used to specify the execution of an expression starting from the first level of the stack. [0] means starting from the top of the stack, [1] representing the beginning of the second layer of the stack. For example, the expression "name" is equivalent to "[0].name". See here.

《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》《》

Accessing the value stack object itself (not their properties) in the JSP
    • Use the top keyword in the expression to access the object itself. For example, the expression "name" is equivalent to "top.name", The expression "[0].top" is equivalent to "top", the expression "[1].top.name" is equivalent to "[1].name".

In short, the main purpose of the value stack is to allow the JSP to easily access the properties of the action.

through a period of contact with the struts2, will own the core value of the OGNL stack said, value stack: Simply, is to store the action stack, when we submit a request to the server side action, there is a stack, if the action on the server side to jump, All actions share a stack, and when you need to save the data in the action, search first from the top of the stack, and if the same property name is found (same as the property name of the data to be obtained), the value is taken out, but it is possible that the found value is not the value we want. The solution to this problem is to use the top syntax and the n syntax.
When jumping on the customer service side, when there is a request to commit to the server action, only one stack exists, the stack is stored in the current requested action, and the original then destroyed (in my opinion, feel like request requests).

****************************************************** ********************************
It is well known that the action class of strut 2 can obtain all relevant values through attributes, such as request parameters, action configuration parameters, passing property values to other actions (via chain results), and so on. The only thing we have to do to get these parameter values is to declare a property with the same name as the parameter in the action class, and before Struts 2 calls the action method of the Actions class (the default is the Execute method), the corresponding Action property is assigned a value.
to do this, the Struts 2 relies heavily on the Valuestack object. This object runs through the life cycle of the action (each object instance of an action class has a Valuestack object). When Struts 2 receives a request for an. Action, the object instance of the action class is established, but the action method is not called, but the corresponding property of the action class is first placed in the The top-level node of the Valuestack object (the Valuestack object is equivalent to a stack). Just all of the property values are default values, such as the value of the property value of the string type Null,int type is 0, and so on.
after processing the above work, Struts 2 invokes the interceptor in the interceptor chain, and when all the interceptors are called, the action method is finally called, and before the action method is called, the The property value in the top-level node of the Valuestack object is assigned to the corresponding property in the action class. It is important to note that this gives us a lot of flexibility. That is, in the process of calling the interceptor in Struts 2, the value of the property in the Valuestack object can be changed, and when a property value is changed, the corresponding property value of the action class becomes the value of the last change in the interceptor.
from the above description it is easy to know that the action class in struts 2 can get the parameter value with the same name as the property is handled by different interceptors, such as the interceptor that obtains the request parameter is the params, get the action configuration parameter of the block The cutter is staticparams and so on. Read the corresponding values inside these interceptors and update the values of the corresponding properties of the top node of the Valuestack object. The Valuestack object, like a conveyor belt, passes the attribute value from one interceptor to another (of course, in the meantime, the property value may change), eventually to the action object, and assigns the value end value of the property in the Valuestack object to the corresponding property of the action class
It is well known that the action class of strut 2 can obtain all relevant values through attributes, such as request parameters, action configuration parameters, passing property values to other actions (via chain results), and so on. The only thing we have to do to get these parameter values is to declare a property with the same name as the parameter in the action class, and before Struts 2 calls the action method of the Actions class (the default is the Execute method), the corresponding Action property is assigned a value.
to do this, the Struts 2 relies heavily on the Valuestack object. This object runs through the life cycle of the action (each object instance of an action class has a Valuestack object). When Struts 2 receives a request for an. Action, the object instance of the action class is established, but the action method is not called, but the corresponding property of the action class is first placed in the The top-level node of the Valuestack object (the Valuestack object is equivalent to a stack). Just all of the property values are default values, such as the value of the property value of the string type Null,int type is 0, and so on.
after processing the above work, Struts 2 invokes the interceptor in the interceptor chain, and when all the interceptors are called, the action method is finally called, and before the action method is called, the The property value in the top-level node of the Valuestack object is assigned to the corresponding property in the action class. It is important to note that this gives us a lot of flexibility. That is, in the process of calling the interceptor in Struts 2, the value of the property in the Valuestack object can be changed, and when a property value is changed, the corresponding property value of the action class becomes the value of the last change in the interceptor.
from the above description it is easy to know that the action class in struts 2 can get the parameter value with the same name as the property is handled by different interceptors, such as the interceptor that obtains the request parameter is the params, get the action configuration parameter of the block The cutter is staticparams and so on. Read the corresponding values inside these interceptors and update the values of the corresponding properties of the top node of the Valuestack object. The Valuestack object, like a conveyor belt, passes the attribute value from one interceptor to another (of course, in the meantime, the property value may change), eventually to the action object, and assigns the value end value of the property in the Valuestack object to the corresponding property of the action class


Note: Organize and publish

Http://my.oschina.net/u/1425737/blog/197939?fromerr=Url6hTpc

Http://www.cnblogs.com/jerryxing/archive/2012/04/23/2467299.html

Understanding of value stacks in Struts2

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.