How do Action class constructors work with Struts2?

冷暖自知 提交于 2019-12-10 23:57:10

问题


I am directly forwarded to a method within my action class (by struts.xml) but I do not actually create a new instance of my Action Class. I ask because I have some variables to set before anything can be done with the ActionClass, and I thought constructors might help me do just that.


回答1:


Struts 2 provides a Prepare Interceptor to prepare your data before the actual execute method runs.This interceptor calls prepare() on actions which implement Preparable. Some of the use cases for using the prepare method in your action class are

  1. Loading an object from the database so that when parameters are set they can be set on this object.
  2. Preparing/init any data which is needed before the actual method call.

In short this interceptor is like an init/constructor for the Action class and you have all control to initialize any required parameters or do any initial lookup.

For details refer to the interceptor page prepare-interceptor



来源:https://stackoverflow.com/questions/9520279/how-do-action-class-constructors-work-with-struts2

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