春季启动CSS显示空白/尝试一切后没有加载 [英] Spring Boot CSS Showing up blank / not loading after trying everything

查看:248
本文介绍了春季启动CSS显示空白/尝试一切后没有加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Spring Boot非常陌生,现在我正在阅读这本书。春季引导在行动。



我创建并编写了我的第一个Web简单Web应用程序,期望在控制台中css文件显示为空白。我已经看过以下帖子:





总结一下,你的浏览器正在缓存你的静态资源,比如CSS文件。 p>

为了打破此行为,请先尝试清理浏览器缓存,然后在Google Chrome浏览器中转到设置,然后清除浏览数据。



其次,将这些行添加到 application.properties 文件中,以便缓存缓存:

  spring.resources.chain.strategy.content.enabled = true 
spring.resources.chain.strategy.content.paths = / **

或者添加它:

  spring.resources.chain.strategy.fixed.enabled = true 
spring.resources.chain.strategy.fixed.paths = / **
spring。 resources.chain.strategy.fixed.version = v12


I am really new to Spring Boot, and I am currently going through the book. Spring Boot in action.

I created and complied my first web simple web app fine, expect the css file shows up blank in the console. I have already looked up the following post:

Spring Boot - CSS not loading
Spring Boot CSS Stripped

I am using Thymleaves and my css file is placed within the static folder as the post and book states, but nothing shows up. my current external link, seems to be the correct way too.

 <link rel="stylesheet" th:href="@{/main.css}" />

Although, the css appear to show in the resources in console, the css file remains blank. Below are my files and code.

File Structure:

Template:

body {
    background-color: #cccccc;
    font-family: arial,helvetica,sans-serif;
}
.bookHeadline {
    font-size: 12pt;
    font-weight: bold;

}
.bookDescription {
    font-size: 10pt;
}
label {
    font-weight: bold;
}

         <html xmlns:th="http://www.springframework.org/schema/data/jaxb">
    <head>
    <title>Reading List</title>
    <link rel="stylesheet" th:href="@{/main.css}" />
    </head>
    <body>
    <h2>Your Reading List</h2>
    <div th:unless="${#lists.isEmpty(books)}">
    <dl th:each="book : ${books}">
        <dt class="bookHeadline">
            <span th:text="${book.title}">Title</span> by
            <span th:text="${book.author}">Author</span>
            (ISBN: <span th:text="${book.isbn}">ISBN</span>)
        </dt>
        <dd class="bookDescription">
    <span th:if="${book.description}"
      th:text="${book.description}">Description</span>
            <span th:if="${book.description eq null}">
    No description available</span>
        </dd>
    </dl>
    </div>
    <div th:if="${#lists.isEmpty(books)}">
    <p>You have no books in your book list</p>
    </div>
    <hr/>
   <h3>Add a book</h3>
    <form method="POST">
    <label for="title">Title:</label>
    <input type="text" name="title" size="50"></input><br/>
    <label for="author">Author:</label>
    <input type="text" name="author" size="50"></input><br/>
    <label for="isbn">ISBN:</label>
    <input type="text" name="isbn" size="15"></input><br/>
    <label for="description">Description:</label><br/>
    <textarea name="description" cols="80" rows="5">
    </textarea><br/>
    <input type="submit"></input>
    </form>
    </body>
    </html>

Controller:

package codenotes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;

@Controller
@RequestMapping("/")
public class BookController {


private BookRepository bookRepository;

@Autowired
public BookController(BookRepository bookRepository) {
    this.bookRepository = bookRepository;
}

@RequestMapping(value = "/{reader}", method = RequestMethod.GET)
public String readerBooks(
        @PathVariable("reader") String reader,
        Model model) {

    List<Book> readingList =
            bookRepository.findByReader(reader);
    if (readingList != null) {

        model.addAttribute("books", readingList);
    }
    return "readingList";
}

@RequestMapping(value = "/{reader}", method = RequestMethod.POST)
public String addToReadingList(
        @PathVariable("reader") String reader, Book book) {
    book.setReader(reader);
    bookRepository.save(book);
    return "redirect:/{reader}";
}
}

Repository:

package codenotes;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
public interface BookRepository extends JpaRepository<Book, Long> {
List<Book> findByReader(String reader);
}

解决方案

I believe you should read this, how to serve static content:

http://docs.spring.io/spring-boot/docs/1.4.2.RELEASE/reference/htmlsingle/#boot-features-spring-mvc-static-content

To sum it up, your browser is caching your static resources, such as CSS files.

In order to break this behavior, try first clean your browser cache, in google chrome you go to settings then clear browsing data.

Secondly, add these lines to your application.properties file in order to bust the cache:

spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**

or add this instead:

spring.resources.chain.strategy.fixed.enabled=true
spring.resources.chain.strategy.fixed.paths=/**
spring.resources.chain.strategy.fixed.version=v12

这篇关于春季启动CSS显示空白/尝试一切后没有加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