Não consigo usar optionsCollection

3 respostas
A

Olá, pessoal :!:

Bom, estou iniciando o estudo do framework struts, e estou precisando da ajuda dos + experientes.

O problema ocorre quando tento usar <html:optionsCollection>. A página simplismente não "carrega", possívelmente por causa de algum erro de sintaxe, sei lá.

Já fiz várias tentativas, mas nada... :cry:

Vou listar partes do código pra vcs:
1) Classe JavaBean que possui o "label" e o "value" a ser mostrado na tag option

package strutsdemo.model;

    import java.io.Serializable;

    public class FaixasIdade implements Serializable {
                    final int value;
                    final String label;

                   public FaixasIdade( int value, String label ){
                       this.value = value;
                       this.label = label;
                   }

                  public int getValue( ){
                       return value;
                   }

                  public String getLabel( ){
                       return label;
                   }
    }
2) Método "editar", da classe que estende DispatchAction
public ActionForward editar(ActionMapping mapping,  ActionForm form, 
                                                    HttpServletRequest request, HttpServletResponse response) throws Exception {
                 ActionMessages errors = new ActionMessages( );
            
                  try{            
                      ...
                      
                     request.setAttribute( &quot;usuario&quot;, user);

                     faixas = new LinkedList( );
                     String[] titulos = {&quot;até 20&quot;, &quot;de 20 a 30&quot;, &quot;de 30 a 40&quot;, &quot;de 40 a 50&quot;, &quot;acima de 50&quot;};
           
                     for( int i =0; i &lt; titulos.length; i++ )
                          faixas.add( new FaixasIdade( ( i + 1 ), titulos[ i ] ));

                     request.setAttribute( &quot;faixas&quot;, faixas );
                      
                       ...
            }

3) Pedaços da página form.jsp

Faixas: 
    [u]Pedaço 1[/u]
 &lt;%
            java.util.Iterator it = ( ( java.util.List ) request.getAttribute("faixas") ).iterator( );
            
                    while( it.hasNext( )){
                        strutsdemo.model.FaixasIdade faixas = ( strutsdemo.model.FaixasIdade ) it.next( );
                        out.println( "&lt;br&gt; Label: " + faixas.getLabel( ) + " e Value: " +  faixas.getValue( ));
                    }                               
 %&gt;   
   
      ...

[u]Pedaço 2[/u]

   &lt;c:forEach var="fx" items="${faixas}"&gt;
       Label: ${fx.label} e Value: ${fx.value}
    &lt;/c:forEach&gt;

    ...

[u]Pedaço 3[/u]

    &lt;td&gt;
          &lt;html:select name="usuario"  property="faixaIdade"&gt;
                  &lt;html:optionsCollection property="faixas" label="label" value="value"/&gt;                  
           &lt;/html:select&gt;
    &lt;/td&gt;
Sendo que as partes abaixo funcionam normalmente ...
&lt;%
            java.util.Iterator it = ( ( java.util.List ) request.getAttribute("faixas") ).iterator( );
            
                    while( it.hasNext( )){
                        strutsdemo.model.FaixasIdade faixas = ( strutsdemo.model.FaixasIdade ) it.next( );
                        out.println( "&lt;br&gt; Label: " + faixas.getLabel( ) + " e Value: " +  faixas.getValue( ));
                    }                               
 %&gt;   
   

   &lt;c:forEach var="fx" items="${faixas}"&gt;
       Label: ${fx.label} e Value: ${fx.value}
    &lt;/c:forEach&gt;

... porém, ao acrescentar optionsCollection ...

&lt;td&gt;
          &lt;html:select name="usuario"  property="faixaIdade"&gt;
                  &lt;html:optionsCollection property="faixas" label="label" value="value"/&gt;                  
           &lt;/html:select&gt;
    &lt;/td&gt;

... tudo desanda :roll:

O que está errado com a declaração desta tag?

Aguardo a ajuda, galera!

[]'s

3 Respostas

A

Será que alguém pode me dar uma dica?

lgmhorus

Tenta:

<td>  
       <html:select name="usuario"  property="faixaIdade">  
               <html:optionsCollection [b]name[/b]="faixas" label="label" value="value"/>                    
        </html:select>  
 </td>

property é quando é um atributo de um dado objeto (como é o caso de label e value aqui). Se vc coloca property sem value, ele assume que é do form. Quando é apenas um objeto na sessão ou no request, você usa name.

A

:lol: Agora foi! Valeu! :thumbup:

Criado 7 de julho de 2008
Ultima resposta 8 de jul. de 2008
Respostas 3
Participantes 2