dav*_*ave 5 java spring web-applications thymeleaf
我有一组基于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
Run Code Online (Sandbox Code Playgroud)
这是示例应用程序的相关项目结构:
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
Run Code Online (Sandbox Code Playgroud)
这是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;
}
Run Code Online (Sandbox Code Playgroud)
这是我的资源处理程序配置(来自WebMvcConfigurerAdapter):
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
{
registry.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}
Run Code Online (Sandbox Code Playgroud)
这是框架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>
Run Code Online (Sandbox Code Playgroud)
这是一个应用程序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>
Run Code Online (Sandbox Code Playgroud)
这是来自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
}
}
Run Code Online (Sandbox Code Playgroud)
该Web应用程序在Tomcat下运行,并且可以使用框架库JAR文件。当我测试控制器URL时,两个都正确加载。也就是说,在两种情况下,都可以找到,加载和显示模板。
但是,如上所述,该框架模板(由/exampleURL 触发)未设置样式,因为未找到其框架CSS文件并将其传递给浏览器。我该如何实现?
(我的框架结构可以吗?是否正确配置了ViewResolver?是否正确配置了资源处理?我href应该在框架模板中使用哪个路径来引用同一JAR中的样式表?)
在 example.html 中尝试替换:
<link rel="stylesheet" type="text/css" th:href="@{/resources/static/css/framework.css}" />
Run Code Online (Sandbox Code Playgroud)
经过:
<link rel="stylesheet" type="text/css" th:href="@{/css/framework.css}"/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
444 次 |
| 最近记录: |