2013-02-25 35 views
6

Tôi gặp sự cố khi hiển thị danh sách thả xuống có các giá trị thích hợp. Tôi đang sử dụng các thẻ <spring-form:select>, <spring-form:options><spring-form:option> và tôi không thể hiển thị thẻ để hiển thị các tùy chọn chính xác. Với mã sau, tôi chỉ nên liệt kê "Tùy chọn 2", "Tùy chọn 7" và "Tùy chọn 8".hình thức mùa xuân: thẻ tùy chọn với enum

* Lưu ý - Tôi KHÔNG muốn hiển thị mọi giá trị Enum có thể, nhưng vì lý do nào đó, Spring có vẻ muốn hiển thị tất cả. Dường như hoàn toàn bỏ qua danh sách được cung cấp cho thẻ <spring-form:options>.

JSP Thẻ

<spring-form:select path="selectOptions"> 
    <spring-form:option value="" label="*** Select Option ***" /> 
    <spring-form:options path="${availableOptions}" /> 
</spring-form:select> 

Enum

public enum SelectOptions { 

    // CHECKSTYLE_OFF: LineLength 

    /** 
    * Option 1. 
    */ 
    OPTION_1(1, "Option 1"), 
    /** 
    * Option 2. 
    */ 
    OPTION_2(2, "Option 2"), 
    /** 
    * Option 3. 
    */ 
    OPTION_3(3, "Option 3"), 
    /** 
    * Option 4. 
    */ 
    OPTION_4(4, "Option 4"), 
    /** 
    * Option 5. 
    */ 
    OPTION_5(5, "Option 5"), 
    /** 
    * Option 6. 
    */ 
    OPTION_6(6, "Option 6"), 
    /** 
    * Option 7. 
    */ 
    OPTION_7(7, "Option 7"), 
    /** 
    * Option 8. 
    */ 
    OPTION_8(8, "Option 8"), 
    /** 
    * Option 9. 
    */ 
    OPTION_9(9, "Option 9"), 
    /** 
    * Option 10. 
    */ 
    OPTION_10(10, "Option 10"); 

    private int id; 
    private String description; 

    /** 
    * Constructor. 
    * 
    * @param id the id 
    * @param description the description 
    */ 
    private SelectOptions(final int id, final String description) { 
     this.id = id; 
     this.description = description; 
    } 

    /** 
    * Retrieves the {@link SelectOptions} associated with the passed in id. 
    * 
    * @param id the id associated with the SelectOptions 
    * @return the SelectOptions 
    */ 
    public static SelectOptions getInstance(final int id) { 

     for (final SelectOptions selectOptions : SelectOptions.values()) { 
      if (selectOptions.id == id) { 
       return selectOptions; 
      } 
     } 

     throw new IllegalArgumentException("SelectOptions could not be determined with id [" + id + "]"); 
    } 

    /** 
    * Retrieves the {@link SelectOptions} associated with the passed in description. 
    * 
    * @param description the description associated with the SelectOptions 
    * @return the SelectOptions 
    */ 
    public static SelectOptions getInstance(final String description) { 

     for (final SelectOptions selectOptions : SelectOptions.values()) { 
      if (selectOptions.description == description) { 
       return selectOptions; 
      } 
     } 

     throw new IllegalArgumentException("SelectOptions could not be determined with description [" + description + "]"); 
    } 

    /** 
    * Simple Getter. 
    * 
    * @return the id 
    */ 
    public int getId() { 
     return this.id; 
    } 

    /** 
    * Simple Setter. 
    * 
    * @param id the id to set 
    */ 
    public void setId(final int id) { 
     this.id = id; 
    } 

    /** 
    * Simple Getter. 
    * 
    * @return the description 
    */ 
    public String getDescription() { 
     return this.description; 
    } 

    /** 
    * Simple Setter. 
    * 
    * @param description the description to set 
    */ 
    public void setDescription(final String description) { 
     this.description = description; 
    } 
} 

khiển

public class SpringController implements SpringControllerInterface { 

    /** 
    * /WEB-INF/jsp/myJSP.jsp. 
    */ 
    private static final String PAGE = "/WEB-INF/jsp/myJSP.jsp"; 

    /** 
    * {@inheritDoc} 
    */ 
    @Override 
    public ModelAndView load(final Model model) { 

     final ModelAndView mav = new ModelAndView(PAGE); 

     final List<SelectOptions> availableOptions = this.getAvailableOptions(); 

     mav.addObject("availableOptions", availableOptions); 

     return mav; 
    } 

