Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot not loading static resources

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

like image 370
SME Avatar asked Aug 04 '16 14:08

SME


People also ask

How do spring boots use static resources?

Using Spring BootSpring Boot comes with a pre-configured implementation of ResourceHttpRequestHandler to facilitate serving static resources. By default, this handler serves static content from any of the /static, /public, /resources, and /META-INF/resources directories that are on the classpath.

Where do I put html files in spring boot?

In the index. html file we have a link that invokes a response from the web application. The file is located in the src/main/resources/static directory, which is a default directory where Spring looks for static content.


1 Answers

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

like image 127
Minjun Yu Avatar answered Nov 03 '22 06:11

Minjun Yu