繁体   English   中英

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

[英]How do I access style sheets in a library JAR file from a Thymeleaf template?

我有一组基于Spring的Java Web应用程序和一个通过JAR文件可用的公共框架库。 我正在使用Thymeleaf来表达观点。

我有一个常见的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:

@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模板(app / 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}"/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM