I'm fairly new to Struts2. So here's the problem:
In my application I'm using Struts2 as my front-end and I have my own design for the back-end which contains Facades and DAOs. Now from one of my DAOs I'm trying to read a XML file in my web app. I tried to use ServletContextListener to get the context-param but it doesn't work. I don't know how to get a reference to the file. Any help on this issue would be appreciated.
You'll need to be more specific, and so far I don't see how this is Struts-related.
Joined: Apr 04, 2008
Thanks for the reply. Actually the problem is how to get a reference to a file in the web app context and since I was using Struts2 I thought this might be the right place to ask it. And to be more specific, I'm using Tomcat 6 and IntelliJ IDEA as my IDE. Here's the case: For example there's a XML file in folder named "pages" under the root folder. When I run the app when I try to access the file I get a FileNotFoundException. When I trace the exception I see that /pages/file.xml is appended to the Tomcat bin folder and not to the web app context. I also used getRealPath() method in ServletContext but it seems it doesn't work in the .war file. Thank you.
If it's a war file you're out of luck. You'll need to get the file as a resource from the class path.
Joined: Apr 04, 2008
David Newton wrote:If it's a war file you're out of luck. You'll need to get the file as a resource from the class path.
OK. Thank you David.
Here's what I got after searching. I figured that this problem is somehow the one many people have faced. And what are the solutions? If you want to read or write a file or resource in a Plain Java Object in the web application:
1. You have to put your resources in WEB-INF/classes and read them as a stream as David mentioned above.
2. For writing part you got no luck since POJO classes know nothing about the context they're running in. You can somehow pass the context to these classes but that ruins the whole idea of classes being reusable. I think the writing part got something to do with security issues so no one can write any file to your web application.
Of course you always can give absolute paths for reading and writing but I don't think that's a good idea if you want your application to be portable. But If your company got it's own servers I think you can write your file outside your application context. Well that's the only way for writing a file as far as I could find (To give paths outside the context).
Well I appreciate any idea on this.
And to Administrators: I started this thread in Struts forum for the reason I mentioned above. If this is not a right place for this thread please transfer it to the appropriate forum so people with the same problem can participate.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com