「Thymeleaf」変数の値を表示するサンプル
書式
変数式 ${変数名}
th:text="${変数名}"
Javaコード
@Controller
public class UserController {
@GetMapping("/user")
public String user(Model model) {
String str="study skill in arkgame.com";
model.addAttribute("testMsg",str);
User user= new User(21,"user001");
model.addAttribute("cftuser",user);
}
}
@Controller
public class UserController {
@GetMapping("/user")
public String user(Model model) {
String str="study skill in arkgame.com";
model.addAttribute("testMsg",str);
User user= new User(21,"user001");
model.addAttribute("cftuser",user);
}
}
@Controller public class UserController { @GetMapping("/user") public String user(Model model) { String str="study skill in arkgame.com"; model.addAttribute("testMsg",str); User user= new User(21,"user001"); model.addAttribute("cftuser",user); } }
htmlコード
<span th:text="${testMsg}"></span>
<div> <span th:text="${cftuser.username}"></span>
</div>
<span th:text="${testMsg}"></span>
<div> <span th:text="${cftuser.username}"></span>
</div>
<span th:text="${testMsg}"></span> <div> <span th:text="${cftuser.username}"></span> </div>