• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts2 basic doubts..

 
Harishwar Madhya
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Harishwar Madhya
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic