I am making a Qute Template and there is a logo image in the header that I need to have loaded.
The path that is being used within the html is this:
<img style="width: 10cm;" src="logo.png" alt="Logo Flower">
The image path from the main folder is resources/META-INF/resources/logo.png.Click here to see the folders.
When the template is rendered, the image is not loaded and shows an error on the GET request:
GET http://localhost:8080/api/user/logo.png 404 (Not Found)
Does anyone have an answer on how to put the path of static files correctly in Qute templates?
It seems your HTTP path is wrong. It should look like this:
GET http://localhost:8080/logo.png
Try to inspect (CTRL + I) your page go to sources and try to figure out where is the problem...
Here is example on my project: META-INF/resources/logo.png
resources path
, and here is my img tag <img id="logo" src="logo.png" alt="Logo">
Related
I am using thymeleaf as my template engin to map XHTML to HTML and flying saucer to generate a pdf file afterwards.
Now i failed to display my static images located at /src/main/resources/ inside y generated pdf file. The file itsself will be displayed fine only images disapear.
Even other locations like /src/main/resources/static or /src/main/resources/public didnt help.
My HTML / XHTML looks like:
<img src="images/logo_black.png"></img>
<img src="/images/logo_black.png"></img>
<img alt="mastercard" th:src="#{classpath:static/images/logo_black.png}" />
<div data-src="images/logo_black.png"></div>
<div data-src="/images/logo_black.png"></div>
<div data-src="#{classpath:static/images/logo_black.png}"></div>
none of them is working properly.
The Images itself are visible by localhost:8048/logo_black.png
I dont want to refer my images with a full url (http://...)
You can include resources from any URL (from the Internet or from your file system). Either way, there are several steps involved:
When generating the HTML from the Thymeleaf template, you can use
#{/some/url} to resolve a path relative to your Web context (assuming you have a Web context), or
#{classpath:/some/url} with will just leave the URL as classpath:/some/url, or
simply a string value constant or a value from a variable (${var}), doesn't matter if it's an absolute URL https://some/url or relative, Thymleaf will leave them unchanged in the resulting HTML.
Before you pass the HTML to Flying Saucer, make sure the URLs are correct. Then Flying Saucer will process all URLs with a UserAgentCallback, by default ITextUserAgent.
The relevant methods in UserAgentCallBack are resolveURI and setBaseURL.
There is some weird logic going on in the default resolveURI method of ITextUserAgent (inherited from NaiveUserAgent). If the baseURL is null, it will try to set it, so it's best to always set it yourself. I had better results with overriding the resolveURI, the following is enough to keep absolute URLs and resolve relative URLs relative to the baseURL:
#Override
public String resolveURI(String uri) {
if (URI(uri).isAbsolute())
return uri;
else
return Paths.get(getBaseURL(), uri).toUri().toString();
}
Finally, in order to resolve the classpath: protocol, you need to define an URLStreamHandler unless there is already one defined (for example, the embedded Tomcat of Spring Boot already does supports this).
You can render image with the help of base 64 .You just convert your image on base 64 and it will show on your web page as well as mobile view.The tags are:
<img th:src="#{data:image/png ;base64,your base 64}"/>
i have a struts2 action which has a variable for an image which i want to display in a jsp page. when i display the value of the variable using the following:
<s:property value="variableName" />
i get the absolute path of the image file.
but when i try the following:
<img src='<s:property value="variableName" />' />
i get a blank there. i have even tried the following:
<img src="${variableName}" />
EDIT: i have done some thinking. the path of the image which is set is in temp folder of tomcat. is it because of that that i am unable to access it?
to no effect. what may be the issue in this?
Always keep in mind when doing web development what you have in server context and what travels to the client browser. If you do what you are suggesting, you will render and send to the client something like this:
<img src="c:\my_local_directory\some_image.jpg"></img>
Which means nothing to the client browser, which is surely executing in some other machine.
You should store your images somewhere where your application server can show them, and give always paths that are relative to your web application. For example, if you have stored some_image.jpg straight in your my-app.war file, something like:
<img src="/my-app/some_image.jpg"></img>
will always work. If you need to refine that image (imagine you need to put a watermark on it before) or need to recover its contents from somewhere (a BLOB in a database or something like that), you will need an entire new action in order to do it, for example:
<img src="/my-app/ImageAction.action?image=some_image"></img>
The src of the img tag must contain an absolute path, then only it can point to the image file from the client machine.
If the variableName contains an absolute path like this
http://localhost/images/pics/icon.gif
The img will finds the image from the server. Thus available always an hence you can use this.
<img src="${variableName}"/>
If the variableName is a relative path ie; something that's recognisable for the server.
/images/pics/icon.gif
The above path will be identified by the server but it wont be identified from the remote machine. In that case you need to have this
<img src="<c:url value=variableName/>" />
Let's say i have created the file
String path = application.getRealPath("userSearchFolder");
String name = path + "/" + (String) session.getAttribute("username") + ".xml";
File file = new File(name);
And later I want to make it available as a link, for example
<a href"<%=file.toURI()%>">File</a>
What happens is I get the directory path not url path ->
file:/D:/Documents/NetBeansProjects/2012/GATE_Project/build/web/userSearchFolder/mjoraid.txt.xml
And when it reaches Firefox, I hover over the link and what i see is
file:///D:/Documents/NetBeansProjects/2012/GATE_Project/build/web/userSearchFolder/mjoraid.xml
When I right click and choose Copy link Location and paste it in URL the xml file opens, but when I click the link, nothing happens.
How could I get a link like this
http://localhost:8080/GATE_Project/somepage/somepage/mjoraid.xml
The getRealPath will give a File system path (hence "real"), as opposed to web app path. So you cannot make it a href.
The following should suffice.
<a href="/userSearchFolder/${userName}.xml">
(Of course you are risking data mining for such public accessible XML files.)
You could use a servlet to serve the file.
This tutorial shows how to serve a pdf file(!)
the theory is the same:
you load the file in the servlet
Set any required headers
write the data to the response
The ContentType should probably be "application/xml".
Ok, i did it manually, similar to how i used to do it in php, create a variable that contains the website main directory.
String searchFolderURL = "http://localhost:8080/GATE_Project/userSearchFolder/";
and then
<a href="<%=searchFolderURL + file.getName()%>" target="_blank" >See original txt File </a>
Thanks btw.
I want to use the following js function to insert an image on a webpage, but I am not referencing the icon file correctly.
function insertWarningIcon(value){
return "<img src='/icons/Warning-icon.png' width='25%' height='25%' />";
}
The image is contained in a java package 'icons.' The file's path is:
C:\Users\XXXX\Documents\NetBeansProjects\distributedTaskMonitor\src\java\icons\Warning-icon.png
The html file's path is:
C:\Users\XXXX\Documents\NetBeansProjects\distributedTaskMonitor\web\gridMain.html
you can't point to src package from web pages, you have to put them into "WebContents" folder (it's outside of src folder)
eg.
WebContents/images/icons/icon1.png
is
<img src="images/icons/icon1.png"/>
I want to render content that i have created locally using html component and put image in this html also by putting image in res folder in jar, i tried
<img src='images/down.png'></img>
<img src='res/images/down.png'></img>
<img src='./images/down.png'></img>
but nothing worked, any suggestion?
[EDIT]
here is my code, i have no idea how to implement DocumentRequestHandler that is why i used DefaultDocumentRequestHandler
DocumentRequestHandler handler = new DefaultDocumentRequestHandler();
HTMLComponent component = new HTMLComponent(handler);
component.getStyle().setBorder(Border.createLineBorder(1));
component.getSelectedStyle().setBorder(Border.createLineBorder(1));
component.setBodyText("<div><b>nirmal:</b>" +
"<img src='res://images/down.png' /></div>");
tried res://images/down.png but nor worked
my image is in res/images
You need to explain how you loaded the HTML, images are loaded relatively to the base URL so you need to define the base URL when creating the HTML (its implicitly detected when loading via URL).
If you created the HTML via setHTML(String) then you need to give absolute paths depending on your DocumentRequestHandler implementation e.g. res://myImage.png or file://myImage.png .