Thymeleaf模板(Spring MVC)无法定位静态资源

时间:2015-08-19 17:30:11

标签: spring spring-mvc gradle spring-boot

Thymeleaf模板问题查找放置在Web应用程序根目录(包括下面的CSS文件)中的静态文件。我通过addResourceHandlers()方法添加了相关的映射(/ resource)(参见下面的config类)。

也许它与最近切换到gradle(以前的Maven)有关。我可能忽略了build.gradle文件中的某些内容?

 <link rel="stylesheet" th:href="@{'/resources/stylesheets/test.css'}" type="text/css" media="screen" />

浏览器控制台输出(在页面加载时)

Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost:8080/resources/stylesheets/test.css".

Thymeleaf

<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:tiles="http://www.thymeleaf.org">
<head>
    <!--Stylesheets -->
    <link rel="stylesheet" th:href="@{'/resources/stylesheets/test.css'}" type="text/css" media="screen" />

    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1"/>

</head>
...
</html>

.war目录的结构

- root/
 --stylesheets/
   --- test.css 
 --images/
 --META-INF/
 --WEB-INF/ 
 --...

配置类

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/resources/**").addResourceLocations("/")
                            .setCachePeriod(31556926);
        }
...
}

Gradle构建文件

buildscript {
    repositories {
        mavenLocal()
        jcenter()
    }
    dependencies {
        classpath 'com.bmuschko:gradle-tomcat-plugin:2.2.2'
    }
}

plugins {
    id "com.bmuschko.tomcat" version "2.2.2"
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'war'
apply plugin: 'com.bmuschko.tomcat-base'
version = '1.0'
group = 'com.project'

sourceCompatibility = 1.8
targetCompatibility = 1.8

war {
    baseName = '/'
    archiveName = "${project.name}.war"
}

war.doLast {
    ant.unzip(src: war.archivePath, dest: "$buildDir/$project.name")
}
sourceSets {
    main {
           java{
               srcDir 'src/main/java'
           }
            resources {
                srcDir 'src/main/resources'
            }
        }
    test {
          java {
             srcDir 'src/test/java'
         }
         resources {
            srcDir 'src/test/resources'
        }
    }
}

dependencies {
    modules {
            module("javassist:javassist") {
                replacedBy("org.javassist:javassist")
            }
        }

    tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
            "org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}"
    tomcat("org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}") {
        exclude group: "org.eclipse.jdt.core.compiler", module: "ecj"
    }
        ....               
}

repositories {
    jcenter()

        mavenCentral()
         mavenLocal()
 }

task wrapper(type: Wrapper) {
    gradleVersion = '2.6'
}


jar {
    manifest {
        attributes 'Implementation-Title': 'App',
                   'Implementation-Version': version
    }
}


test {
    systemProperties 'property': 'value'  
  testLogging {
        // Show that tests are run in the command-line output
        events 'started', 'passed'
        exceptionFormat "full"
        showStandardStreams = true
        showCauses = true
        showExceptions = true

        }
}

5 个答案:

答案 0 :(得分:2)

事实证明,Controller中的类级别RequestMapping是原因。不得不将其更改为方法级别(见下文)。

错误

@Controller("/register")
public class RegistrationController {    
   @RequestMapping(method = RequestMethod.GET)
    public String showRegistrationForm(WebRequest request, Model model) {
        return "...";
    }

正确

@Controller
public class RegistrationController {
   @RequestMapping(value="/register",method = RequestMethod.GET)
    public String showRegistrationForm(WebRequest request, Model model) {
        return "...";
    }

答案 1 :(得分:0)

  1. 您必须将静态资源放在“src / main / webapp / resources / static”文件夹中(不在“Java Resources / src / main / resources”中)。 (如果您使用Eclipse,您可能会多次看到“src”文件夹。)

  2. 在MvcConfig.java中,您应该将静态资源'链接和接受的模式添加为belove。

     public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/","/resources/");
    }
    
  3. 在您的html页面中使用带有thymleaf演示文稿的静态资源链接。

  4. <link th:href="@{./resources/static/css/bootstrap.min.css}"  type="text/css" rel="stylesheet" media="screen"></link>

答案 2 :(得分:-1)

为什么不尝试添加上下文路径(〜),如下所示。

符号&#34;〜&#34;将被视为相对于服务器根目录。

<link rel="stylesheet" th:href="@{'~/resources/stylesheets/test.css'}" type="text/css" media="screen" />

我希望这会有所帮助。

答案 3 :(得分:-1)

您可以将资源定位到类路径,如下所示:

registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/") .setCachePeriod(31556926);

我建议您添加目录名称:

registry.addResourceHandler("/resources/stylesheets/**").addResourceLocations("classpath:/stylesheets/") .setCachePeriod(31556926);

您无需再修改其他文件。

答案 4 :(得分:-1)

<link rel="stylesheet" th:href="@{/resources/stylesheets/test.css}" type="text/css" media="screen" />

尝试不使用''因为您正在引用资源而不是字符串。另外,如果不是所有代码都是Thymeleaf,那么为了做得更好,你应该做的事情如下:

<link rel="stylesheet" href="../resources/stylesheets/test.css" th:href="@{/resources/stylesheets/test.css}" type="text/css" media="screen" />