2013-02-25 12 views
6

Sto riscontrando problemi nella visualizzazione di un elenco a discesa con i valori corretti. Sto usando i tag <spring-form:select>, <spring-form:options> e <spring-form:option> e non riesco a farlo visualizzare le opzioni corrette. Con il seguente codice dovrei elencare solo "Opzione 2", "Opzione 7" e "Opzione 8".spring-form: tag opzioni con enum

* Nota: NON desidero visualizzare tutti i valori Enum possibili, ma per qualche motivo Spring sembra volerli visualizzare tutti. Sembra che ignori completamente l'elenco fornito con il tag <spring-form:options>.

JSP Tag

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

controller

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

Modello

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, posso usare l'HTML e C tag per creare l'elenco -ma mi piacerebbe essere in grado di utilizzare effettivamente il etichette a forma di molla in modo appropriato. Sono sicuro che mi manca solo qualcosa di stupido. – Mostfoolish

+0

Suppongo tu abbia controllato il ritorno getAvailableOptions() per essere sicuro che l'elenco delle opzioni disponibili sia corretto? Potete fornire quel codice, e possibilmente l'HTML che si finisce dopo che tutto viene reso al browser? – CodeChimp

+0

le Opzioni disponibili erano una lista che ho semplicemente creato nel Controller. E sì, ho confermato che ha le 3 opzioni che mi aspettavo. – Mostfoolish

risposta

3

Sembra che la soluzione a questo problema era che stavo usando l'attributo "percorso" nel tag <spring-form:options>. Dovevano essere "articoli", non "percorsi".

il frammento JSP corretto:

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

Non è nemmeno necessario utilizzare l'attributo item se si utilizza il tag <spring-form:options>.
Il tag delle opzioni:
Rende un elenco di tag "opzione" HTML. Imposta 'selezionato' come appropriato in base al valore associato.
Riferimenti Spring form tld. Quindi, per il tuo bisogno, credo che sia sufficiente tornare a JSTL core con <spring-form:option/>.

+1

In questo modo viene visualizzato ogni valore all'interno dell'Enum, che NON è ciò che voglio fare. – Mostfoolish

11

Se è stato creato un controller primavera e si desidera passare un enum alla tua pagina JSP, si può fare in questo modo:

Enum esempio:

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

} 

Passando questo enum al modello:

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

di utilizzare nella pagina JSP con la forma:

<form:select path="selection"> 
    <form:options items="${coins}" itemValue="shortName" itemLabel="fullName" /> 
</form:select>