I can't access files that are not stored in src/main/ project path
Project structure:
Now I can access all files from webapp/resources but can't access files from userResources folder
Spring resourceHandler:
#EnableWebMvc
#Configuration
public class AppConfig extends WebMvcConfigurerAdapter {
//some configs
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resource/**").addResourceLocations("/resources/");
registry.addResourceHandler("/vendors/**").addResourceLocations("/vendors/");
//this line won't work
registry.addResourceHandler("/userResources/**").addResourceLocations(
"home/pika/workspace/Spring/Bookmarks/userResources")
);
}
}
In view
<img class="fb-image-lg" src="/home/pika/workspace/Spring/Bookmarks/userResources/admin/profile/wallImage.png" alt="Profile wall image" align="left">
Image not found, I try to change src with: "/userResources/admin/profile/wallImage.png" anyway it doesn't work
<img class="fb-image-profile thumbnail" src="/resources/images/defaultProfilePhoto.png" alt="Profile face image" align="left">
Found image.
What I'm doing wrong?
PS Github repo may it helps you to understand project structure
The resource location "home/pika/workspace/Spring/Bookmarks/userResources" is looked for inside the web app, not on the file system (just like the other starting with "/resources"). To target the file system, use a file URL:
file:/home/pika/workspace/Spring/Bookmarks/userResources
This is however a terrible idea: the web server where you'll deploy that application doesn't have the same directory structure as the one you have on your developer machine. You should include these resources inside the webapp. Or store these images in your database. Or on the file system, but then at least with an externalized based directory, that isn't hard-coded like this in the code.
Related
My Maven Project Structure is Like Below.I want To Load Css In mainMenu.jsp File.
<link rel="stylesheet" href="../Main.css">
Above Line Is Not Working.So How Can I Load Main.css in mainMenu?
Don't confuse your WEB-INF layout with what the browser sees. Move your Main.css to "webapp" (or, more commonly "webapp/style"), and then reference it as "Main.css" (if in webapp) or "style/Main.css" (if in webapp/style).
Under WEB-INF, you typically only want files which are processed server-side, eg. JSP (when using a ViewRenderer) or web.xml.
Spring Boot automatically adds static resources located within any of the following directories as mentioned here:
/META-INF/resources/
/resources/
/static/
/public/
Hence, the mistake you are making is by placing static content in wrong directory.
I Have Solved Issue By Adding Below Line OF Code In Web Configuraion And Add All Front End File In resources Folder.
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}
I have a maven project with spring-mvc configurations with the hierarchy mentioned in below image. I've placed list.js file inside all the possible places in src folder and its sub folders but I couldn't access it from my list.jsp file. What am I doing wrong here? Can anyone help me to figure it out?
Static Resources like JS, CSS, Images,etc visible from webapp folder in a web application.
So, you can create js folder under webapp folder and place your list.js file inside it.
Also, you need to tell Spring to not process these static resources path.
Looks like you had defined your Spring Configuration in Java Classes i.e. WebConfig. You need to add below code
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
}
Lastly, change your path in jsp file like this
<script
src="${pageContext.request.contextPath}/js/list.js"></script>
Hope it helps to solve your problem.
I have a image file inside
enter image description here
Here is my configuration inside Configuration class.
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/META-INF/resources/", "classpath:/resources/**",
"classpath:/static/**", "classpath:/public/" };
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
Inside Jsp I am not able to load the image under div tag
<div id="image">
<p>
<img src="/images/reviewCount.jpg" width="600" height="400" border="0" usemap="#chart">
</p>
I tried
1) To give absolute path like src/main/resources/static/images/reviewCount.jpg
2) src="{/images}/reviewCount.jpg"
Both the approaches went in a wine.
Any one who can some shed light would be appreciated.
TIA.
regards
Vinod
Spring Boot automatically maps static folder in the resources to serve static content. Just put images like this:
You have to place all the images, css, js files inside your webapp folder.. ie webapp/resources.
If you are packaging the application as a jar then dont use the src/main/webapp
this will only work with war packaging and it will be ignored by most build tools if you generate a jar.
In Spring boot by default, resources are mapped on /** but you can tune that via spring.mvc.static-path-pattern. For instance, relocating all resources to /resources/** can be achieved as follows:
spring.mvc.static-path-pattern=/resources/**
Read more from here
It is a typo. You have reviewCount.png in static/images folder and you are referring reviewCount.jpg from div tag.
Today i have a problem with Bootstrap in my Spring Boot app.
Spefically, when i start the app and open the browser, i can see my page but with "classic" html; that is, i don't see the page formatted with the css of Bootstrap but with normal html5 (so, Bootstrap is nota loaded).
What i have done?
First, i've download Boostrap framework; then i try to put the 3 folder css, js and fonts under both this path:
src/main/webapp (first)
src/main/resource/static (then)
In the second case, i tried also to move from /static folder into resource, that is src/main/resource
When i've put this 3 folder in resource and resource/static, i tried 2 configuration:
add in application.properties file this line of code: spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
Use a MvcConfiguration class that extends the WebMvcConfigurerAdapter class and override the method addResourceHandlers this way:
public void addResourceHandlers(ResourceHandlerRegistry registry)
{
registry.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}
Of course, done this, in my jsp file (located under src/main/webapp/WEB-INF/jsp) i'v put the 2 line of code to tell the page where take the css and js file:
<link href="/resources/static/css/bootstrap.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="/resources/static/js/bootstrap.min.js"></script>
and of course, these line change from /resource/static/ with /webapp when i try to put under src/main/webapp the 3 Bootstrap folder. But, after this, i cannot see Bootstrap loaded. What i wrong?
Update: solved. The problem was based on a setting in my applications.properties:
server.port=8080
server.contextPath=/polaris
that is, the change of the context path. A usefull tread on this site is this: Spring 4 - addResourceHandlers not resolving the static resources
Using in JSP by link href and give path like static/css/somename.css
Do 4 Step:
1.maven-Clean
2.update project
3.maven install
4.Run As Spring Boot App
Hope it will help.
The Path Of Any CSS Should be Like This in Spring Boot
You can refer following link for spring boot with boostrap template on web application. example_link. You have to change following property values on application.properties file inside the above application for jsp page load. Make sure inside webapp folder having correct name.
spring.mvc.view.prefix=/html/
spring.mvc.view.suffix=.html
I am new to Spring framework in spring site There is tutorial at https://spring.io/guides/gs/uploading-files/ that upload file to the root folder "upload-dir" (this folder is beside src root folder)
questions:
How can I access and show image in browser (or access it in thymeleaf by th:src="#{}" syntax) -
by browsing to localhost:8080/files/first.jpg because of controller it give me download link.
should I always upload file to folder that beside src folder for example I want to upload file to "src/main/resources/static/file" is it possible?
When accessing files in your code, Spring will (by default) assume that the src/main/resources is the parent directory. If you are planning on accessing the files that are uploaded, then I would use src/main/resources (or a subdirectory of this location) as the upload path. This way, you can simply access them in Thymeleaf as such:
Location: src/main/resources/picture.jpg
Thymeleaf: th:src="#{picture.jpg}"
Or if the file exists in a subdirectory:
Location: src/main/resources/somedir/picture.jpg
Thymeleaf: th:src="#{somedir/picture.jpg}"
If you are storing the file(s) elsewhere, then you can also access them using various prefixes like classpath or file, i.e.:
classpath:com/myapp/config.xml
See more about Resources in Spring here:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/resources.html
Hope this helps!