Struts2 Basic doubts
Im new to Struts2.... I have some few doubts below...
a) 开发者_C百科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
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 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 extends ActionSupport
) to factor out the common functionality of your webapp, and make all (or nearly all) your actions extend it.
精彩评论