标签: thymeleaf

Thymeleaf:如果属性和属性存在,则显示文本

如果属性和属性存在,是否有一种简单的方法在百万富翁中显示属性属性的内容?如果我的html页面中存在属性"summary"的属性"error",我想展示它:

<span th:text="${error.summary}">error summary</span>
Run Code Online (Sandbox Code Playgroud)

如果没有属性"error",则会引发以下错误:

org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Field or property 'summary' cannot be found on null

目前我正在使用以下方法,这似乎太复杂了.

<span th:if="${error != null and error.summary != null}"><span th:text="${error.summary}">error summary</span></span>
Run Code Online (Sandbox Code Playgroud)

有没有更简单的方法来实现这一目标?

spring spring-el thymeleaf

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

使用Thymeleaf模板而不包含片段定义元素?

假设我有两个Thymeleaf模板:

index.html:

<!DOCTYPE html>
<html>
<head></head>
<body>
<header>foo</header>
<section>
    <div th:replace="fragments/main :: content"></div>
</section>
<footer>bar</footer>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

fragments/main.html:

<!DOCTYPE html>
<html>
<head></head>
<body>
<div th:fragment="content">
    <p>This is the main content.</p>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

如何防止Tymeleaf div在合成输出中包含定义片段的内容?也就是说,我如何让Thymleaf生成以下输出:

<!DOCTYPE html>
<html>
<head></head>
<body>
<header>foo</header>
<section>
    <p>This is the main content.</p>
</section>
<footer>bar</footer>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

代替:

<!DOCTYPE html>
<html>
<head></head>
<body>
<header>foo</header>
<section>
    <div>
        <p>This is the main content.</p>
    </div>
</section>
<footer>bar</footer>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

thymeleaf

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

如何使用Thymeleaf动态设置HTML元素的id属性

假设我有一个对象:$ {object}

我有以下表格:

<form id="{{'myForm' + object.id}" class="some class"
      th:action="@{/doSomething}" method="post">
    ....
</form>
Run Code Online (Sandbox Code Playgroud)

如果我们假设object.id为'1',我的目标是设置id ="myForm1".

PS:我写它的方式正在研究Angular JS.

html rest spring thymeleaf

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

对于Thymeleaf的每个运营商

我找不到在Thymeleaf模板中构建简单for-each-loop的语法.我对just th:each=""属性不满意,因为它复制了它所在的标签.

我正在寻找的是:

<th:foreach th:each="...">
...block to be repeated...
</th>
Run Code Online (Sandbox Code Playgroud)

什么是模拟<c:forEach items="..." var="..."><t:loop source="..." value="...">Tapestry.有什么相似的吗?

foreach spring for-loop spring-mvc thymeleaf

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

IntelliJ中的Spring boot + thymeleaf:无法解析变量

我正在IntelliJ上使用spring boot和thymeleaf编写一个简短的Web表单应用程序,但似乎在html文件中,模型中的所有字段都无法解析.这是我的代码:

控制器类:

@Controller
public class IndexController{

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(){
        return "index";
    }

    @RequestMapping(value="/", method = RequestMethod.POST)
    public String addNewPost(@Valid Post post, BindingResult bindingResult, Model model){
        if(bindingResult.hasErrors()){
            return "index";
        }
        model.addAttribute("title",post.getTitle());
        model.addAttribute("content",post.getContent());
        return "hello";
    }
}
Run Code Online (Sandbox Code Playgroud)

型号类:

public class Post {

    @Size(min=4, max=35)
    private String title;

    @Size(min=30, max=1000)
    private String content;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    } …
Run Code Online (Sandbox Code Playgroud)

java spring intellij-idea web thymeleaf

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

从Thymeleaf进入课堂

如何从Thymeleaf访问一些简单的POJO静态属性?例如:

public final static int PROJECT_NAME_MAX_LENGTH = 255;
Run Code Online (Sandbox Code Playgroud)

如何输入:

<input type="text" th:maxlength="??" />
Run Code Online (Sandbox Code Playgroud)

java thymeleaf

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

Thymeleaf - 严格的HTML解析问题

HTML5允许更自由地编写某些标签,即没有相应的END标签.例如,input不需要关闭.</input>但是如果在Thymeleaf中选择模板模式 HTML5,那么Thymeleaf引擎会抱怨这个并且不会解析HTML模板.我想覆盖此默认的严格标记检查行为.即Thymeleaf应解析带有元和输入(AND ALIKE)标签的HTML模板,不带其RESP.关闭标签.PL.指南.

当你有这样的事情时,它也会抱怨

<a href="/home/pic/image.png" download="/path/to/file" data-gallery></a>
Run Code Online (Sandbox Code Playgroud)

当它遇到数据库引发时会引发异常"应该跟着'='"这有点令人讨厌,因为它需要HTML5的灵活性.

html html5 thymeleaf

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

解析模板"index"时出错,模板可能不存在,或者任何已配置的模板解析器都可能无法访问

之前已经问过这个问题,但我没有解决我的问题而且我得到了一些奇怪的功能.

如果我将index.html文件放在静态目录中,如下所示:

在此输入图像描述

我在浏览器中收到以下错误:

在此输入图像描述

在我的控制台中:

[THYMELEAF][http-nio-8080-exec-3] Exception processing template "login": 
Exception parsing document: template="login", line 6 - column 3
2015-08-11 16:09:07.922 ERROR 5756 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].
[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] 
in context with path [] threw exception [Request processing failed; nested 
exception is org.thymeleaf.exceptions.TemplateInputException: Exception 
parsing document: template="login", line 6 - column 3] with root cause

org.xml.sax.SAXParseException: The element type "meta" must be terminated by 
the matching end-tag "</meta>".
Run Code Online (Sandbox Code Playgroud)

但是,如果我将index.html文件移动到templates目录中,我的浏览器中会出现以下错误: 在此输入图像描述

在此输入图像描述

我添加了我的视图解析器:

@Controller
@EnableWebMvc
public class WebController extends WebMvcConfigurerAdapter { …
Run Code Online (Sandbox Code Playgroud)

thymeleaf spring-boot

22
推荐指数
7
解决办法
12万
查看次数

如何使用thymeleaf为html元素分配多个属性

我的百里香页面上有一个选择框.我已经为它定义了一个属性,如:

th:attr="labelId='associateTSF' + ${mViewStat.index}"
Run Code Online (Sandbox Code Playgroud)

有没有办法设置多个?就像是:

th:attr="labelId='associateTSF' + ${mViewStat.index}; missionGroup=${mView.missionGroup}"
Run Code Online (Sandbox Code Playgroud)

我已经试过了这个; 而且空白,没有成功.我发现的所有例子都是单值.

谢谢!

html spring-mvc thymeleaf

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

Thymeleaf + Spring:如何保持断线?

我正在使用带弹簧的Thymeleaf模板引擎,我想显示通过多行textarea存储的文本.

在我的数据库中,多行字符串存储为"\n",如下所示:"Test1 \nTest2 \n ......"

用th:text得到了:"Test1 Test2"没有换行符.

如何使用Thymeleaf显示换行符并避免手动"\n"替换为<br />然后避免使用th:utext(这种开放形式到xss注入)?

谢谢 !

spring spring-el thymeleaf

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