    /** 
    * {@inheritDoc} 
    */ 
    @Override 
    public ModelAndView save(final Model model) { 

     final ModelAndView mav = new ModelAndView(PAGE); 

     // TODO 

     return mav; 
    } 

    /** 
    * {@inheritDoc} 
    */ 
    @Override 
    public Model getModel() { 
     return new ModelImpl(); 
    } 

    /** 
    * @return a list of available options 
    */ 
    public List<SelectOptions> getAvailableOptions() { 

     final List<SelectOptions> availableOptions = Lists.newArrayList(); 

     availableOptions.add(SelectOptions.OPTION_1); 
     availableOptions.add(SelectOptions.OPTION_7); 
     availableOptions.add(SelectOptions.OPTION_8); 

     return availableOptions; 
    } 
    } 

Mẫu

public class ModelImpl implements Model { 

    private SelectOptions selectOptions; 

    /** 
    * Simple Getter. 
    * 
    * @return the selectOptions 
    */ 
    @Override 
    public SelectOptions getSelectOptions() { 
     return this.selectOptions; 
    } 

    /** 
    * Simple Setter. 
    * 
    * @param selectOptions the selectOptions to set 
    */ 
    @Override 
    public void setSelectOptions(final SelectOptions selectOptions) { 
     this.selectOptions = selectOptions; 
    } 


} 
+0

btw, tôi có thể sử dụng html và c thẻ để tạo danh sách -Nhưng tôi muốn để có thể thực sự sử dụng các thẻ dạng mùa xuân một cách thích hợp. Tôi chắc chắn rằng tôi chỉ thiếu một cái gì đó ngớ ngẩn. – Mostfoolish

+0

Tôi cho rằng bạn đã kiểm tra trả về getAvailableOptions() để đảm bảo danh sách các tùy chọn có sẵn là chính xác? Bạn có thể cung cấp mã đó và có thể là HTML mà bạn kết thúc sau khi mọi thứ được hiển thị cho trình duyệt không? – CodeChimp

+0

availableOptions là danh sách mà tôi vừa tạo trong Controller. Và có, tôi đã xác nhận rằng nó có 3 lựa chọn mà tôi đã mong đợi trong đó. – Mostfoolish

Trả lời

3

Dường như giải pháp cho vấn đề này là tôi đang sử dụng thuộc tính "đường dẫn" trong thẻ <spring-form:options>. Nó phải là "vật phẩm" chứ không phải "đường dẫn".

JSP đoạn điều chỉnh:

<spring-form:select path="selectOptions"> 
    <spring-form:option value="" label="*** Select Option ***" /> 
    <spring-form:options items="${availableOptions}" /> 
</spring-form:select> 
0

Bạn thậm chí không cần sử dụng các thuộc tính mục nếu bạn đang sử dụng thẻ <spring-form:options>.
Thẻ tùy chọn:
Cho mượn danh sách các thẻ 'tùy chọn' HTML. Đặt 'được chọn' là thích hợp dựa trên giá trị giới hạn.
Tham chiếu Spring form tld. Vì vậy, cho nhu cầu của bạn, tôi đoán rơi trở lại JSTL core với <spring-form:option/> sẽ đủ.

+1

Làm điều đó hiển thị mọi giá trị bên trong Enum-đó là KHÔNG những gì tôi muốn làm. – Mostfoolish

11

Nếu bạn đã tạo một bộ điều khiển mùa xuân và bạn muốn vượt qua một enum trang jsp, bạn có thể làm điều đó như thế này:

Enum dụ:

public enum Coin { 

    HEADS("Heads", "heads"), 
    TAILS("Tails", "tails"); 

    private final String fullName; 
    private final String shortName; 

    private Coin(String fullName, String shortName) { 
     this.fullName = fullName; 
     this.shortName = shortName; 
    } 

    public String getFullName() { 
     return fullName; 
    } 

    public String getShortName() { 
     return shortName; 
    } 

} 

Đi qua enum này để mô hình của bạn:

model.addObject("coins", Coin.values()); 

Sử dụng nó trong trang jsp của bạn với hình thức:

<form:select path="selection"> 
    <form:options items="${coins}" itemValue="shortName" itemLabel="fullName" /> 
</form:select> 
Các vấn đề liên quan