【发布时间】:2017-09-26 03:01:41
【问题描述】:
我有一组基于 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 中的样式表?)
【问题讨论】:
-
问题解决了吗?
-
顺便说一句,这个问题是关于如何从 jar 加载共享模板的一个很棒的例子。我们想从 CDN 加载 /css,所以这不是 OP 的实际问题。谢谢!
标签: java spring web-applications thymeleaf