【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));
        
    }
}

輸出:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM