【问题标题】:thymeleaf and maven web project don't recognize css filesthymeleaf 和 maven web 项目无法识别 css 文件
【发布时间】:2021-01-16 18:11:24
【问题描述】:

我正在尝试将 css 文件链接到 html 文件,就像他们在下一个教程 https://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html 中所做的那样,但是当我在 tomcat 中运行应用程序时,HTML 可以工作,但 CSS 丢失,我在浏览器上收到下一个错误控制台“加载资源失败:服务器响应状态为 404()”,与此相反,如果我在没有 tomcat 的情况下打开 html,css 文件工作正常,所以我正确指向文件。 如果有帮助,我已经使用 maven-archetype-webapp 创建了项目

这是我的项目结构

我要链接css文件的HTML

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Web de productos - Inicias sesion</title>
    <link rel="stylesheet" type="text/css" media="all"
         href="../../css/normalize.css" th:href="@{/css/normalize.css}" />  
    <link rel="stylesheet" type="text/css" media="all"
        href="../../css/index.css" th:href="@{/css/index.css}" />   
</head>
<body> ... </body>
</html>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>ss.webdeproductos</groupId>
  <artifactId>WebDeProductos2</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>WebDeProductos2 Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
        
    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>       
    </dependency>
    
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.3.6.Final</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.21</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf</artifactId>
        <version>3.0.11.RELEASE</version>
    </dependency>
    
  </dependencies>

  <build>
    <finalName>WebDeProductos2</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

你知道如何解决这个问题吗?

【问题讨论】:

  • 当你调用正在运行的应用程序时,你看到html了吗?我不清楚,如果没有任何效果或只有 CSS 丢失。
  • @ElmarBrauch 只是缺少 CSS,我要编辑我的问题
  • 我在下面的答案中又添加了一个方面。

标签: java maven thymeleaf java-ee-7 tomcat9


【解决方案1】:

在您的百里香模板中有href 的双重定义,请参阅href="../../css/normalize.css" th:href="@{/css/normalize.css}"。您应该删除其中之一 - 在浏览器中检查您的 html 源,路径可能与您的文件夹结构不匹配。

Thymeleaf 有一个不同的基本位置来检查静态文件,如 css(或 js)。
由于您以这种方式引用 css 文件 th:href="@{/css/normalize.css}" 请尝试以下文件夹结构:

  • WEB-INF/静态
  • WEB-INF/静态/css
  • WEB-INF/静态/js
  • WEB-INF/模板

在我的德语博客中,我写了一篇关于 Spring Boot 和 Thymeleaf 的文章:
https://agile-coding.blogspot.com/2020/10/spring-mvc-thymeleaf.html

我的包含文件夹结构的示例代码可以在这里找到:
https://github.com/elmar-brauch/thymeleaf

【讨论】:

  • 谢谢你的回答,我试过这个文件夹结构,但它不起作用。我认为这种文件夹结构在您使用 spring 时有效,但我没有使用 Spring。无论如何,如果我不能解决这个问题,我将把我的项目转移到我知道这很好的地方。
  • 我已经尝试过,但没有奏效。我认为问题出在其他地方,因为如果您看到我在问题中留下的教程,他们使用像我这样的文件夹结构(这里的项目 github.com/thymeleaf/thymeleafexamples-gtvg 来自百里香官方页面)。
猜你喜欢
  • 2012-08-25
  • 2011-01-12
  • 1970-01-01
  • 1970-01-01
  • 2016-10-23
  • 1970-01-01
  • 1970-01-01
  • 2015-10-21
  • 2021-11-20
相关资源
最近更新 更多