Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can one read a text file in a Struts 2 app [duplicate]

Developing a Struts 2 app I run in a following problem. I need to read a text file that is deployed in web server with my app. How can I access it knowing its relative path. In other words how can I find absolute path if I know relative path inside the deployed directory. When I had similar problem with servlets I used to use this.getContextPath() (or something similar) that returned an absolute path to the folder in the webserver.

EDIT: Thank you guys for answer. For me worked this:

String path=GetPointsOfInterestAction.class.getResource("../../../resources/visitor_attractions.txt")

Could you please explain why I it worked because I'm making first steps in java.

like image 208
Eugeniu Torica Avatar asked Nov 23 '09 14:11

Eugeniu Torica


People also ask

What are best practices to follow while developing Struts2 application?

Some of the best practices while developing Struts2 application are: 1. Always try to extend struts-default package while creating your package to avoid code redundancy in configuring interceptors. 2. For common tasks across the application, such as logging request params, try to use interceptors.

How do you use strut tags?

To use the Struts 2 tags on the view page, you must include a tag library directive. Typically, the taglib directive is <%@ taglib prefix="s" uri="/struts-tags" %> . So the prefix for all the Struts 2 tags will be s .

What is ActionSupport in Struts2?

ActionSupport class implements a no. of interfaces like Action, Validateable, LocaleProvider and Serializable etc. It is more commonly used instead of Action interface.

What are the technologies there in struts framework?

Struts is an open source framework that extends the Java Servlet API and employs a Model, View, Controller (MVC) architecture. It enables you to create maintainable, extensible, and flexible web applications based on standard technologies, such as JSP pages, JavaBeans, resource bundles, and XML.


3 Answers

If it is placed in the webapp's classpath, then just use:

InputStream input = servletContext.getResourceAsStream("file.txt");

If it is placed in the global classpath, then use:

InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("file.txt");

If it is placed in the webcontent, then just use:

InputStream input = new FileInputStream(servletContext.getRealPath("file.txt"));

The examples assumes that they're placed in the root. You can of course use relative path as opposed to the classpath root or webcontent root, e.g. path/to/file.txt. You can get the ServletContext in Struts by ServletActionContext#getServletContext().

Edit: You edited your question with following:

EDIT: Thank you guys for answer. For me worked this:

String path=GetPointsOfInterestAction.class.getResource("../../../resources/visitor_attractions.txt")

Could you please explain why

This is actually not the "right" way, but this is also doable. You only need to make sure that you know the relative path of the file as opposed to the actual path of the GetPointsOfInterestAction class. This class is of course placed in a different package, so you basically need to step a directory back (like you do in normal disk file systems: cd ../ and so on). Again, this is not the most elegant way. You need to use one of the first two aforementioned ways, with the resource name resources/visitor_attractions.txt.

like image 154
BalusC Avatar answered Oct 02 '22 19:10

BalusC


Here

ServletActionContext.getServletContext()

You seem to know what to do from there.

like image 22
Bozho Avatar answered Oct 02 '22 18:10

Bozho


You could use a Resource Bundle. I use them to look up properties files. I believe that the directory just needs to be on the Classpath and the ResourceBundle will find it.

like image 29
Poindexter Avatar answered Oct 02 '22 19:10

Poindexter