Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS not loading in Spring Boot

I am new to spring frame work and spring boot.I am trying to add the static html file with CSS,javascript,js. the file structure is

PROJECT STRUCTURE

and my html file head looks like this

<html xmlns:th="http://www.thymeleaf.org"> <head>     <title>HeavyIndustry by HTML5Templates.com</title>     <meta http-equiv="content-type" content="text/html; charset=utf-8" />     <meta name="description" content="" />     <meta name="keywords" content="" />      <link rel="stylesheet" type="text/css" media="all" href="css/5grid/core.css" th:href="@{css/5grid/core}" />     <link rel="stylesheet" type="text/css" href="css/5grid/core-desktop.css" />     <link rel="stylesheet" type="text/css" href="css/5grid/core-1200px.css" />     <link rel="stylesheet" type="text/css" href="css/5grid/core-noscript.css" />     <link rel="stylesheet" type="text/css" href="css/style.css" />     <link rel="stylesheet" type="text/css" href="css/style-desktop.css" />      <script src="css/5grid/jquery.js" type="text/javascript"></script>     <script src="css/5grid/init.js?use=mobile,desktop,1000px&amp;mobileUI=1&amp;mobileUI.theme=none" type="text/javascript"></script>     <!--[if IE 9]><link rel="stylesheet" href="css/style-ie9.css" /><![endif]--> </head> 

when i run the spring project only the content is shown and the CSS is not applied.then the browser show the following error in the console 404 Not Found error for the .css,.js files

some body help me to sort out this issue.Thanks in Advance.

like image 255
senthil prabhu Avatar asked Jan 18 '14 11:01

senthil prabhu


People also ask

Why is my CSS file not loading?

A site may not load the CSS file due to browser caching. It's the most common cause and is the easiest to fix because you only need to remove the cache from your browser. Yet, there are times when an invalid line of code or conflict with other themes and plugins can also make the CSS file unreadable.


2 Answers

You need to put your css in /resources/static/css. This change fixed the problem for me. Here is my current directory structure.

src   main     java       controller         WebAppMain.java     resources       views         index.html       static         css           index.css           bootstrap.min.css 

Here is my template resolver:

public class WebAppMain {    public static void main(String[] args) {     SpringApplication app = new SpringApplication(WebAppMain.class);     System.out.print("Starting app with System Args: [" );     for (String s : args) {       System.out.print(s + " ");     }     System.out.println("]");     app.run(args);   }     @Bean   public ViewResolver viewResolver() {     ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();     templateResolver.setTemplateMode("XHTML");     templateResolver.setPrefix("views/");     templateResolver.setSuffix(".html");      SpringTemplateEngine engine = new SpringTemplateEngine();     engine.setTemplateResolver(templateResolver);      ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();     viewResolver.setTemplateEngine(engine);     return viewResolver;   } } 

And just in case, here is my index.html:

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring3-3.dtd"> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"       xmlns:th="http://www.thymeleaf.org"> <head>     <title>Subscribe</title>     <meta charset="utf-8" />     <meta http-equiv="X-UA-Compatible" content="IE=edge" />     <meta name="viewport" content="width=device-width, initial-scale=1" />          <!-- Bootstrap -->     <link type="text/css" href="css/bootstrap.min.css" rel="stylesheet" />     <link type="text/css" href="css/index.css" rel="stylesheet" /> </head> <body> <h1> Hello</h1> <p> Hello World!</p>      <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>     <!-- Include all compiled plugins (below), or include individual files as needed -->     <script src="js/bootstrap.min.js"></script> </body> </html> 
like image 112
Nick Humrich Avatar answered Sep 19 '22 13:09

Nick Humrich


Put css files into webapp resources folder:

src/main/webapp/resources/css/  

Configure resource handler

public class WebConfig extends WebMvcConfigurerAdapter {          @Override         public void addResourceHandlers(ResourceHandlerRegistry registry) {                 registry.addResourceHandler("/resources/**")                         .addResourceLocations("/resources/");         } 

Example projects:

  • https://github.com/spring-guides/tut-web/tree/master/6/complete
  • Spring Boot Service Template with Static Content

Source:

  • Designing and Implementing a Web Application with Spring
  • Serving Web Content with Spring MVC
like image 30
MariuszS Avatar answered Sep 20 '22 13:09

MariuszS