Loading image in Spring application - image

I have this kind of solution in my Spring application:
http://www.java2s.com/Code/Java/PDF-RTF/AddingPNGimagetoPdfdocument.htm
Anyhow my problem is that my picture is in different folder like WEB-INF/layout/picture.gif.
I don't get it to work. Is there any disrictions, maybe?

Move your picture to the classpath as a resource, so then you can load it to iText easily. In order to do that you should copy the image file to the directory where the classes are, so the image is packed into the JAR or alternatively is in /WEB-INF/classes directory if this is JAva webapp (war).
Factory method of com.lowagie.itext.Image takes java.net.URL as argument, so you do this:
URL imageUrl = getClass().getResource("/your/image/package/image.png");
Image image = Image.getInstance(imageUrl);

This answer assumes that you try to access the images from a web browser directly.
Images that can directly accessed by an browser have to be located outside of the WEB-INF directory.
For example in a maven project
-project/main/src
-webapp
-images <- in the same parent directory like META-INF and WEB-INF
-META-INF
-WEB-INF

Related

Spring MVC Controller opens wrong .jsp file

my project structure looks like below:
The issue is when I return in HomeController .jsp file named "home" and run this project on Tomcat server I receive website but not from home.jsp, but index.jsp (1 screenshot).
My question is is there any file where starting .jsp file is defined as a launcher ? If not so why index.jsp always launches while Controller Class returns a different file ?
Solved
I have created a new project (Maven project + Web archetype) and works fine. I assume that a problem was with location of web folder. Now I have it inside src folder unlike in previous structure.

How to add directory and subdirectories in static location in spring boot

I want to use resources from static directory and this directory may have another subdirectories.
In spring boot application.properties file I am using below key to refer outside directory:C:/custResources/. I am able to refer resources from this directory but not from the subdirectoriese e.g. C:/custResources/1, C:/custResources/2 etc.
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:/C:/custResources/
Looks like I am doing something wrong.
Below property is sufficient to add the whole directory and its folder as static location.
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:/C:/resources
I am able to access video placed inside C:/resources/1 as localhost:8080/1/sample.mp4 and so on.

Can we provide JSP pages from sec/main/resources source folder

I am trying to provide JSP pages under sec/main/resources instead of webapp folder and configured source folder in application.properties as below but not working, let me know can we do this and how
spring.mvc.view.prefix=/src/main/resources
spring.mvc.view.suffix=.jsp

what is the url that I can use to access to a folder images in Spring Boot

Hey I have a problem with Spring boot what is the url that I can use in my controller for save images In a folder exist in : " resources/static/images "
You can access the static resources by just appending the path of them inside the static/public folder to your base URL.
Example:
You have an image in the folder src/main/resources/static/images/brand.png, the URL for that would be ${host}/images/brand.png.

How to import css/js in spring MVC application jsp page

I am new to spring mvc , I am creating a user registration page , there I need to put external css and import external js .
I put the script tag with src and link tag with href. But i think in Spring MVC application they will not work straight forward.
For this I added resourceservlet mapping but still it is not working
I am not able to find any way how I csn include these assets in my jsp file.
Please help me to get this one.
Regards,
Pranav
Put the CSS, JS, image files, etc., into a directory (which may contain subdirectories) in your web site, and than map the requests to that directory:
<mvc:resources mapping="/resources/**" location="/public-resources/"/>
In this example the files are in the public-resources directory, and you refer to them using /resources in the link in your JSP.
For more information see Spring docs

Resources