2014-10-19 154 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