![](/img/trans.png)
[英]Calling Java Method/Variable in HTML - Thymeleaf Spring Boot
[英]Render a variable as HTML in Java Spring Thymeleaf
在 my.java 文件中,我有
@RequestMapping(value = "/page1", method = RequestMethod.GET)
public String callback(@RequestParam(name = "token") String token, Model model){
//do something with token
//result variable is either
//"<p style=\"color:red;font-size:20px\"><strong>fail</strong></p>"
//or
//"<p style=\"color:green;font-size:20px\"><strong>pass</strong></p>"
model.addAttribute("result", result);
return "page1";
在我的 page1.html 文件中:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Page1</title>
</head>
<body>
<p th:text="'here is the result: ' + ${result}"></p>
</body>
</html>
现在,我的 page1 显示:
here is the result: <p style="color:green;font-size:20px"><strong>pass</strong></p>
是否可以将我的结果变量呈现为 html 以便我的 page1 显示一个大的绿色通道? 除了 th:text + ${var},还有其他格式选项吗?我使用的是 spring 引导和 thymeleaf。 我试图不为此使用 javascript 。
像这样的东西,但对于 java
这是 controller 级别的表示和语义的错误混合,以及 HTML 级别的内容和样式的错误混合。 相反,使用 boolean 属性作为result
并使用th:if
或类似的东西来切换模板中的 HTML 内容,并避免内联 styles 以支持class="success"
使用这个:
th:utext="'here is the result: ' + ${result}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.