【发布时间】:2016-03-29 13:24:27
【问题描述】:
我正在使用 sping mvc 4.2.2 和 thymeleaf-spring4 2.1.2 构建一个 Web 应用程序,并且大多数页面共享相同的标题、侧边栏等。因此我决定只有一个 view (. html) 替换子 div 取决于来自 url 的参数 page。
页面名称会被Controller翻译成片段名称:
//SpringTestHello.java
@Controller
public class SpringTestHello {
@RequestMapping("/test")
public String testMap(@RequestParam(value="page", required=false, defaultValue="overview") String page, Model model) {
if (page.equalsIgnoreCase("overview"))
page = "overview :: overview";
if (page.equalsIgnoreCase("user"))
page = "overview :: user";
model.addAttribute("page", page);
return "main_page";
}
}
主页面会用page属性替换一个子div:
<!--main_page.html-->
<div id="pageContent" th:if="${!page.isEmpty()}"
th:replace="${page}">Main Content here</div>
<!--This causes exception-->
但是这会导致 TemplateInputException:
org.thymeleaf.exceptions.TemplateInputException:
Error resolving template "overview :: overview", template might not exist or might not be
accessible by any of the configured Template Resolvers (main_page:123)
虽然使用字符串文字按预期工作:
<!--main_page.html-->
<div id="pageContent" th:replace="overview :: overview"></div>
<!--This shows the correct fragment-->
片段名称作为参数传递时为什么无法解析模板?
【问题讨论】:
标签: java spring web-services spring-mvc thymeleaf