「Spring Boot」Thymeleafでオブジェクト情報を取得するサンプル

サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@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";
}
}
@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"; } }
@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画面

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
//オブジェクトの属性情報を取得
<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>
//オブジェクトの属性情報を取得 <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>
//オブジェクトの属性情報を取得
<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>

 

SpringMVC

Posted by arkgame