春天:找不到资源

时间:2014-10-19 12:16:15

标签: java css spring spring-mvc resources

我对Spring很新,我无法访问我的资源。我使用Spring + Thymeleaf,我的定向结构是:

src
  - main
    - java
      - Application.java
      - controller
        - LanchController.java
      - ...
    - resources
      - css
        - test.css
      - templates
        - account.html
      - ...

应用程序正确加载但html模板找不到css等。 account.html:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" type="text/css" href="/css/test.css"/>
</head>
<body>
<p>Hello, User!</p>
</body>
</html>

我已经尝试了许多路径,例如../css/.../resources/css/..,但没有任何作用。我希望有一个人可以帮助我。我遵循了本教程:https://spring.io/guides/gs/serving-web-content/

问候 Chryb

2 个答案:

答案 0 :(得分:1)

请参阅此处的示例: http://blog.codeleak.pl/2014/04/how-to-spring-boot-and-thymeleaf-with-maven.html

看起来您需要在链接中添加Thymeleaf属性:

<!DOCTYPE html>
<html>
<head>
    <title>Hello Spring Boot!</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <link href="../static/css/core.css"
          th:href="@{/css/core.css}"
          rel="stylesheet" media="screen" />
</head>
<body>
<p>Hello Spring Boot!</p>
</body>
</html>

答案 1 :(得分:-2)

请参阅https://github.com/webcompere/SpringHelloWorld中的示例您需要在spring上下文中声明资源文件夹。

在我的示例中,我的Spring调度程序在其自己的spring beans文件中声明 - https://github.com/webcompere/SpringHelloWorld/blob/master/src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml

行:

<mvc:resources mapping ="/css/**" location ="/WEB-INF/css/" />
<mvc:resources mapping ="/js/**" location ="/WEB-INF/js/" />
<mvc:resources mapping ="/images/**" location ="/WEB-INF/images/" />
<mvc:resources mapping ="/*. html" location ="/WEB-INF/" />

是调度员如何乐意为这些资源提供服务。我需要这个的原因是因为我告诉应用程序通过spring路由响应所有内容,因为这在web.xml中

 <servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
       <url-pattern>/</url-pattern>
 </servlet-mapping>
相关问题