<html:select> Dinâmico!

Boa tarde pessoal,

Bom, tenho uma tela de pesquisa onde tenho três campos básicos (login):

1. <html:select property="idunidade" size="1">
2. <html:text property="usuario" maxlength="25" size="36" onfocus=""/>
3. <html:password property="senha" maxlength="25" size="36"/>

O problema consiste em que ao abrir esta tela de login, no campo select, devera ser “populado” automaticamente por um “bean” gravado na sessão.

Imagino que “ao carregado do <body>” ele fosse até o meu ActionForm e fizesse a busca, gravasse o objeto na sessão e só que não sei como pegar esse objeto “bean popupado” e popular o meu select!!!

package com.webeduk.action;

import com.webeduk.actionform.LoginActionForm;
import com.webeduk.classesbeans.Unidade;
import com.webeduk.classesbeans.Usuario;
import com.webeduk.classesdatabase.UnidadeDBAccess;
import com.webeduk.classesdatabase.UsuarioDBAccess;
import com.webeduk.database.DBAccess;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.LookupDispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import java.util.*;


public class LoginAction extends LookupDispatchAction &#123;
    
    /* forward name=&quot;success&quot; path=&quot;&quot; */
    private final static String SUCCESS = &quot;principal&quot;;
    private final static String ERROR   = &quot;loginfailed&quot;;
    
    protected Map getKeyMethodMap&#40;&#41; &#123;
        Map map = new HashMap&#40;&#41;;
        map.put&#40;&quot;button.consultar&quot;, &quot;add&quot;&#41;;
        map.put&#40;&quot;button.edit&quot;, &quot;edit&quot;&#41;;
        map.put&#40;&quot;button.delete&quot;, &quot;delete&quot;&#41;;
        return map;
    &#125;
    
    /** Action called on Add button click
     */
    
    public ActionForward buildComboUnidades&#40;ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response&#41; throws java.lang.Exception 
    &#123;
        try
        &#123;
            List unidade = UnidadeDBAccess.loadUnidades&#40;&#41;;
            request.getSession&#40;&#41;.setAttribute&#40;&quot;unidade&quot;,unidade&#41;;
        &#125;
        catch &#40;NullPointerException e&#41;
        &#123;
            return new ActionForward&#40;mapping.getInput&#40;&#41;&#41;;
        &#125;
        return new ActionForward&#40;mapping.getInput&#40;&#41;&#41;;
    &#125;

    /** Action called on Edit button click
     */
    public ActionForward edit&#40;ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response&#41; &#123;
        // TODO&#58; implement edit method
        return mapping.findForward&#40;SUCCESS&#41;;
    &#125;
    
    /** Action called on Delete button click
     */
    public ActionForward delete&#40;ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response&#41; throws java.lang.Exception &#123;
        // TODO&#58;implement delete method
        return mapping.findForward&#40;SUCCESS&#41;;
    &#125;
    
&#125;
package com.webeduk.actionform;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;


