标签: thymeleaf

获得百里香的春季应用环境

我的Spring Boot应用程序运行3个配置:

  • application.properties - >用于开发环境
  • application-test.properties - >用于测试环境
  • application-production.properties - >用于生产环境

如何进入百里香叶环境应用程序正在运行?

我只需要在生产环境中包含Google Analytics代码.

environment spring thymeleaf spring-boot

28
推荐指数
2
解决办法
9347
查看次数

Thymeleaf不显示Spring表单错误消息

我正在将Spring jsp应用程序迁移到Thymeleaf但是在显示表单错误时遇到问题.

我正在使用SpringTemplateEngine和ThymeleafViewResolver并且模板的渲染工作.表单值也在表单输入字段中填充.

到目前为止唯一不起作用的是显示表单错误消息.

我的控制器看起来像:

@RequestMapping(method = RequestMethod.POST)
String save(@Valid CustomerForm form, BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes) {
    if (bindingResult.hasErrors()) {
        model.addAttribute("form", form)
        return "app/customers/create"
    }
    ....
Run Code Online (Sandbox Code Playgroud)

我打印了bindingResult以验证它是否包含错误:

binding result = org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'customerForm' on field 'name': rejected value []; codes [customerForm.name.NotBlank,name.NotBlank,java.lang.String.NotBlank,NotBlank]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [customerForm.name,name]; arguments []; default message [name]]; default message [may not be empty]
Run Code Online (Sandbox Code Playgroud)

当我尝试使用以下方法显示错误:

<ul>
    <li th:each="e : ${#fields.detailedErrors()}" th:class="${e.global}? globalerr : fielderr">
        <span th:text="${e.global}? '*' : ${e.fieldName}">The field name</span> …
Run Code Online (Sandbox Code Playgroud)

spring thymeleaf

27
推荐指数
2
解决办法
4万
查看次数

在ThymeLeaf中循环遍历数组

我是ThymeLeaf的新手,我想知道是否有办法循环<p>html标签以及遍历该<p>标签内的数组.我希望其中的元素smokeTest最终出现在不同的段落中.

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<p th:text="${smokeTests[0].name}"/>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助

arrays loops paragraph thymeleaf

27
推荐指数
1
解决办法
4万
查看次数

Thymeleaf:检查是否定义了变量

我如何检查是否变量定义在Thymeleaf?

在Javascript中有类似的东西:

if (typeof variable !== 'undefined') { }
Run Code Online (Sandbox Code Playgroud)

或者这在PHP中:

if (isset($var)) { }
Run Code Online (Sandbox Code Playgroud)

在Thymeleaf中有相同的东西吗?

java spring spring-mvc thymeleaf spring-boot

27
推荐指数
3
解决办法
6万
查看次数

具有Spring Security的Thymeleaf - 如何检查用户是否已登录?

我正在使用带有Thymeleaf和Spring Security的Spring Boot.我有一个带登录链接的简单视图.当用户登录时,我想将登录链接更改为注销链接.

我试过了:

<div sec:authorize="#{isAuthenticated()}">
  <a th:href="@{/logout}">Log out</a>
</div>
<div sec:authorize="#{isAnonymous()}">
  <a th:href="@{/login}">Log in</a>
</div>
Run Code Online (Sandbox Code Playgroud)

但它不起作用 - 它显示两个链接.

最好的祝福.

编辑:我解决了.我必须注册Thymeleaf方言.为了做到这一点,我创建了一个新的配置类,它创建了SpringSecurityDialect bean:

@Configuration
public class ThymeleafConfig {

    @Bean
    public SpringSecurityDialect springSecurityDialect(){
        return new SpringSecurityDialect();
    }
}
Run Code Online (Sandbox Code Playgroud)

spring spring-mvc spring-security thymeleaf

27
推荐指数
1
解决办法
2万
查看次数

Thymeleaf - 自定义属性

我需要从消息资源中设置自定义属性(data-validation-matches-message)值.

<input data-validation-matches-message="Text from messages resources" />
Run Code Online (Sandbox Code Playgroud)

我可以接收和打印消息资源值为:

<p th:text="#{user.notfound}"></p>
Run Code Online (Sandbox Code Playgroud)

但是如何为自定义属性设置此值(data-validation-matches-message)?

UPD(我用这个)

<input th:attr="data-validation-matches-message=#{user.notfound}"/>
Run Code Online (Sandbox Code Playgroud)

spring-mvc thymeleaf

26
推荐指数
2
解决办法
1万
查看次数

百里香的解决方案是什么?

我读到现在首选的是thymeleaf而不是JSP.百事可乐解决了JSP无法解决的问题?百里香叶与其他模板引擎相比如何?

java jsp template-engine thymeleaf

26
推荐指数
1
解决办法
1万
查看次数

Thymeleaf - 格式化时检查NULL的最佳做法(即日期)

我有一个Thymeleaf模板代码来格式化日期.有时候该日期将null在返回的对象中.null在这种情况下,在Thymeleaf 检查的最佳方法是什么?目前该模板抛出以下错误:

Caused by: java.lang.IllegalArgumentException: Cannot apply format on null
    at org.thymeleaf.util.Validate.notNull(Validate.java:37)
    at org.thymeleaf.util.DateUtils.format(DateUtils.java:182)
    at org.thymeleaf.expression.Dates.format(Dates.java:164)
Run Code Online (Sandbox Code Playgroud)

thymeleaf

24
推荐指数
2
解决办法
2万
查看次数

如何在弹簧靴中添加百里香方言?

我正在使用Spring Boot,我想添加IE条件评论Thymeleaf方言.

我把它包含在我的maven pom.xml中,但它不起作用.我如何告诉Thymeleaf使用它?

spring thymeleaf spring-boot

24
推荐指数
3
解决办法
2万
查看次数

Thymeleaf - 如何有条件地将checked属性添加到输入

如您所知,input组件具有一个属性,checked是否将复选框标记为默认启用.

<input type="checkbox" name="mycheckbox" checked="checked"/>
Run Code Online (Sandbox Code Playgroud)

要在默认情况下禁用该复选框,checked应声明该异常.是否可以checked通过Thymeleaf中的旗帜设置属性?

html html-input thymeleaf

24
推荐指数
2
解决办法
5万
查看次数