jkf*_*kfe 3 java spring spring-mvc thymeleaf
我有一个带有 thymeleaf 的 Spring MVC 应用程序。
根据控制器方法中测试的条件,我想从视图中显示或隐藏 html 元素(输入、跨度、div、按钮...)。
怎么做?例如,在 asp.net 中,如果您想要或不想显示它,可以执行 myButton.Visible = false(或 true)。
春天的百里香里有类似的东西吗?谢谢。
您可以通过传递属性来实现它
org.springframework.ui.Model
并使用 Thymeleaf 的th:if
属性
演示:
package com.example.demo.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class MealController {
@GetMapping("/order")
public String getCondition(@RequestParam(required = false) String myMeal, Model model) {
model.addAttribute("meal", myMeal);
return "meal/meal-site";
}
}
Run Code Online (Sandbox Code Playgroud)
资源/模板/meal-site.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Hot Dog?</title>
</head>
<body>
<div th:if="'hotdog' == ${meal}">A hotdog! </div>
<div th:if="'hotdog' != ${meal}">Not a hotdog </div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8106 次 |
最近记录: |