2014-10-19 130 views
0

我對Spring很陌生,無法訪問我的資源。我使用Spring + Thymeleaf和我directionary結構是:Spring:找不到資源

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

+0

是視圖解析器在WebConfig – 2014-10-20 08:47:42

回答

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> 
+0

現在設置的問題是該版本提供了一個錯誤,並說:「無法找到模板位置:類路徑資源[模板/](請添加一些模板或檢查您的Thymeleaf配置)」我應該在哪裏添加這個位置? – Chryb 2014-10-19 12:28:32

+0

查看更新的答案。 – 2014-10-19 12:38:08

+0

對不起,但這不起作用。 – Chryb 2014-10-19 12:44:10

-2

請請參閱https://github.com/webcompere/SpringHelloWorld中的示例您需要在spring上下文中聲明資源文件夾。

在我的例子,我的春節,調度員在其自己的春天豆文件中聲明 - 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/" /> 

是調度員怎麼高興爲這些資源。我需要這樣的原因是因爲我已經告訴通過彈簧路由到一切響應應用程序,因爲這在web.xml

<servlet-mapping> 
    <servlet-name>mvc-dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
</servlet-mapping> 
+0

你是什麼意思? 'web.xml'文件? – Chryb 2014-10-19 12:49:42

+0

請參閱此文章的編輯 - 提供更多詳細信息。 – 2014-10-20 09:16:51