[英]Should / Can I make Struts 2 conventions forget about an action's execute() method?
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.我正在使用 Struts 2 和struts2-convention-plugin做我的第一步,目前正在使用一些典型的
create()
、 edit()
、 view()
和delete()
操作方法处理 CRUD 操作类 ( SongCrudAction
)。 All those methods are annotated with @Action
.所有这些方法都用
@Action
注释。
Generally, I'd like to inherit the useful default behavior from ActionSupport
, but that also includes inheriting the execute()
method.通常,我想从
ActionSupport
继承有用的默认行为,但这也包括继承execute()
方法。
Using the configuration browser, I can see that there is an action named song-crud
pointing to it.使用配置浏览器,我可以看到有一个名为
song-crud
的动作指向它。 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.那个
song-crud
动作已经过时了,我想摆脱它,因为我有自己的动作方法来完成所有需要的操作。 I guess, thanks to Struts2 conventions, that song-crud
action is assumed although I haven't declared it anywhere.我想,多亏了 Struts2 的约定,虽然我没有在任何地方声明它,但假设了
song-crud
动作。
In this particular case, one possible solution would be to give some reasonable meaning to the execute()
method, eg I could use it for the view()
operation.在这种特殊情况下,一种可能的解决方案是为
execute()
方法赋予一些合理的含义,例如我可以将它用于view()
操作。 A drawback would be that the action class should then be renamed (or annotated?) according to the execute()
method's behavior (eg ViewSongAction
instead of SongCrudAction
).一个缺点是应该根据
execute()
方法的行为(例如ViewSongAction
而不是SongCrudAction
)重命名(或注释?)动作类。 Otherwise, the view action's URL would be inappropriate (ie /view-song?id=5
would fit better than /song-crud?id=5
).否则,查看操作的 URL 将是不合适的(即
/view-song?id=5
比/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.另一方面,重命名类也很丑陋,因为它只是一个 CRUD 而不是视图操作类。
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.无论如何,普遍的问题仍然存在:每当我从
ActionSupport
继承时,它都会为我提供一个execute()
方法,该方法可能对我正在做的事情有用也可能没有用。
So, it seems reasonable to somehow tell Struts 2 to exceptionally forget about the execute()
method in my CRUD action class.因此,以某种方式告诉 Struts 2 特别忘记我的 CRUD 操作类中的
execute()
方法似乎是合理的。 Is that right, and how can I achieve that?这是对的,我怎样才能做到这一点? Is there a better solution?
有更好的解决方案吗?
The execute
method will run by default if you not specify the method
attribute in the action config.如果您未在操作配置中指定
method
属性, execute
方法将默认运行。 And if you not use DMI to run your methods.如果您不使用DMI来运行您的方法。 The
ActionSupport
is already implemented the execute
method as it implemented the Action
interface . ActionSupport
已经实现了execute
方法,因为它实现了Action
接口。 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.
如果没有 execute 方法,也没有在配置中指定其他方法,框架将抛出异常。
that is true anyway, the opposite if you have the execute method, and other methods then which method will be executed?无论如何,这是真的,相反,如果您有 execute 方法和其他方法,那么将执行哪个方法? 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.如果没有指定方法,您可以省略操作配置中的
method
属性,并使用 DMI 调用操作中的任何方法,包括execute
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.