Detailed explanation of Action singleton and multiple examples in Struts, and detailed explanation of StrutsAction

Source: Internet
Author: User

Detailed explanation of Action singleton and multiple examples in Struts, and detailed explanation of StrutsAction

Detailed explanation of Action singleton and multiple examples in Struts

In struts2, actions are multiple examples, that is, each time a network address is accessed, an action is generated.

Public class pr_action {public pr_action () {System. out. println ("action created successfully !!! ") ;}Public void execute (){}}

Run the code and you can see that every time you access this network address, it will be output in the console !!!

If it is a Singleton, if two users modify the attribute values of an object, the modification time is different because the attributes accessed by the two users are different and the operation results are different.

For example, a cloth with a length of 300 cm can be used as a coat (with a length of 100 cm) and a trousers (with a length of 200 cm ); the length obtained by both Party A and Party B is 300,
A wants to make a coat and trousers. He first intercepts 100cm to make a coat. After the coat is finished, he will make the trousers. At this moment, B will take 100cm as the coat. That's good, when I finished my coat and made my pants, I found that the rest of the cloth (100 cm) was not enough to make my pants ..... This is the impact on the system performance. The solution is to provide a 300cm cloth for Party A and Party B, so that they will not be stolen by others. It is also the difference between a single instance and multiple instances.

If it is set to a Singleton, multiple threads will share one ActionContext and ValueStack, so that problems may occur during concurrent access.

The Action of struts 2 is a multi-instance, not a Singleton, that is, an Action object is generated for each request. The reason is: struts 2 Action contains data. For example, the data you enter on the page will be contained in the member variable of the Action. If the Action is a single instance, the data will affect each other in a multi-threaded environment. For example, the data entered by others will be visible to you. Therefore, the Action of Struts2 is in the Multi-sample mode.

If the problem occurs, can the struts2 action be changed to the singleton mode? When I used spring to generate an action, I found that all the generated actions were single-instance. Isn't this making my program run out of bugs by default? Information submitted by the previous user. If the next user does not fill in the information, the information entered by the previous user is actually gone.

Background:

1) Struts2 generates an Action instance for each request.

2) by default, beans managed by Spring Ioc containers are single instances.

First of all, from the perspective of data security, we should ensure that the Action is multi-instance so that data problems do not occur. However, if some actions, such as only admin, can be operated, or some actions, are shared by the whole site to improve performance, then the singleton mode can be used.

But fortunately, Spring bean can set its scope for each, so the above problem is not a problem. If multiple examples are used, set scope = "prototype" during spring action bean configuration. Okay, the problem ends here.

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

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.