CSS没有加载Spring Boot

sen*_*bhu 25 spring spring-mvc spring-boot

我是Spring框架工作和spring boot的新手.我正在尝试使用CSS,javascript,js添加静态html文件.文件结构是

项目结构

我的html文件头看起来像这样

<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>
Run Code Online (Sandbox Code Playgroud)

当我运行spring项目时,只显示内容并且不应用CSS.然后浏览器在.css,.js文件的控制台404 Not Found错误中显示以下错误

有些人帮我解决了这个问题.谢谢你.

Nic*_*ich 34

你需要把你的CSS放进去/resources/static/css.这个改变为我解决了这个问题.这是我当前的目录结构.

src
  main
    java
      controller
        WebAppMain.java
    resources
      views
        index.html
      static
        css
          index.css
          bootstrap.min.css
Run Code Online (Sandbox Code Playgroud)

这是我的模板解析器:

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;
  }
}
Run Code Online (Sandbox Code Playgroud)

为了以防万一,这是我的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>
Run Code Online (Sandbox Code Playgroud)


Mar*_*szS 10

css文件放入webapp资源文件夹:

src/main/webapp/resources/css/ 
Run Code Online (Sandbox Code Playgroud)

配置资源处理程序

public class WebConfig extends WebMvcConfigurerAdapter {

        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/resources/**")
                        .addResourceLocations("/resources/");
        }
Run Code Online (Sandbox Code Playgroud)

示例项目:

资源:

  • 仅供参考:春季启动文档不鼓励将内容放入webapp中,因为默认情况下它没有添加到可执行jar文件中 (5认同)
  • 另请参阅[Spring Boot](https://github.com/spring-projects/spring-boot/blob/master/docs/howto.md#serve-static-content)文档(在这种特殊情况下可能更相关). (2认同)

小智 7

经过多次尝试后,这对我有用:

  1. CSS位置:/resources/static/css/stylesheet.css
  2. html中的链接路径:th:href="@{/css/stylesheet.css}"
  3. 网络安全配置:
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/css/**");
    }
    
    Run Code Online (Sandbox Code Playgroud)

  • 很好的示例,但 WebSecurityConfig 不是强制性的:它可以在没有 WebSecurityConfig 的情况下完美运行。 (2认同)

Pat*_*ard 5

Spring Boot将尝试在某些默认位置查找您的视图。请看以下链接。

http://docs.spring.io/spring-boot/docs/1.1.4.RELEASE/reference/htmlsingle/#common-application-properties

如果要构建可执行jar,则应将资源放置在src / main / resources下,而不是src / main / webapp下,以便在构建时将它们复制到您的jar中。

您的index.html应该放在src / main / resources / templates下,就像您已经拥有了一样,但是您的静态资源则不应该。默认情况下,Spring Boot将在此处查找您的Thymeleaf视图。而且您实际上不需要为Thymeleaf定义自己的视图解析器,如果您spring-boot-starter-thymeleaf的项目中有依赖项,Spring Boot会为您进行设置。

# THYMELEAF (ThymeleafAutoConfiguration)
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html # ;charset=<encoding> is added
spring.thymeleaf.cache=true # set to false for hot refresh
Run Code Online (Sandbox Code Playgroud)

就像其他人提到的那样,如果将CSS放在src / main / resources / static / css或src / main / resources / public / css中,则可以从HTML中的href =“ css / 5grid ...”引用它们。


Eli*_*iuX 5

我遇到了同样的问题并通过以下方式解决了它:

  1. 确保您正在导出的文件夹可用于网络

    public class WebMvcConfig extends WebMvcConfigurerAdapter {
    
        private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
                "classpath:/META-INF/resources/", "classpath:/resources/",
                "classpath:/static/", "classpath:/public/"
        };
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/**")
                    .addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

    此外,您必须将css样式文件夹放入src/main/resources/ ( static | public | resources | META-INF/resources ) 文件夹

  2. 确保您的安全策略不会阻止它们

    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Override
        public void configure(WebSecurity web) throws Exception {
            //Web resources
            web.ignoring().antMatchers("/css/**");
            web.ignoring().antMatchers("/scripts/**");
            web.ignoring().antMatchers("/images/**");
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

那应该够了