7
votes

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

7
where are your static files located ? src/main/resources ?AchillesVan
show us how your jsp files are configured? and you're sure you have no other static file configuration?kukkuz
Your style.css is single quoted. maybe you should try double quotes.AchillesVan
I've added some additional information aboveSME
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.MikeJoe

7 Answers

1
votes

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">

0
votes

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.

0
votes

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

0
votes

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.

0
votes

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"/>
0
votes

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">
0
votes

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")