Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CSS未在Spring引导中加载_Spring_Spring Mvc_Spring Boot - Fatal编程技术网

CSS未在Spring引导中加载

CSS未在Spring引导中加载,spring,spring-mvc,spring-boot,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"

我是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>

HTML5模板网站发布的HeavyIndustry
当我运行spring项目时,只显示内容,不应用CSS 404未找到.css、.js文件的错误

有人帮我解决这个问题。提前谢谢。

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

src/main/webapp/resources/css/ 
配置资源处理程序

public class WebConfig extends WebMvcConfigurerAdapter {

        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/resources/**")
                        .addResourceLocations("/resources/");
        }
示例项目:

资料来源:


    • 您需要将css放入
      /resources/static/css
      中。这个改变为我解决了问题。这是我当前的目录结构

      src
        main
          java
            controller
              WebAppMain.java
          resources
            views
              index.html
            static
              css
                index.css
                bootstrap.min.css
      
      这是我的模板解析器:

      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;
        }
      }
      
      为了以防万一,这里是我的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>
      
      
      订阅
      你好
      你好,世界


      Spring Boot将尝试在一些默认位置查找视图。看看下面的链接

      如果您正在构建一个可执行jar,那么您的资源应该放在src/main/resources下,而不是src/main/webapp下,以便在构建时将它们复制到您的jar中

      您的index.html应该放在src/main/resources/templates下,就像您看到的那样,但静态资源不应该放在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
      
      #THYMELEAF(THYMELEAF自动配置)
      spring.thymeleaf.prefix=类路径:/templates/
      spring.thymeleaf.suffix=.html
      spring.thymeleaf.mode=HTML5
      spring.thymeleaf.encoding=UTF-8
      spring.thymeleaf.content type=text/html#;字符集=已添加
      spring.thymeleaf.cache=true#设置为false进行热刷新
      

      正如其他人所提到的,如果您将css放在src/main/resources/static/css或src/main/resources/public/css中,那么在HTML中从href=“css/5grid…”引用它们应该会起作用。

      我也是spring boot新手,我也有同样的问题。 我已经将正确的路径手动输入到浏览器中,并且看到了tomcat提供的404。 然后我在以下位置找到了解决方案:

      现在css文件可以通过代码访问


      您必须将css文件夹移动到src/main/resources/static/css,然后内容才是可读的(在我的本地配置中)。

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

    • 确保您要导出的文件夹可用于web

      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);
          }
      }
      
      此外,您必须将cssstyle文件夹放入src/main/resources/(static | public | resources | META-INF/resources)文件夹

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

      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/**");
          }
      }
      
    • 这应该足够了

      然而,在Spring Boot的例子中,值得一提的是Spring是如何 Boot处理静态内容。当springboot的web 自动配置是为Spring MVC自动配置bean, 这些bean包括一个将/**映射到多个 资源位置。这些资源位置包括(相对于 类路径的根目录)可以执行以下操作:

    • /META-INF/资源/
    • /资源/
    • /静态/
    • /公开的/
    • 在传统的Maven/Gradle构建的应用程序中,您通常会 src/main/webapp上的静态内容,以便将其放置在 生成生成的WAR文件的根。在构建WAR文件时 对于Spring Boot,这仍然是一个选项。但你也可以选择 将静态内容放置在映射到 资源处理程序

      
      [这是我的项目结构的图像。我添加了webapp目录以支持.jsp文件。这个方法request.getContextPath()对我很有效。希望我能在这方面帮助别人…只要路径存在,它就会得到路径。
      注意:你的网络配置中应该有一个解析器bean
      `在这里输入代码“@Bean”
      公共内部资源viewResolver viewResolver(){
      InternalResourceViewResolver resolver=new`enter code here`InternalResourceViewResolver();
      resolver.setPrefix(“/WEB-INF/jsp/”);
      resolver.setSuffix(“.jsp”);
      返回解析器;
      }` 
      
      对于添加的目录][1]
      这是我经过多次尝试后的结果:

    • css位置:
      /resources/static/css/stylesheet.css
    • html中的链接路径:
      th:href=“@{/css/stylesheet.css}”
    • Web安全配置:
      @Override
      public void configure(WebSecurity web) throws Exception {
          web.ignoring().antMatchers("/css/**");
      }
      

    • 另请参阅文档(在这种特殊情况下可能更相关)。@MariuszS我已更改了上述文件夹结构。现在index.html本身未加载。浏览器显示以下错误“HTTP状态500-请求处理失败;嵌套异常为org.thymeleaf.exceptions.TemplateInputException:解析模板“索引”时出错,模板可能不存在,或者任何已配置的模板解析程序都无法访问模板“仅供参考:spring boot文档不鼓励将内容放入webapp,因为默认情况下它不会添加到可执行JAR中。我也有同样的问题,我认为tomcat没有设置读取css文件夹的权限。但我对spring boot也是新手,我没有解决方案。参考它,添加关于为什么必须将css文件放在静态文件夹中的信息可能会很有趣。它对我也有用@FernandoSanchiz我想这就是框架的工作方式。我不知道
      为什么
      是什么。很好的示例,但是WebSecurityConfig不是必需的:它可以完美地运行。