Struts2基本疑惑
我是 Struts2 的新手...我有一些疑问如下...
a) 在 Struts2 中,应用程序如何找到 struts.xml? (我们不在 web.xml 文件中定义 struts.xml,与 Struts1 不同,我们在 web.xml 文件中定义 struts-config.xml,那么它如何看到 struts.xml 文件)
b) 为什么我们不写 < code> 为我们的 Action
类扩展了 ActionSupport 。我见过很多例子,但没有使用任何其他预定义的 Action
类进行扩展。它是如何找到 如果我们不扩展任何其他预定义的操作类或实现 Action 接口方法,我们的 Action 类中的execute()
方法或 populate()
方法?
c) 在什么情况下我们使用extends Action Support
Im new to Struts2.... I have some few doubts below...
a) In Struts2, how does application finds struts.xml?? (we don't define struts.xml in web.xml file unlike in Struts1 which we define struts-config.xml in web.xml file, so how its going to see that struts.xml file)
b) why dont we write extends ActionSupport
for our Action
class.. I have seen many examples without extending with any other predefined Action
class.. How does it find execute()
method or populate()
method in our Action class if we dont extend any other predefined action class or implement Action interface methods ??
c) In what cases we use extends Action Support
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
a) 如果您不覆盖配置文件名(web.xml 中 Struts 过滤器的“config”参数),则它将默认为“struts.xml”。这只是一个硬编码的默认值,因此是“按约定配置”。
b) 该框架允许“普通旧 java 对象”(POJO) 进行操作。只需告诉它要调用什么方法(在struts.xml中),它就会使用反射来找到这样的方法(必须是无参数并返回一个字符串)并调用它。另一方面,某些接口用于附加功能,例如,如果您的类实现了 Preparable,则在执行之前将自动调用prepare() 方法(可能类似于 Struts1 中的“populate”?)
c) 扩展 ActionSupport 是完全可选,但可以访问一些可能有用的功能,例如某些操作方法(例如“输入”)的默认实现、国际化的便捷方法等。
a) If you don't override the configuration file name ("config" parameter to the Struts filter in web.xml), then it will default to "struts.xml". This is simply a hard-coded default, hence "configuration by convention".
b) The framework allows "plain old java objects" (POJOs) for actions. Just tell it what method to call (in struts.xml), and it will use reflection to find such a method (must be no-args and return a String) and call it. On the other hand, some interfaces are used for additional functionality, for example if your class implements Preparable then the prepare() method will automatically be called prior to execution (perhaps probably similar to "populate" in Struts1?)
c) Extending ActionSupport is entirely optional, but gives access to some functionality that might be useful, such as default implementations for some action methods such as "input", convenient methods for internationalization, etc.
托德的回答+1。
b) :请注意,无需指定方法(尽管可以这样做),默认情况下(“约定”)将调用
execute()
方法。对于 c) :扩展 ActionSupport 是可选的,并且 IMO 非常频繁。有时,还建议实现您自己的(例如)
BaseAction
(它经常扩展ActionSupport
),以分解出您的网络应用程序的常见功能,并使所有(或几乎所有)你的行动延长了它。+1 to Todd's answer.
To b) : notice that there is no need to specify a method (though one can do it), by default ("convention") the
execute()
method will be called.To c) : extending
ActionSupport
is optional, and IMO quite frequent. Sometimes it's also advisable to implement your own (say)BaseAction
(which frequently extendsActionSupport
) to factor out the common functionality of your webapp, and make all (or nearly all) your actions extend it.