【SpringBoot+Thymeleaf】枚举类实现下拉框

效果如下:

代码如下:

1.前端:HTML

          <div>
            <label for="type">Type</label>
            <select id="type">
              <option th:each="type : ${allTypes}" th:value="${type}" th:text="#{${'seedstarter.type.' + type}}">Wireframe</option>
            </select>
          </div>

  其中 #{${'seedstarter.type.' + type}} 是与从 ${allTypes} 遍历出来的 type 进行拼接。根据拼接结果从 messages.properties 文件里读取符合的值,赋回给前端的option标签的text属性的值。

 messages.properties 里存储的值如下:

seedstarter.type.WOOD=Wood
seedstarter.type.PLASTIC=Plastic

  浏览器F12后的显示如下:

 

 

 

2.后端:Controller

@ModelAttribute("allTypes")
    public List<Type> populateTypes() {
        return Arrays.asList(Type.ALL);
    }

  注意:其实还有一个接收前端访问请求@RequestMapping的方法,只是不是本篇文章的重点,所以忽略掉了(但在下文的【@ModelAttribute有两个应用】里有出现)。

@ModelAttribute有两个应用:

  1)绑定参数,参数给方法。例如下面例子中@ModelAttribute的位置

@RequestMapping(value = {"/index","/"})
    public ModelAndView index(ModelAndView mv,@ModelAttribute SeedStarterForm seedStarterForm) {
        seedStarterForm.setDatePlanted(Calendar.getInstance().getTime());
        mv.addObject("seedStarterForm",seedStarterForm);
        mv.setViewName("index");
        return mv;
    }

  2)注释方法,返回值给前端。在有注释@RequestMapping的方法的基础上,可以另有一个有@ModelAttribute的方法,且这个方法的返回值会返回给前端。本篇的重点就是这个的应用。

 

3.后端:Entities

public enum Type {
    
    PLASTIC("PLASTIC"), 
    WOOD("WOOD");
    
    // 与values方法一样
    public static final Type[] ALL = { PLASTIC, WOOD };
    
    private final String name;

    private Type(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }


    @Override
    public String toString() {
        return getName();
    }
}

趁此补充/复习一下枚举类的知识:

1.在比较两个枚举类型的值时,永远不需要调用equals,而直接使用“==”就可以了。

2.构造器只是在构造枚举常量的时候被调用。

例子:

public enum Size {

    SMALL("S"),MEDIUM("M"),LARGE("L"),EXTRA_LAGER("XL");
    
    private String abbreviation;
    
    private Size(String abbreviation) {
        this.abbreviation = abbreviation;
    }
    
    public String getAbbreviation() {
        return abbreviation;
    }

    @Override
    public String toString() {
        return getAbbreviation();
    }
    
}
public class EnumTest {

    public static void main(String[] args) {
        // 最有用的方法,这个方法能够返回枚举常量名
        System.out.println(Size.SMALL.toString());// 如果重写了toString方法,则返回“S”;反之,则是“SMALL”。
        // valueOf是静态方法,也是toString的逆方法,toString是返回String型,那么valueOf就是返回Size这个枚举类型。
        System.out.println(Size.valueOf(Size.class, "SMALL"));
        
        Size size = Size.valueOf(Size.class, "SMALL");
        // 在比较两个枚举类型时,用“==”而不是用equals
        if (size == Size.SMALL) {
            System.out.println("good job");
        }
        // 每个枚举型都有一个values静态方法,它就返回一个包含全部枚举值的数组。
        Size[] values = Size.values();
        for (Size s:values) {
            System.out.println(s);
        }
        
        // ordinal方法返回enum声明中枚举变量的位置,位置从0开始计数。
        int ordinal = Size.MEDIUM.ordinal();
        System.out.println(ordinal);// 返回1
        
        // A.compareTo(B) 如果A出现在B之前,则返回一个负值;如果A==B,则返回0;否则,返回正值。
        System.out.println(Size.MEDIUM.compareTo(Size.SMALL));
        System.out.println(Size.MEDIUM.compareTo(Size.MEDIUM));
        System.out.println(Size.MEDIUM.compareTo(Size.LARGE));
        System.out.println(Size.MEDIUM.compareTo(Size.EXTRA_LAGER));
        
    }
}

输出:

 

posted @ 2020-06-17 15:10  yougatei  阅读(1989)  评论(0编辑  收藏  举报