Detailed explanation of single case and multiple cases of Action in Struts

  • 2021-12-13 16:41:10
  • OfStack

Detailed explanation of single case and multiple cases of Action in Struts

There are many examples of action in struts2, that is, one action will be generated every time the network address is accessed


public class pr_action {
  public pr_action(){
    System.out.println(" Create action Success! ! ! ");
  }
  public void execute(){

  }
}

Running the code, you can see that every time you access the network address, it will be output once in the console! ! !

If it is a singleton, if two users modify the attribute value of one object, the attributes accessed by two users are different and the results obtained by operation are different because of the different modification time of users.

For example, there is a piece of cloth with a length of 300cm, which can make a coat (using 100cm) and a trouser (using 200cm); The length obtained by accessing both A and B at the same time is 300cm,
A wants to make tops and trousers. He first intercepted 100cm to make a coat, Wait until the coat is finished before making pants. At this time, B just took 100cm to make a coat. Well, when A finished making a coat and then made trousers, it was found that the remaining cloth (100cm) was not enough to make trousers... This affected the performance of the system. The solution is to give A and B a piece of 300cm cloth, so that there will be no cloth stolen by others, which is also the difference between single instance and multiple instances

If it is set to singleton, multiple threads will share one ActionContext and one ValueStack, so there will be problems when accessing concurrently

The Action of struts 2 is a multi-instance, not a singleton, that is, an object that produces one Action per request. The reason: struts 2 contains data in Action, for example, the data you fill in on the page will be included in the member variables of Action. If Action is a single instance, these data will interact with each other in a multithreaded environment, for example, causing the data filled in by others to be seen by you. Therefore, Action of Struts2 is multi-case mode.

The problem arises. Can action of struts2 be changed into singleton mode? When I used spring to generate action, I found that the generated action was all singleton. Doesn't this make my program run out of bug by default? If the information submitted by the previous user is not filled in by the next user, it actually goes to the information entered by the previous user.

Background:

1) Struts2 generates an instance of Action for every request.

2) The Ioc container-managed bean of Spring is single-instance by default.

First of all, from the perspective of data security, our Action should be guaranteed to be multiple cases, so that there will be no data problems. However, if some action, such as only admin can operate, or some action, the whole station shares one to improve performance, then the singleton mode can be used.

Fortunately, bean of Spring can set its scope for every one, so the above problem is not a problem. If you use multiple cases, set scope = "prototype" during action bean configuration for spring. Well, that's the end of the problem.

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: