如何从Thymeleaf模板访问库JAR文件中的样式表?

问题描述:

我有一套基于Spring的Java Web应用程序和一个通用框架库,通过JAR文件提供给他们。我使用Thymeleaf的意见。如何从Thymeleaf模板访问库JAR文件中的样式表?

我有一个共同的Thymeleaf模板,由几个Web应用程序使用。模板HTML文件及其关联的样式表都在库JAR文件中。当Web应用程序基于框架模板显示视图时,该模板由Thymeleaf找到并处理。

我的问题是,我不知道如何导入样式表,使它被加载并传回给浏览器。最终的结果是无风格(尽管是正确的)HTML。

我使用Gradle作为我的构建工具。

下面是框架我的相关项目结构:

common-framework/src/main 
    ../java      - contains the framework code 
    ../resources     - contains the framework resources 
     ../static 
      ../css 
       example.css   - Style sheet for use in templates 
     ../templates 
      ../fwk 
       example.html  - Thymeleaf template 

下面是一个示例应用我的相关项目结构:

app/src/main 
    ../java      - contains the application code 
    ../resources     - contains the application resources 
     ../templates 
      ../app 
       start.html   - Thymeleaf template 
    ../webapp 
     ../resources 
      ../css 
       app.css    - Style sheet for use in the app 

这里所使用的应用程序和双方的ViewResolver豆框架定位模板:

@Bean 
public ViewResolver viewResolver() 
{ 
    ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver(); 
    templateResolver.setTemplateMode("HTML5"); 
    templateResolver.setPrefix("templates/"); 
    templateResolver.setSuffix(".html"); 
    templateResolver.setCharacterEncoding("UTF-8"); 
    templateResolver.setOrder(1); 

    SpringTemplateEngine engine = new SpringTemplateEngine(); 
    engine.setTemplateResolver(templateResolver); 

    ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); 
    viewResolver.setTemplateEngine(engine); 
    viewResolver.setCharacterEncoding("UTF-8"); 
    viewResolver.setOrder(1); 

    return viewResolver; 
} 

这是我的源处理程序配置(从我WebMvcConfigurerAdapter):

@Override 
public void addResourceHandlers(ResourceHandlerRegistry registry) 
{ 
    registry.addResourceHandler("/resources/**") 
      .addResourceLocations("/resources/"); 
} 

这里的框架Thymeleaf模板(FWK/example.html的)

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> 

<head> 
    <meta charset="utf-8" /> 
    <title th:text="${title}"></title> 
    <link rel="stylesheet" type="text/css" th:href="@{/resources/static/css/framework.css}" /> <!-- *** PROBLEM IS HERE *** --> 
</head> 
<body> 
    ... 
</body> 
</html> 

下面是一个应用程序Thymeleaf模板(应用/的start.html)

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> 

<head> 
    <meta charset="utf-8" /> 
    <title th:text="${title}"></title> 
    <link rel="stylesheet" type="text/css" th:href="@{/resources/css/app.css}" /> <!-- *** This works correctly. *** --> 
</head> 
<body> 
    ... 
</body> 
</html> 

下面是一个来自Web应用程序的示例控制器。注意:这是一个依赖于框架库的单独项目。

@Controller 
public class ExampleController 
{ 
    @GetMapping(path = "/start") 
    public String start(Model model, HttpServletRequest request) 
    { 
     ... 
     return "app/start"; // Uses an application template 
    } 

    @GetMapping(path = "/example") 
    public String example(Model model) 
    { 
     ... 
     return "fwk/example"; // Uses a framework template 
    } 
} 

Web应用程序在Tomcat下运行,并且框架库JAR文件可用。当我测试控制器URL时,两者都正确加载。也就是说,在这两种情况下,都会找到,加载并显示模板。

但是,正如我上面提到的,框架模板(由/example URL触发)是未定义的,因为它的框架CSS文件未找到并传递给浏览器。我如何实现这一目标?

(是我的框架结构好吗?难道我的ViewResolver的配置是否正确?是我的资源处理正确配置?我应在框架模板使用什么href路径指到一个样式表在同一JAR?)

在example.html的尝试更换:

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

通过:

<link rel="stylesheet" type="text/css" th:href="@{/css/framework.css}"/> 
+0

感谢您的回答。不幸的是,这没有奏效。它解析为“/app/css/framework.css”并导致404错误。 – dave