Is it best practice to map multiple actions to the

2019-01-12 11:04发布

问题:

I am developing contact manager in struts with options(add,delete,modify,print).

I am thinking to map multiple actions(map multiple actions to the same class struts 2.3) to the same class(ContactManager).

I want to know whether it is correct or whether there is need to write separate action for each action?

回答1:

One good way to go, imho , is to use one Action for every action you have to perform, each one extending its last-forked parent Actions, like:

BaseAction
|
|----------BaseReportAction
|          |----- ExcelReportAction
|          |----- PDFReportAction
|          |----- CSVReportAction
|
|
|----------BaseCRUDAction
|          |----- CreateAction
|          |----- ReadAction
|          |----- UpdateAction
|          |----- DeleteAction
|
|
|----------BaseAJAXAction
|          |----- ReadSessionCountdownAction
|          |----- CheckNewMailsAction
|

etc...

Every Action extended by others will share protected attributes / methods to the children.

Take a look at this: Changing parameters after bind in Struts 2

My 2 cents.



回答2:

I think you should use a single action. It is totally a good practice, after all they provided the facility to invoke different methods in the same Action class for a purpose! You can achieve something like this by including a wildcard which which can map your action name to appropriate method in Action class.Like this-

    <action name="abc*" method="{1}" />