### th:each
controller
@RequestMapping("/{month}") public ModelAndView index(ModelAndView mav) { mav.setViewName("index"); ArrayList<String[]> data = new ArrayList<String[]>(); data.add(new String[] {"taro","taro@yamada","090-999-999"}); data.add(new String[] {"hanako","hanako@sato","080-888-888"}); data.add(new String[] {"sachiko","sachiko@saito","080-888-888"}); mav.addObject("data",data); return mav; }
<table> <tr> <th>NAME</th> <th>MAIL</th> <th>TEL</th> </tr> <tr th:each="obj:${data}"> <td th:text="${obj[0]}"></td> <td th:text="${obj[1]}"></td> <td th:text="${obj[2]}"></td> </tr> </table>
### プリプロセッシング
-> 式の一部を事前に評価させ、その結果を元に変数式を実行する
概念がイマイチよくわからんが。。
@RequestMapping("/{num}") public ModelAndView index(@PathVariable int num, ModelAndView mav) { mav.setViewName("index"); mav.addObject("num",num); if (num > 0) { mav.addObject("check","num >= data.size() ? 0 : num"); } else { mav.addObject("check", "num <= data.size() * -1 ? 0 : num * -1"); } ArrayList<DataObject> data = new ArrayList<DataObject>(); data.add(new DataObject(0, "taro","taro@yamada")); data.add(new DataObject(1, "hanako","hanako@sato")); data.add(new DataObject(2, "sachiko","sachiko@saito")); mav.addObject("data",data); return mav; }
html
<p th:text="|expression[ ${check} ]|"></p> <table th:object="${data.get(__${check}__)}"> <tr> <th>ID</th> <td th:text="*{id}"></td> </tr> <tr> <th>NAME</th> <td th:text="*{name}"></td> </tr> <tr> <th>MAIL</th> <td th:text="*{value}"></td> </tr> </table>
ん? 意味がわからん。
### インライン処理
th:inline=”text”と書くと、そのタグの内部のみインライン処理が可能になる
@RequestMapping("/") public ModelAndView index(ModelAndView mav) { mav.setViewName("index"); ArrayList<DataObject> data = new ArrayList<DataObject>(); data.add(new DataObject(0, "taro","taro@yamada")); data.add(new DataObject(1, "hanako","hanako@sato")); data.add(new DataObject(2, "sachiko","sachiko@saito")); mav.addObject("data",data); return mav; }
html
<table th:inline="text"> <tr> <th>ID</th> <th>NAME</th> <th>MAIL</th> </tr> <tr th:each="obj: ${data}"> <td>[[${obj.id}]]</td> <td>[[${obj.name}]]</td> <td>[[${obj.value}]]</td> </tr> </table>
#### jsのインライン処理
@RequestMapping("/{tax}") public ModelAndView index(@PathVariable int tax, ModelAndView mav) { mav.setViewName("index"); mav.addObject("tax",tax); return mav; }
<h1>Helo page</h1> <p id="msg"></p> <input type="text" id="text1"> <button onclick="action()">click</button> <script th:inline="javascript"> function action(){ var val = document.getElementById("text1").value; var res = parseInt(val * ((100 + /*[[${tax}]]*/) /100 )); document.getElementById("msg").innerHTML = "include tax: " + res; } </script>
js側に渡せるってこと?
よくわからんが凄え。