Question

I am doing my first steps with Struts 2 and the struts2-convention-plugin, currently working on a CRUD action class (SongCrudAction) with some typical create(), edit(), view() and delete() action methods. All those methods are annotated with @Action.

Generally, I'd like to inherit the useful default behavior from ActionSupport, but that also includes inheriting the execute() method.

Using the configuration browser, I can see that there is an action named song-crud pointing to it. That song-crud action is obsolete and I'd like to get rid of it, as I've got my own action methods for all desired operations. I guess, thanks to Struts2 conventions, that song-crud action is assumed although I haven't declared it anywhere.

In this particular case, one possible solution would be to give some reasonable meaning to the execute() method, e.g. I could use it for the view() operation. A drawback would be that the action class should then be renamed (or annotated?) according to the execute() method's behavior (e.g. ViewSongAction instead of SongCrudAction). Otherwise, the view action's URL would be inappropriate (i.e. /view-song?id=5 would fit better than /song-crud?id=5).

On the other hand, renaming the class would be ugly, too, because it's just a CRUD and not a view action class.

Anyway, the general problem remains: whenever I inherit from ActionSupport, it presents me with an execute() method which may or may not be useful for what I'm doing.

So, it seems reasonable to somehow tell Struts 2 to exceptionally forget about the execute() method in my CRUD action class. Is that right, and how can I achieve that? Is there a better solution?

Was it helpful?

Solution

The execute method will run by default if you not specify the method attribute in the action config. And if you not use DMI to run your methods. The ActionSupport is already implemented the execute method as it implemented the Action interface. But the note that appear with exclamation image below looks really strange.

If there is no execute method and no other method specified in the configuration the framework will throw an exception.

that is true anyway, the opposite if you have the execute method, and other methods then which method will be executed? You could omit the method attribute in the action config and use DMI to call any method in the action including the execute method if no method is specified.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top