I'm trying to implement the versioning when serving static resources with Spring MVC. I need to force the clients reload the resources when a new version of the application is deployed.
I've followed the spring official guide here and I cannot let it work with the mvc:resources, but it works in the JSP pages.
Here my configuration in springMVC-servlet.xml:
<mvc:resources mapping="/style-#{applicationProps['version']}/**" location="/style" />
...
<util:properties id="applicationProps" location="/WEB-INF/conf/application.properties"/>
and this is the part of the jsp that is working:
<spring:eval expression="#applicationProps['version']" var="applicationVersion"/>
<link rel="stylesheet" type="text/css" href="/style-${applicationVersion}/style-common.css">
and finally this is the page source in the browser:
<link rel="stylesheet" type="text/css" href="/style-2.1-SNAPSHOT/style-common.css">
The problem is that this file is not mapped. When I click on it it says: resource not found...
Any idea how to proceed, debug, understand the issue?
Many thanks!
Replace
<mvc:resources mapping="/style-#{applicationProps['version']}/**" location="/style" />
By
<mvc:resources mapping="/style-#{applicationProps['version']}/**" location="/style-#{applicationProps['version']}/" />
Resource location attribute should be complete folder name.
Related
I have been searching all day, moved the style.css everywhere and still not managed to get it loaded. The images wont load as well.
My structure:
But if i click the firefox button, it loads:
This is how the style.css is imported in the head of the index:
(tried all kind of combinations)
When i check the developer tools, it says 404 for GET request (style.css and the pictures)
Spring Boot knows where the static directory is, so you don't need to use the ../ technique to get to the files within. The other part is that you should be using an annotation to get there. It may depend on what view template you are using, but for thymeleaf this is how you would achieve pulling in the css file:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>Your Title</title>
<link rel="stylesheet" th:href="#{/css/style.css}" />
</head>
Notice the annotation in th:href="#{/css/style.css}"
Make sure you include the namespace of your view template, th in the example.
Images work the same way:
<img th:src="#{/img/stanev2.png}" />
I'm working on a project in Spring MVC. I would like to connect CSS and js under it. However, for some reason, spring can not see these files. In the previous project which was configured using XML and everything worked fine. Any suggestion would be helpful.
Project Structure
<script type="text/javascript" src='<spring:url value="/resources/js/script.js"/>'></script>
<link href='<spring:url value="/resources/css/style.css"/>' rel="stylesheet" />
Configuration:
public void addResourceHandlers(final ResourceHandlerRegistry registry){
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
Move the resource folder under webapp folder. And with this line you can connect your css file.
<link rel="stylesheet" href="/css/styles.css">
Similarly write code to connect script like
<script type="text/javascript" src="/js/script.js"></script>
I'm having issues getting my css sheets to load in the browser for my spring webapp - I keep getting the following error:
The stylesheet http://localhost:8080/MyBudget/resources/css/hello.css
was not loaded because its MIME type, “text/html”, is not “text/css”.
File Strucutre
In my web.xml file, I have specified my resources as following;
<mvc:resources location="/resources/core/" mapping="/resources/**"/>
In my view (.JSP), I'm referencing the css file as following:
<link rel="stylesheet" href="<c:url value="/resources/css/hello.css"/>" type="text/css" />
I have tried just about everything to get this to work, and nothing - any suggestions?
Hi Thats coused by mvc configuration I think,
are you using load and startup configuraiton ?
I had same problem I solved by
spring.mvc.servlet.load-on-startup=1
to add my application.properties file
I read from here
https://www.javatpoint.com/load-on-startup
I'm learning Spring MVC. My static resources are successfully mapped using spring taglib. But resources are not mapped if i use JSTL c taglib.
Project Structure
web.xml
spring-servlet.xml
main.css
h1{
color:red;
}
welcome.jsp
output
Jquery and main.js is loaded with the spring taglib that's why h2 has body, but I don't understand why h1's color is not red?
05-May-2017 15:45:24.843 WARNING [http-nio-8080-exec-4] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/basic/public-resources/css/main.css] in DispatcherServlet with name 'dispatcher'
I found the solution. I changed
<link rel="stylesheet" href='<c:url value="/public-resources/css/main.css" />'>
to
<link rel="stylesheet" href='<c:url value="/resources/css/main.css" />'>
IntelliJ is giving me error that the directory is not resolved and the whole directory is red. but when deployed, it works.
Setup
servlet-context.xml
<resources mapping="/resources/**" location="/resources/" />
.jsp
<script type="text/javascript" src="/resources/js/test.js"></script>
file location
webapp/resources/js/test.js
Problem
When I access localhost:8080/myApp/resources/js/test.js it is found but what is actually rendered in the page is localhost:8080/resources/js/test.js so /myApp is missing.
Is this really the case for localhost or is there something wrong with my setup?
This is a fresh Spring MVC Project made using STS (template) so I doubt it's the setup because it's already there from the start.
You have to add the context path when declaring the script
<script type="text/javascript" src="${pageContext.request.ContextPath}/resources/js/test.js"></script>
Assuming the script is declared in your jsp page. The context path points to the root of your app.