Spring Boot not loading static resources
Asked Answered
K

8

7

There are loads of questions about spring boot not loading static resources and having read them all (almost) I still can't fix this issue. At this stage I have opted not to run with spring boot but I'd still like to know what the issue was. I am using Eclipse, Java 8 and Maven.

I have an application class that looks like this:

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

I have created a css file - src/main/resources/static/style.css and referenced this from a jsp:

<link href="<c:url value='style.css'/>" rel="stylesheet">

The page loads but not the css. This is the error - 405 Method Not Allowed

I think think is correct but not sure. All help appreciated.

Based on some of the comments below this is how things look now.

My jsp files are configured in src/main/resources/application.properties as follows:

spring.mvc.view.prefix:/WEB-INF/views/
spring.mvc.view.suffix:.jsp

My Jsp is very simple, and is located in /WEB-INF/views/home.jsp

<!DOCTYPE html>
<%@ page pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
    <link href="public/style.css" rel="stylesheet" type="text/css"/>
</head>
<body>
    <p>Hello world!</p>
</body>
</html>

I have also tried linking my css file like this:

<link href="style.css" rel="stylesheet" type="text/css"/>

My css file, located in webapp/public/style.css, is also very simple

p {
    color: red;
}

My jsp loads but not the css. I have run my application using various methods including:

From the command line - java -jar contacts.jar

Inside eclipse - mvn spring-boot:run and mvn tomcat7:run-war Also inside eclipse by right clicking the Application.class file and selecting Run As -> Java Application.

I am using Spring Boot Version 1.4.0.RELEASE

Kwan answered 4/8, 2016 at 14:47 Comment(5)
where are your static files located ? src/main/resources ?Extraterritorial
show us how your jsp files are configured? and you're sure you have no other static file configuration?Entrepreneur
Your style.css is single quoted. maybe you should try double quotes.Extraterritorial
I've added some additional information aboveKwan
Did you figure the solution out? I am surprised that this simple thing was not answered. I am hating spring and java ecosystem right now because it's so bloated and so hard to get even simple things right.Carlotacarlotta
T
2

CSS Location

Put your static resources such as css outside of src/main/resources, which is used for application resources such as properties files.

I always put css files under src/main/webapp/assets/css folder.

Configuration

I am using Java config instead of XML, the following snippet shows you how to configure spring boot to recognize and find the css.

@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware{    

    // view resolver and other confugurations ommitted...

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/assets/**").addResourceLocations("/assets/");
    }
}

Access the css

<link href="<c:url value="/assets/css/style.css" />" rel="stylesheet">

Tulley answered 4/8, 2016 at 19:12 Comment(4)
None of this is necessary when using Spring Boot, particularly @EnableWebMvc which will switch off Boot's auto-configuration of Spring MVCFaustino
@Minjun.Y this assertion is true for Spring MVC projects. Not for springboot.Extraterritorial
@AndyWilkinson Thank you for your clarification. I did not completely understand how spring-boot on this subject works and just tried to provide something that somehow works for me. Now I learned.Tulley
@Georgesvanhoutte Agreed. Thanks.Tulley
C
1

if this dependency is missing, add it to pom.xml file

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>
Cacciatore answered 16/12, 2023 at 9:22 Comment(0)
E
0

Assuming you are not disabling WebMvcAutoConfiguration

Create the public folder on the webapp dir

And on you JSP page

<link href="public/style.css" rel="stylesheet" type="text/css"/>

Assuming you style.css inside public folder i.e webapp > public > styles.css

Here's a docs describing the how to use static content in much more detail.

Elodia answered 4/8, 2016 at 16:14 Comment(1)
can you add you folder structureElodia
E
0

By default Spring Boot will serve static content from a directory called src/main/resources/static (or /public or just /resources or /META-INF/resources) . When your project is created, Spring Boot will automatically provide the static folder under src/main/resources. For that reason i store all my static resources inside the static folder.
In your case i would place one style.css file in each and every one of these folders and start a debugging process by removing them one by one until ... you know.

Source:

enter image description here

Extraterritorial answered 5/8, 2016 at 11:52 Comment(1)
then your classpatch is NOT in your IDE. Must be somewhere in your C drive . Find a way to import your classpath into eclipse.Extraterritorial
D
0

Try looking for a controller with a RequestMapping with a http method thats not GET, that is accepting your style.css request. This can be any request mapping without "value".

This has happened to me when I moved to spring boot from jetty. In the old jetty solution the static content was served from a different servlet.

Darrin answered 18/9, 2016 at 17:26 Comment(0)
M
0

I struggled with connecting my static resources to my jsp pages and this is what I finally used to get it working with Spring boot 2.0. You can see my properties and also what the urls look like when mapping to static resources like images or plain html.

Next we need to define the template prefix and suffix for our JSP files in application.properties. Thus add:

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
server.servlet.context-path=/pdx

http://localhost:8080/pdx/images/thedocks.jpg access static resources in src/main/resources/static/images/thedocks.jpg

http://localhost:8080/pdx/ loads index.html in src/main/resources/static/index.html

http://localhost:8080/pdx/css/home.css loads css class in src/main/resources/static/css/home.css

http://localhost:8080/pdx/h loads my home controller with @Controller("/") and @GetRequest(“/h”) annotations.

my jsp page loads the static image like this

<img alt="the docks" src="/pdx/images/thedocks.jpg"/>
Misdoing answered 8/3, 2018 at 18:42 Comment(0)
M
0

Add in your application.properties file:

spring.mvc.static-path-pattern=/resources/**

And make sure your all static files are inside the src/main/resources/static/{css,js,other files} directory.

To get CSS File use spring uri like this--->

<spring:url var="css" value="/resources/css"/>

then link it like

<link href="${css}/bootstrap.min.css" rel="stylesheet">
Methionine answered 27/1, 2019 at 8:8 Comment(0)
T
0

Since Eclipse is mentioned in original question I am linking with this one: Java class getResource() with eclipse

Sometimes correct configuraton will not work when spring boot jar is run from eclipse. This can be resolved by adding "main\resources" folder to run configuration classpath manually ("run configurations"->"Classpath tab"->"User entries")

Thunder answered 27/6, 2019 at 10:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.