查看Spring Boot和Thymeleaf的帮助程序

查看Spring Boot和Thymeleaf的帮助程序,spring,spring-mvc,spring-boot,thymeleaf,Spring,Spring Mvc,Spring Boot,Thymeleaf,我是SpringBoot的新手,现在我们想用SpringBoot2和Thymeleaf3作为模板引擎用Java重写我们的旧PHP应用程序 我们的传统应用程序有数百个表单和数千个输入字段。为此,我们使用了一个简单的模板辅助程序,它使渲染输入字段、标签和容器div变得非常简单。一个小例子: FormBuilder::addInputField("Close","close_time",$data->getClose_time()); 生成: <tr> <th>

我是SpringBoot的新手,现在我们想用SpringBoot2和Thymeleaf3作为模板引擎用Java重写我们的旧PHP应用程序

我们的传统应用程序有数百个表单和数千个输入字段。为此,我们使用了一个简单的模板辅助程序,它使渲染输入字段、标签和容器div变得非常简单。一个小例子:

FormBuilder::addInputField("Close","close_time",$data->getClose_time());
生成:

<tr>
    <th>Close:</th>
    <td><input type="text" name="close_time" id="close_time_id" size="30" value=""> </td>
</tr>

关闭:

我怎样才能在春季和冬季达到这个目标

选项1。使用Thymeleaf片段

formBuilder.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<tr th:fragment="inputField (title, name, value)">
    <th th:text="${title}"> or can use [[${title}]]</th>
    <td><input type="text" th:name="${name}" 
               id="close_time_id" size="30" th:value="${value}"> </td>
</tr>
</body>
</html>

标题
或者可以使用[${title}]]
然后在主布局中可以使用

<body>
<table>
    <tbody>
    <tr th:replace="formBuilder::inputField ('Close','close_time', ${value})"></tr>
    </tbody>
</table>
</body>


选项2。使用SpringBean服务

Thymeleaf允许使用@beanName语法()访问在Spring应用程序上下文中注册的bean,例如:

<div th:text="${@formBuilder.addInputField('Close','close_time')}">...</div>
。。。

是的,我们使用的是Thymeleaf v3。两个选项都有效,谢谢!我还认识到一件事:replace似乎只处理片段,而不处理bean服务。bean服务是否可以以th:replace这样的形式使用它?我的意思是消除输出中的holder元素?因此,在上面的示例中,我不希望holder div标记只是bean服务的输出。@vmx使用
th:remove=“tag”
来消除holder标记,或者我认为,您可以只使用
[${@formBuilder.addInputField('Close','Close_time')}]
而不使用任何标记