I am using the html:image tag to include images in the jsp. the image location is specified in the message resource properties file of struts. the paths are defined as
../../images/image1.gid previously. now the context is changing due to some reason, so the images are not being loaded. how do i access the context path in the message resource file? i tried key={0}/images/image1.gif, but this works only for the bean:message as it contains arg0 attribute. but how i do for the image? any round about way?
PS: <img src=<bean:message arg0="<%=request.getContextPath()%>" key="image1"/>> works. but i cant change in 100s of jsp files which uses html:image.
thanks
V
but i cant change in 100s of jsp files which uses html:image.
Use a text editor with Find & Replace in All Open Files facility, like Editplus and Notepad++.
Related
Is there any way to include an .vm file inside the .HTML file and return the .HTML file as the view in spring, I have seen example where the whole HTML changed to .vm and used as view. But I would like only one page to be changed as .vm with all other files still uses .HTML extension, is this possible in spring, could you please suggest
Regards
Vivek
I have two different requirements to use Freemarker Templates. One is to print some portion of the webpage and the other is to generate an customized HTML file. I have placed all the FTL files under the path WEB-INF/ftl/ The web application pulls the correct FTL file from the above location and prints the web page. When I had to generate the HTML file, the Freemarker is not able to locate the respective FTL file.
I tried to keep that particular FTL file under resources/ftl/, but still it could not pick up the file. I created a package under the src and placed the FTL. Maven does not take that package as it does not have java files in it.
Configuration cfg = new Configuration();
cfg.setClassForTemplateLoading(this.getClass(), "");
Template template = cfg.getTemplate("helloworld.ftl");
I can think of fixing this in two different ways. One way is to keep the file inside the project or in the classpath so as to be picked up by Freemarker. Other way is to get it as an InputStream and pass it to Freemarker. But I dont see any methods to accept the file as an InputStream or File.
ServletActionContext.getServletContext().getResourceAsStream("WEB-INF/ftl/helloworld.ftl");
Can you please let me know how to fix this issue?
I am able to fix this on my own.
There is a method available in the configuration where you can set the Template location and ServletContext.
cfg.setServletContextForTemplateLoading(context, "WEB-INF/ftl");
You just need to pass the servletContext object to the above method with the FTL location. When you process the template by the following code, it will just pick up the file and process it.
Template template = cfg.getTemplate("helloworld.ftl");
I have a Java project,exported as a JAR file (Desktop Application) which generates a HTML file as output. The output html file, needs to read one image file, as the page's logo. The JAR application will be in say X folder. The target html file will be placed dynamically anywhere. How do I make the html,residing in someother location, access the image, inside the JAR file.
In short, how do I determine the path for the below code, for the above scenario.
java.net.URL url = getClass().getResource("image.jpg");
fw.write("<tr><td><b>"+csname+"</b></td><td> <img src = "+url.toString()+"'>/td></tr>");
works fine, when i run in eclipse. But not when exported as JAR
The resultant html file,in some other folder has the code
<img src="rsrc:com/demo/dirapitoword/image.jpg">
You just need to read the image as a stream from the classpath, e.g.:
InputStream in = getClass().getResourceAsStream("image.jpg");
and write the stream out as a file to a known place on disk. There are lots of ways to do this, if you're using Java 7 or above, try:
File out = new File("image.jpg");
Files.copy(in, out.toPath());
Then, your src attribute can use the relative location you chose to display this image in the HTML, without having to worry about Jar compatibility in the browser / client.
You can find that the jar: URI Scheme exist for .zip containers as described in the Wikipedia here http://en.wikipedia.org/wiki/URI_scheme
The format is:
jar:<url>!/[<entry>]
BUT it is not supported by all browsers (only Firefox actually) as described in this article: https://code.google.com/p/browsersec/wiki/Part1
Even in that case, I think it is not very nice that the output .html can be elsewhere but contains an absolute path to your resources.
I suggest to use the data: URI scheme which allows to embed the image within the HTML page.
There is a question that covers this procedure: How to display Base64 images in HTML?
And in one of the answers, there is an interesting fiddle sample here: http://jsfiddle.net/hpP45/
I am making a web application using jsp and servlets .But am facing two problem that i have no ides how to remove :
Problem 1 : I am creating new folders in my WEB-INF folder .But what i want is that instead of giving full paths .I just provide relevant path Like :
File tempfilesstore = new File("C:\\Users\\admin\\Desktop\\SharedCrpto1\\web\\RetrievedFiles\\"+fileid+"-"+personname);
if(!tempfilesstore.exists())
tempfilesstore.mkdirs();
Can this full path be avoided as only path from web folder of the application is required.
Problem 2 : I keep a image in this folder by performing some operation on original image being browsed by the client on browser.
Now when i see the image in folder then it is present their But if i try to see the same image in browser it does not display the image .When i refresh my page for 3-4 times than sometimes it get displayed and sometimes after manually opening it by going to specified location.What can be reason for it ?Please help.
Here is how am trying to get image on browser :
<img src="RetrievedFiles/<%=path%>/<%=sharedfilee%>" alt="Image Preview Not Availablee" width="300" height="300" />
Here ,
String path=presentfileid+"-"+personname;
String sharedfilee=rs.getString("FILE_NAME");
First of all, in a JEE point of view, all files within the WEB-INF folder are not meant to be accessed by anyone but your server. It means that images, CSS files, javascript files, etc. in this directory will not be rendered by your web browser. Your JEE server will prevent that to happen.
So, in order to access files from your web browser, you need to put them outside the WEB-INF folder (at the same level, in a "images" folder, for instance).
For your first problem : Yes, you can use relative paths to instantiate files, using your classLoader.
this.getClass().getClassLoader().getResource("resourcePath")
or
this.getClass().getClassLoader().getResourceAsStream("resourcePath")
depending on your needs. The first one returns a URI (http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)) whereas the second returns an InputStream (http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#getResourceAsStream(java.lang.String)) that you can use with a FileInputStream.
The ressources are located in the classes folder, and the path is relative to the class your get the resource from. For example, you can use "/myImage.png" as a path to get the image at the root level.
Putting all your resources files in the "classes" folder (within subfolder if you want to) is a good architecture design.
For your second problem, you have mainly 2 solutions :
if the image is rendered without any transformation, put it in a folder outside WEB-INF (see the beginning of my comment), and it will be visible from outside. In you JSP, you can access it like that :
request.getContextPath() + "/" + sharedfile
if the image needs a transformation, use a servlet instead
I hope that helps you.
Regards,
Alexandre FILLATRE
I am trying to create a PDF file using struts 2.Action class location is as follows.
/home/Jagan/MATCH/Jagan/src/ActionClasses/PDFFile.java
Here workspace starts from
/MATCH
In PDFFile.java.I am writing as given below and it is working fine.
pdfwriter=PdfWriter.getInstance(document,new
FileOutputStream("/home/Jagan/xyz.pdf"));
But i have to create this under the folder
/home/Jagan/MATCH/Jagan/PDFs
I should not use /home/Jagan/ as it will become hardcode if i have to run this application in other system.
I tried
pdfwriter=PdfWriter.getInstance(document,new
FileOutputStream("../../../PDFs/xyz.pdf"));
But it is not creating file.Even if it works it is not feasible solution (because "../../../" does not work in windows ).
Please suggest me a good way to specify path for creating file.
Adding to the question.
I have to provide download option for downloading this created file in JSP page.Which struts tag should i use. Please provide me syntax for that
downloading files has 2 aproaches:
1. you can return it with outstream result like is explained here
2. as you are trying to save the file first at filesystem then access it from another url.
Answer to your question is you should get servletContext.getRealPath("/WEB-INF"), and after that everything is relative to WEB-INF.
I should not use /home/Jagan/ as it will become hardcode if i have to run this application in other system.
Correct, Use following instead
System.getProperty("user.home");///home/Jagan/, it will return you path to your home dir