Home>

Single and multiple cases of action in struts

There are multiple actions in struts2,That is, an action is generated every time the network address is accessed

public class pr_action {
  public pr_action () {
    system.out.println ("Successful action created !!!");
  }
  public void execute () {
  }
}

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

If it is a singleton,If two users modify the property value of an object,Because the user modified time is different,The attributes accessed by the two users are different.The results are different.

For example:there is a piece of cloth with a length of 300cm, which can be used as a jacket (100cm) and a pair of pants (200cm);both A and B visit the same length of 300cm,

A wants to make jackets and pants,He first took 100cm to make a coat.Wait until the top is done before making pants,At this time, B also happened to take 100cm to make a coat.OK, when I finished the top and then made the pants, I found that the remaining cloth (100cm) was not enough to make pants ... This is the effect on the performance of the system,The solution is to give A and B a piece of 300cm cloth, and there will be no stealing of the cloth by others.Also the difference between single instance and multiple instances

If set to a singleton,Then multiple threads will share an actioncontext and valuestack, so that problems will occur when concurrent access

struts 2 action is multi-instance and not singleton,That is, an action object is generated for each request. The reason is:struts 2 action contains data,For example, the data you fill in the page will be included in the action member variables.If the action is a single instance,These data will affect each other in a multi-threaded environment,For example, you may see data filled in by others.Therefore, the action of struts2 is multi-case mode.

Problem occurred,Can I make struts2 action into singleton mode?When I use spring to generate actions, I find that the generated actions are all singletons.Doesn't this make my program run out of bugs by default?Information submitted by the previous user,If the next user does n’t fill it,Actually went to the information entered by the previous user.

background:

1) struts2 will do every request,Generate an action instance to process.

2) Spring's ioc container-managed beans are single-instance by default.

First consider the issue of data security,Our action should be guaranteed to be multiple cases,This will not cause data problems.But if there is an action such as only admin,Or some actions, a common site to improve performance,In this case,You can use the singleton pattern.

Fortunately,Spring beans can set its scope for each, so the above problem is not a problem.If multiple cases are used,Just set the scope="prototype" during the configuration of the spring action bean. Well, this is the end of the problem.

jsp
  • Previous Difference between drawable bitmap canvas paint in Android
  • Next JS to achieve image zoom and drag special code sharing