「Spring Boot」Thymeleafでオブジェクト情報を取得するサンプル
サンプルコード
@Controller
public class StudController {
//Beanをインジェクション
@Resource
private StudentServiceImp stu;
@RequestMapping(value = "/")
public String index(Model model){
//オブジェクトの情報の取得
Student stuObj = stu.findStudentById(20210115002);
//attributeの設定
model.addAttribute("resultObj",stuObj);
return "index";
}
}
JSP画面
//オブジェクトの属性情報を取得
<table th:each="cft:${resObj}">
<tr >
<td>学生ID</td>
<td th:text="${cft.getId(}"></td>
</tr>
<tr >
<td>学生名前</td>
<td th:text="${cft.getName()}"></td>
</tr>
<tr >
<td>学生点数</td>
<td th:text="${cft.getScore()}"></td>
</tr>
</table>