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



Thymelaf模板定位web应用程序根目录中的静态文件(包括下面的CSS文件(时出现问题。我已经通过addResourceHandlers((方法添加了相关的映射(/resources((请参阅下面的配置类(。

也许这与最近转投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".

胸腺

<!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);
        }
...
}

渐变构建文件

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
        }
}

事实证明,控制器内部的类级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. 您必须将静态资源定位到"src/main/webapp/resources/static"文件夹中(而不是"Java resources/src/main/resources"中(。(如果您在Eclipse上工作,您可能会多次看到"src"文件夹(。

  2. 在MvcConfig.java中,您应该添加静态资源的链接和接受的模式,如下所示。

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

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

为什么不尝试像下面这样附加上下文路径(~(呢。

符号"~"将被视为相对于服务器根。

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

我希望这能有所帮助。

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

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

我建议您添加目录名:

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

您不再需要修改其他文件。

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

尝试不使用",因为您引用的是资源而不是字符串。为了做得更好,万一不是所有的代码都是Thymelaf,你应该做一些类似的事情:

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

最新更新