在我的.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>
现在,我的第1页显示:
here is the result: <p style="color:green;font-size:20px"><strong>pass</strong></p>
是否可以将我的结果变量呈现为html,以便我的page1显示一个大的绿色通行证?除了th:text+${var}之外,还有其他格式选项吗?我用的是弹簧靴和百里香。我尽量不使用javascript。
像这样的东西,但对于java
使用这个:
th:utext="'here is the result: ' + ${result}"
这在控制器级别是表示和语义的糟糕混合,在HTML级别是内容和样式的糟糕混合。相反,请为result
使用布尔属性,并使用th:if
或类似的属性来切换模板中的HTML内容,避免使用内联样式,而使用类似class="success"
的样式。