[Spring Boot]Modelクラスでリスト(list)の値を渡すサンプルコード
Javaコード
@Controller
public class StudentController {
@Resource
private StudentServiceImp stt;
@RequestMapping(value = "/")
public String index(Model model){
//学生情報の検索
List<Student> lstA = stt.findStudentByAge(18);
//Modelにリストを登録
model.addAttribute("resLst",lstA)
return "index";
}
}
@Controller
public class StudentController {
@Resource
private StudentServiceImp stt;
@RequestMapping(value = "/")
public String index(Model model){
//学生情報の検索
List<Student> lstA = stt.findStudentByAge(18);
//Modelにリストを登録
model.addAttribute("resLst",lstA)
return "index";
}
}
@Controller public class StudentController { @Resource private StudentServiceImp stt; @RequestMapping(value = "/") public String index(Model model){ //学生情報の検索 List<Student> lstA = stt.findStudentByAge(18); //Modelにリストを登録 model.addAttribute("resLst",lstA) return "index"; } }
JSPコード
<table th:each="res:${resLst}">
<tr >
<td>ユーザID</td>
<td th:text="${res.id}"></td>
</tr>
<tr >
<td>ユーザ名</td>
<td th:text="${res.name}"></td>
</tr>
<tr >
<td>点数</td>
<td th:text="${res.score}"></td>
</tr>
</table>
<table th:each="res:${resLst}">
<tr >
<td>ユーザID</td>
<td th:text="${res.id}"></td>
</tr>
<tr >
<td>ユーザ名</td>
<td th:text="${res.name}"></td>
</tr>
<tr >
<td>点数</td>
<td th:text="${res.score}"></td>
</tr>
</table>
<table th:each="res:${resLst}"> <tr > <td>ユーザID</td> <td th:text="${res.id}"></td> </tr> <tr > <td>ユーザ名</td> <td th:text="${res.name}"></td> </tr> <tr > <td>点数</td> <td th:text="${res.score}"></td> </tr> </table>