Thymeleaf + spring 动态替换 [英] Thymeleaf + spring dynamic replace

查看:35
本文介绍了Thymeleaf + spring 动态替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 Thymeleaf 中创建动态替换?

我有以下控制器:

@Controller公共类登录控制器{@RequestMapping("/登录")公共字符串getLogin(模型模型){model.addAttribute("模板","登录");返回索引";}}

以及以下视图:

<html xmlns:th="http://www.thymeleaf.org" ><头></头><身体><div th:replace="fragments/${template} :: ${template}"></div></html>

我收到以下错误:

解析模板fragments/${template}"时出错,模板可能不存在或可能无法被任何配置的模板解析器访问

更新

我尝试像这样预处理我的变量:

<div th:replace="fragments/${__#{${template}}__} :: ${__#{${template}}__}"></div>

现在 ${template} 怎么被替换为 login 我现在有以下错误:

评估 SpringEL 表达式的异常:??login_en_US??"

解决方案

我相信在 thymeleaf 中管理这种行为的适当方法是使用 layout:fragment 标签.如果我错了,请纠正我.这是我的布局页面的一个简单示例,以及动态"加载的登录页面:

layout.html

<头><title layout:title-pattern="$DECORATOR_TITLE - $CONTENT_TITLE">布局</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/><meta http-equiv="X-UA-Compatible" content="IE=Edge"/><身体><div><div class="app-container"><div th:fragment="内容">

<div th:fragment="script"></div></html>

然后,当 login 被加载时,它将 th:fragment div 替换为 html 视图中与控制器方法返回的字符串匹配的关联 div,在本例中为 login.html:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"xmlns:layout="http://www.w3.org/1999/xhtml"布局:装饰者=布局"><头><title>登录</title><身体><div th:fragment="内容"><form th:action="@{/login}" method="post"><div><标签>用户名:<input type="text" name="username"/></label></div><div><标签>密码:<input type="password" name="password"/></label></div><div><input type="submit" value="Sign In"/></div></表单>

</html>

现在,如果您想有条件地加载另一个片段,我采用的方法是添加带有 th:if 情况的替换标签.下面是一个基于当前用户属性显示不同问题的表单示例:

<div th:replace="fragments/custom-questions :: type-1-checkboxes"></div>

<div th:if="${foo.type} == 'type_2'"><div th:replace="fragments/custom-questions :: type-2-checkboxes"></div>

然后从文件 custom-questions.html 加载关联的 div:

//东西

<div th:fragment="type-2-checkboxes">//东西

Is it possible to create a dynamic replace in Thymeleaf?

I have the following controller:

@Controller
public class LoginController {

    @RequestMapping("/login")
    public String getLogin(Model model){
        model.addAttribute("template","login");
        return "index";
    }
}

And the following view:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" >
<head></head>
<body>

<div th:replace="fragments/${template} :: ${template}"></div>

</body>
</html>

And i'm getting the following error:

Error resolving template "fragments/${template}", template might not exist or might not be accessible by any of the configured Template Resolvers

UPDATE

I tried to preprocess my variables like this:

<div th:replace="fragments/${__#{${template}}__} :: ${__#{${template}}__}"></div>

How ever now ${template} is getting replaced with login i have the following error now:

Exception evaluating SpringEL expression: "??login_en_US??"

解决方案

I believe the appropriate method to manage this behavior in thymeleaf is to use layout:fragment tags. Please correct me if I'm wrong. Here is a simple example of my layout page, and the login page which is 'dynamically' loaded:

layout.html

<html xmlns:layout="http://www.w3.org/1999/xhtml" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <title layout:title-pattern="$DECORATOR_TITLE - $CONTENT_TITLE">Layout</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge"/>
</head>
<body>
<div>
    <div class="app-container">
        <div th:fragment="content">
        </div>
    </div>
</div>
<div th:fragment="script"></div>
</body>
</html>

Then, when login gets loaded, it replaces the th:fragment div with the associated div in the html view which matches the string returned by the controller method, in this case login.html:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.w3.org/1999/xhtml"
      layout:decorator="layout">
<head>
    <title>Login</title>
</head>
<body>
<div th:fragment="content">
    <form th:action="@{/login}" method="post">
        <div><label> User Name : <input type="text" name="username"/> </label></div>
        <div><label> Password: <input type="password" name="password"/> </label></div>
        <div><input type="submit" value="Sign In"/></div>
    </form>
</div>
</body>
</html>

Now, if you want to load another fragment conditionally, the approach I take is to add replace tags with th:if cases. Here's an example of a Form that displays different questions based on an attribute of the current user:

<div th:if="${foo.type)} == 'type_1'">
    <div th:replace="fragments/custom-questions :: type-1-checkboxes"></div>
</div>
<div th:if="${foo.type} == 'type_2'">
    <div th:replace="fragments/custom-questions :: type-2-checkboxes"></div>
</div>

Then the associated div gets loaded from the file custom-questions.html:

<div th:fragment="type-1-checkboxes">
  //stuff
</div>

<div th:fragment="type-2-checkboxes">
  //stuff
</div>

这篇关于Thymeleaf + spring 动态替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