Render image in SpringMvc - java

I want my image show up in receipt_failure.html via such thymeleaf expression :
<img th:src="#{/src/main/resources/images/icons-alert-circle.png}" />
Image itself has settled in such SpringMVC folder structure:
I do not get where issue comes from because seems like I keep path structure but I am getting such error on my local environment

I moved /images folder into /static folder then fix html like this:
<img th:src="#{/images/icons-alert-circle.png}" />
and issue has been fixed

Related

How to use Thymeleaf / Spring / Eclipse to show image by '<img th:src '

problem:
<img src="#{/images/0.jpg}" alt="error occured"/>
I am using Eclipse and folder images is in src/main/resources/static/images and Thymeleaf template which uses:
<img src="#{/images/0.jpg}" alt="error occured"/>
is in folder: src/main/resources/templates
The image is for example: 1.jpg
No, th:src is 'Thymeleaf Standard Dialect'. If your project is properly set up, spring/thymeleaf will be able to find your image on the webserver (e.g. Tomcat) by the given relative path itself. You do not need any own controller.
I hope this will help you: https://stackoverflow.com/a/29475520/9158389

Add a source from another file in eclipse

Probably a ridiculously easy question here but I must be phrasing it weird in all of my search queries to find similar solutions.
So in my eclipse project I have a folder with some .jsp files in it. In another folder there are some .jpgs . I want to use one of these .jpg files in my .jsp but for some reason cannot get the classpath correct.
I tried right clicking and copying the qualified name and using that path but it wont link correctly for some reason...
my code looks like :
<img src="pikachu.jpg" height="300" />
I've also tried:
<img src="/My_Project_Name/WebContent/images/pikachu.jpg" height="300" />
Note: the Jsp is in:
/My_Project_Name/WebContent/JSP_FOLDER/JSP.jsp
thanks in advance - I know this should be a simple thing...
You need to use
<img src="<%= request.contextPath %>/images/pikachu.jpg" height="300" />
in your JSP.
<% request.contextPath %> will expand to the path under which your web server is serving your app.
Every resource inside of WebContent/ will then be accessible relative to this path.
Related:
http://kodejava.org/how-do-i-get-web-application-context-path-in-jsp/
Similar to Aaron's answer but I believe he has a typo in it (missing an equal sign).
Try this instead, which in my opinion is a little less messy:
<img src="${pageContext.request.contextPath}/images/pikachu.jpg" height="300" />
Aaron's solution would have been:
<img src="<%= request.getContextPath() %>/images/pikachu.jpg" height="300" />
If those don't work, view the page source and copy/paste us what the line looks like when you are using those.
Use
<img src="../images/pikachu.jpg" height="300" />
..(double dots) will move you to parent directory where you are having images folder.

image is not displayed after getting the path from the database

I have tried the image uploading code in java.Images are upload in -
D:\bridge\Project\.metadata\.plugins\ org.eclipse.wst.server.core\tmp0\wtpwebapps\bridghc\Example\image-folder\2nd page.jpg this directory.
This fully path is also inserted in database.When i fetch the path from database i get the same result but when i write
<img id="0" src="D:\bridge\Project\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\bridghc\Example\image-folder\2nd page.jpg" name="txtrepimg0">.
In the web page i am not able to fetch the image.I got failed to load url.
Please help.
The src attribute in the img tag only accepts web addresses as targets, not local directories. So the path should look something like http://mysite/images/myimage.jpg
You should store relative path into database not Actual path of file. you should only store
file name, as your folder is fixed to store images
2nd page.jpg
and use the following in img tag
<img id="0" src="application_name/your-relative-path-of-image-folder/2nd page.jpg
" name="txtrepimg0">
like
<img id="0" src="/bridghc/Example/image-folder/2nd page.jpg" name="txtrepimg0">
In your case application name is bridghc
Try change your upload path to something relative to your website location.
For example, if your webfiles(html, css, javascript, etc...) are in:
D:\bridge\Project\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\bridghc\Example
add a folder like "image-folder" in a relative path like
D:\bridge\Project\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\bridghc\Example\image-folder
save the relative path in database
\image-folder\image.png
and use it in your website
<img id="0" src="\image-folder\image.jpg" name="txtrepimg0">

Linking Files And Directory Structuring

I'm working on a web project using java/ jsp/ servlets/ html/ css in eclipse tomcat, where everything is in the WebContent folder.
In my jsp files... When I try to include other jsp files (using a link like "/fileName.jsp" in jsp include directive) I can do that successfully.
But When I try to include image files (using a link like "/fileName.jpg" in the <img src=""> tag) nothing happens.
Nothing happens because instead of looking in the WebContent folder for image file it looks in the tomcat home directory, i.e.
Instead of looking at "http ://localhost:port/projectName/..." it looks at "http: //localhost:port/..."
Why does it look at the wrong location only with <img src=""> tags but not in <%# /> tag.
A workaround for this is that I start giving absolute paths "/projectName/..." However doing this means I'm hardcoding project name everywhere. This is what I do not want.
Don't include binary content in an ascii output. Why not just use the img tag? If you need to do something to produce a jpeg, I would use a Servlet.
Because the jsp-Links in the website are getting processed and the image links not. Either change the image path or develop an filter that changes the images'links.
Yes Templar, that could have been a way to solve my problem.
However, I simply changed the Context Root of my project from "Project Name" to "/" in Eclipse. This solved my problem.

unable to locate exact location of image file in jsp page

i am saving my images through java coding in a folder called "files".
and in my jsp page i am trying to display that image using
<img alt="" src="${pageContext.request.contextPath}/files/IMG168.jpg"> But the image is not displaying! What could be the reason.
Please see the below images and suggest me solution for my problem.
You should put the files folder in WebContent directory.
Web pages contains as below
Web pages
|______page1.jsp
|______index.jsp
|______page2.jsp
|______WEB-INF
|______images
|______css
|
Just try this and see
<img alt="" src="../../files/IMG168.jpg">
Updated,
I think you have to get physical location of files folder and put it in to src. Example if your files folder in C:\visionbook\files your
src="C:\visionbook\files\IMG168.jpg"

Categories

Resources