1. 定义枚举类,用来封装下拉框数据
package com.example.jmw.common.enums;
public enum CsvMasterTypes {
受注ファイル("1", "受注ファイル"),
受注先マスタ("2", "受注先マスタ");
private String code;
private String name;
CsvMasterTypes(String code, String name) {
this.code = code;
this.name = name;
}
public String getCode() {
return code;
}
public String getName() {
return name;
}
}
2.创建一个form,用来封装返回到前台的数据
import com.example.jmw.common.enums.CsvMasterTypes;
import lombok.Data;
@Data
public class Test12Form {
// 自定义枚举Array类型
private CsvMasterTypes[] csvMasterTypes;
}
3. Controller层直接将数据放到ModelAndView中返回给前台
@Controller
@RequestMapping("/test12")
public class Test12Controller {
// 页面初期表示
@RequestMapping("/init")
public ModelAndView init() {
Test12Form test12Form = new Test12Form();
// 将自定义枚举类放入form对象中
test12Form.setCsvMasterTypes(CsvMasterTypes.values());
// 指定页面名称和要返回到页面中的数据
ModelAndView modelView = new ModelAndView();
modelAndView.setViewName("test12");
modelAndView.addObject("entity", test12Form);
return modelAndView;
}
}
4. 前台遍历获取
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<title>展示后台的枚举类数据</title>
</head>
<body>
<label>
<select th:object="${entity}">
<!--
csvMasterTypes相当于后台form中的CsvMasterTypes[]
enum相当于我们自定义的枚举类
-->
<th:block th:each="enum : *{csvMasterTypes}">
<option th:value="${enum.code}">[[${enum.name}]]</option>
</th:block>
</select>
</label>
</body>
</html>
5. 效果