简体   繁体   English

我应该/可以让 Struts 2 约定忘记操作的 execute() 方法吗?

[英]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.

相关问题 Struts2-在动作的execute方法内访问请求对象 - Struts2 - Access request object inside action's execute method 从 Action 的 execute() 方法创建一个新的 Struts Action 实例有什么问题吗? - Any problem with creating a new Struts Action instance from the Action's execute() method? Struts 2没有调用action类的execute()方法 - Struts 2 not calling action class execute() method 当我在struts2 + spring3项目中添加struts2-rest-plugin时,struts操作未采用默认方法(执行) - struts action is not taking the default method(execute) when i add struts2-rest-plugin in struts2+spring3 project 我可以在 Struts 2 中映射一个返回对象的操作方法吗 - Can I map an action method that returns Object in Struts 2 我可以制作一个带有将要执行的参数的方法吗 - Can i make a method that takes arguments that it will execute 如何将参数化方法传递给Struts 2动作的method属性? - How to pass a parameterised method to a Struts 2 action's method attribute? 我正在使用struts1,关于动作名称 - I'm using struts1, about the action name Struts2-如何使用 <s:a> 之前不知道我将要使用的操作? - Struts2 - How can I use <s:a> without previously knowing the action that I'll use? Struts 2-动态设置方法名称,从一个Action调用另一个Action方法,以执行 - Struts 2 - Dynamically set the method name, from one Action to call another action method, to execute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM