Action class execution flow in Struts

≯℡__Kan透↙ 提交于 2020-03-27 07:03:40

问题


I was in an impression thatexecute() method in Struts is the first method called when action class is invoked from struts.xml. But when I debug my code which has a constructor in action class, I see that constructor gets executed first(I am not creating any instance here).

1) Can some please explain the flow of execution in action class, i.e. what methods are called before actually getting to execute.

2) I am not sure why constructor is getting called


回答1:


Struts calls a lot of stuff before the action is executed. But it's configurable in struts.xml. You can see the request flow of the action execution from this answer.

Internally Struts uses the ObjectFactory to build all objects defined by the configuration. See more about ObjectFactory docs or The Struts 2 Request Flow.


Constructors should have the default constructor or no-argument constructor, otherwise it won't be constructed. For detailed explanation about differences you can see Java default constructor.

You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument constructor of the superclass. In this situation, the compiler will complain if the superclass doesn't have a no-argument constructor so you must verify that it does. If your class has no explicit superclass, then it has an implicit superclass of Object, which does have a no-argument constructor.


The methods are called mostly by interceptors before the action is executed. The default interceptor stack is defaultStack. It has a lot of functionality involved that is executed before the action. See this answer to get impression about default configuration.

Interceptors are invoked before the action execution. It's like a pipeline that serves the request. Each of them invoked one after another. When the last interceptor processed the action is executed. What interceptors to invoke configured in the action configuration or via annotations. For detail explanation of interceptors see docs.




来源:https://stackoverflow.com/questions/43592332/action-class-execution-flow-in-struts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!