thymeleaf循环 foreach

thymeleaf的 foreach循环功能
通过该功能进行下拉框的构造
html代码如下:

  

后台java拼接json代码如下:

 			List subjects = subjectMapper.selectList(queryWrapper);
            JSONArray listJa = new JSONArray();
            JSONObject tmpJo = new JSONObject();
            tmpJo.put("code","");
            tmpJo.put("name","");
            listJa.add(tmpJo);
            for (Subject subject:subjects) {
                tmpJo = new JSONObject();
                tmpJo.put("code",subject.getSubjectCode());
                tmpJo.put("name",subject.getSubjectNam());
                listJa.add(tmpJo);
            }

th:each的代码和java中的foreach是等价的
这里有一个需要注意的点是th:each是写在option中的,即对option进行循环,如果写在select标签中,会构造出th:each中数据长度对应个数的下拉框
通过上面代码构造出的下拉框如下:
thymeleaf循环 foreach_第1张图片

你可能感兴趣的:(thymeleaf)