aspose file tools*
The moose likes Struts and the fly likes Struts2 basic doubts.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts2 basic doubts.." Watch "Struts2 basic doubts.." New topic
Author

Struts2 basic doubts..

Harishwar Madhya
Greenhorn

Joined: Sep 01, 2010
Posts: 14
Im new to Struts2.... I have some few doubts below...

a) In Struts2, how does it find struts.xml?? (we dnt 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 seeen 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 dnt extend any other predefined action class or implement Action interface methods ??

Waiting for quick reply...


Harishwar
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

a) the default behavior is to look for struts.xml file at the root of the classes directory. Even I think in Struts 1, if you don't give any details in web.xml, it automatically looks for struts-config.xml. You can override this default behavior through the web.xml file.

b) Struts 2 can use POJOs as actions. So your action need not extend or implement any Struts class/interface. By default struts looks for a method named "execute" in your action class. You can however configure your action to execute any method when a request is received...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Harishwar Madhya
Greenhorn

Joined: Sep 01, 2010
Posts: 14
Ankit Garg wrote:a) the default behavior is to look for struts.xml file at the root of the classes directory. Even I think in Struts 1, if you don't give any details in web.xml, it automatically looks for struts-config.xml. You can override this default behavior through the web.xml file.



In Struts1, if we look at the code for ActionServlet, then you can see that by default the value for "config" = "/WEB-INF/struts-config.xml" , so thats the reason even if we dont specify the path for struts-config.xmlin web.xml, it takes that default value.. Assume if we change the name of struts xml file or if we change the path for it, then we MUST definitely has to specify that config value in web.xml, otherwise it cannot see...


But Im not understandng exactly in case of Struts2.. How does it find "struts.xml" file and how does it look for execute and populate methods automatically..

I will be very much appreciated if anyone would give an exact answer for my doubt... waiting for..



Harishwar
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You know you can look at the source, right?
Harishwar Madhya wrote:How does it find "struts.xml" file

It tries loading it.
and how does it look for execute and populate methods automatically..

Reflection, if the class doesn't implement Action.
Harishwar Madhya
Greenhorn

Joined: Sep 01, 2010
Posts: 14
Hey david, thanks for your reply... Whats the concept of Reflection?? Could you explain what it is?

you said "It tries loading it." ?? means?? There should be some way to know about struts.xml file for the application.. Any idea how does our application knows where does struts.xml placed ??


Harishwar
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Reflection:

http://download.oracle.com/javase/tutorial/reflect/index.html
http://java.sun.com/developer/technicalArticles/ALT/Reflection/

Config file:

Assuming the developer hasn't explicitly defined a config file location, S2 will attempt to load struts.xml from the root of the classpath as a resource. I'm not sure what else I can tell you--a resource either exists at a given location, or it doesn't. If it's there, it'll get processed. If it's not, it won't.
Harishwar Madhya
Greenhorn

Joined: Sep 01, 2010
Posts: 14
Yeah I agree that if our struts.xml file will be there inside /WEB-INF folder, then it automatically loads that file.. But my actual doubt is "How does it look that file"?? If anyone knows please kindly tell me.. :)



Harishwar
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I told you: it attempts to load the file as a resource from the root of the classpath (*not* at /WEB-INF--Struts 2 looks on the classpath).
 
Consider Paul's rocket mass heater.
 
subject: Struts2 basic doubts..