[Spring Boot]Modelクラスでリスト(list)の値を渡すサンプルコード

Javaコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@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コード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>

 

Spring Boot

Posted by arkgame