我的Spring Boot应用程序运行3个配置:
如何进入百里香叶环境应用程序正在运行?
我只需要在生产环境中包含Google Analytics代码.
我正在将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) 我是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)
谢谢你的帮助
我如何检查是否变量定义在Thymeleaf?
在Javascript中有类似的东西:
if (typeof variable !== 'undefined') { }
Run Code Online (Sandbox Code Playgroud)
或者这在PHP中:
if (isset($var)) { }
Run Code Online (Sandbox Code Playgroud)
在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) 我需要从消息资源中设置自定义属性(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) 我读到现在首选的是thymeleaf而不是JSP.百事可乐解决了JSP无法解决的问题?百里香叶与其他模板引擎相比如何?
我有一个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) 我正在使用Spring Boot,我想添加IE条件评论Thymeleaf方言.
我把它包含在我的maven pom.xml中,但它不起作用.我如何告诉Thymeleaf使用它?
如您所知,input组件具有一个属性,checked是否将复选框标记为默认启用.
<input type="checkbox" name="mycheckbox" checked="checked"/>
Run Code Online (Sandbox Code Playgroud)
要在默认情况下禁用该复选框,checked应声明该异常.是否可以checked通过Thymeleaf中的旗帜设置属性?
thymeleaf ×10
spring ×5
spring-boot ×3
spring-mvc ×3
java ×2
arrays ×1
environment ×1
html ×1
html-input ×1
jsp ×1
loops ×1
paragraph ×1