public class LoginActionForm extends org.apache.struts.action.ActionForm &#123;
    
    private String usuario;
    private String senha;
    private int    idunidade;
    private String blender;
    /*
    */
    public void setUsuario&#40;String usuario&#41;
    &#123;
        this.usuario = usuario;
    &#125;
    /*
    */
    public String getUsuario&#40;&#41;
    &#123;
        return this.usuario;
    &#125;
    /*
    */
    public void setSenha&#40;String senha&#41;
    &#123;
        this.senha = senha;
    &#125;
    /*
    */
    public String getSenha&#40;&#41;
    &#123;
        return this.senha;
    &#125;
    /*
    */
    public void setIdunidade&#40;int idunidade&#41;
    &#123;
        this.idunidade = idunidade;
    &#125;
    /*
    */
    public int getIdunidade&#40;&#41;
    &#123;
        return this.idunidade;
    &#125;
    /*
    */
    public void setBlender&#40;String blender&#41;
    &#123;
        this.blender = blender;
    &#125;
    /*
    */
    public String getBlender&#40;&#41;
    &#123;
        return this.blender;
    &#125;
    /*
    */
    public LoginActionForm&#40;&#41; 
    &#123;
        super&#40;&#41;;
    &#125;
    /*
    */
    public ActionErrors validate&#40;ActionMapping mapping, HttpServletRequest request&#41; 
    &#123;
        ActionErrors errors = new ActionErrors&#40;&#41;;
        /*
        */
        // aqui tava o questionamento da unidade
        /*
        */
        if &#40;getBlender&#40;&#41;.equals&#40;&quot;logar&quot;&#41;&#41;
        &#123;
            request.getSession&#40;&#41;.setAttribute&#40;&quot;consultarUsuario&quot;,&quot;true&quot;&#41;;
        &#125;
        /*
        */
        if &#40;&#40;getUsuario&#40;&#41;.equals&#40;&quot;&quot;&#41;&#41; || &#40;getSenha&#40;&#41;.equals&#40;&quot;&quot;&#41;&#41; || &#40;getIdunidade&#40;&#41;&lt;1&#41;&#41;
        &#123;
            errors.add&#40;&quot;img&quot;,       new ActionMessage&#40;&quot;errorsimgfailed&quot;&#41;&#41;;
        &#125;
        /*
        */
        if &#40;getUsuario&#40;&#41;.equals&#40;&quot;&quot;&#41;&#41;
        &#123;
            errors.add&#40;&quot;usuario&quot;,   new ActionMessage&#40;&quot;errors.login.usuario&quot;&#41;&#41;;
        &#125;
        /*
        */
        if &#40;getSenha&#40;&#41;.equals&#40;&quot;&quot;&#41;&#41;
        &#123;
            errors.add&#40;&quot;senha&quot;, new ActionMessage&#40;&quot;errors.login.senha&quot;&#41;&#41;;
        &#125;
        /*
         */
        if &#40;getIdunidade&#40;&#41;&lt;1&#41;
        &#123;
            errors.add&#40;&quot;idunidade&quot;, new ActionMessage&#40;&quot;errors.login.unidade&quot;&#41;&#41;;
        &#125;
        /*
        */
        return errors;
    &#125;
    /*
    */
    public void reset&#40;ActionMapping actionMapping, HttpServletRequest httpServletRequest&#41; 
    &#123;
        this.usuario   = null;
        this.senha     = null;
        this.idunidade = 0;
        this.blender   = null;
    &#125; 
&#125;

Opa,

você pode usar o código asseguir:

&lt;html&#58;select property=&quot;idUnidade&quot;&gt;&lt;html&#58;options collection=&quot;listaUnidade&quot; property=&quot;value&quot; labelProperty=&quot;label&quot;/&gt;&lt;/html&#58;select&gt;

Neste caso você setaria no request um Arraylist, Collection ou algo que seja “iterável” de objetos que tenha as propriedades value e label, você pode usar neste caso a classe LabelValueBean.

:okok:

Bom, valeu pela dica!!! :grin:

Mas a dificuldade de entender a lógica de ação do Struts ainda esta me dando dores de cabeça.

A indagação é, como posso fazer com que ele leia o método buildComboUnidades da Classe “LoginAction” para que ele construa o “bean” da sessão com as unidades, mostrando isso no “select”, sem dar o seguinte erro:

Lembrando que essa “JSP” é a página de login!

agradeço quem puder me ajudar! :sad:

[quote=“marcossousa”]Opa,

você pode usar o código asseguir:

&lt;html&#58;select property=&quot;idUnidade&quot;&gt;&lt;html&#58;options collection=&quot;listaUnidade&quot; property=&quot;value&quot; labelProperty=&quot;label&quot;/&gt;&lt;/html&#58;select&gt;

Neste caso você setaria no request um Arraylist, Collection ou algo que seja “iterável” de objetos que tenha as propriedades value e label, você pode usar neste caso a classe LabelValueBean.

:okok:[/quote]

Bom dia, usei a dica do Marcos, só que a minha Collection não é de objetos que possuem atributos e métodos; mas objetos do tipo String. Como faço para “iterá-los”?

Ex: Collection p = new ArrayList();
p.add(“a”);
p.add(“b”);
p.add(“c”);

<html:select property=“abc”><html:options collection=“p” property="?" labelProperty="?"/></html:select>

O que coloco como property e labelProperty?

Antônio Matheus.