2013-02-25 4 views
6

適切な値でドロップダウンリストを表示する際に問題が発生しています。 <spring-form:select><spring-form:options><spring-form:option>タグを使用していますが、正しいオプションを表示することができません。次のコードでは、「Option 2」、「Option 7」、および「Option 8」のみをリストアップする必要があります。spring-form:enumのオプションタグ

*注 - 考えられるすべての列挙値を表示したくないのですが、何らかの理由でSpringにすべての値を表示したいようです。 <spring-form:options>タグに提供されるリストを完全に無視しているようです。

JSPタグ

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

列挙

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

コントローラ

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

モデル

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

、私は私が実際に使用できるようにしたいと思いブタリストを作成するには、HTMLとCのタグを使用することができますspring-formタグを適切に使用します。私はちょっとばかげたものを逃していると確信しています。 – Mostfoolish

+0

getAvailableOptions()リターンをチェックして、使用可能なオプションのリストが正しいことを確認したとしますか?あなたはそのコードを提供することができますか、そしておそらくすべてがブラウザにレンダリングされた後に終わるHTMLでしょうか? – CodeChimp

+0

availableOptionsはコントローラで単純に作成したリストでした。そして、はい、私はそれが私がそれで期待していた3つのオプションがあることを確認しました。 – Mostfoolish

答えて

3

この問題の解決策は、<spring-form:options>タグで属性「パス」を使用していたようです。それは "項目"であってはならず、 "経路"ではありませんでした。

修正JSPフラグメント:

列挙例:あなたは春のコントローラを作成し、あなたのJSPページに列挙型を渡したい場合は

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

<spring-form:options>タグを使用している場合、items属性を使用する必要はありません。
オプションタグ:
HTMLオプションタグのリストを表示します。バインドされた値に基づいて適切に 'selected'を設定します。
参考文献Spring form tld。 あなたの必要性のために私はJSTL coreに戻ると思います。<spring-form:option/>で十分です。

+1

これを実行すると、Enum内のすべての値が表示されます。これは実行したくない値です。 – Mostfoolish

11

は、あなたはこのようにそれを行うことができます

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

} 

モデルにこの列挙を渡す:

フォームでのJSPページでそれを使用して
model.addObject("coins", Coin.values()); 

:ところで

<form:select path="selection"> 
    <form:options items="${coins}" itemValue="shortName" itemLabel="fullName" /> 
</form:select> 
関連する問題