You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Márcio Gurgel <ma...@gmail.com> on 2008/04/22 02:47:58 UTC

[RESOLVED] Help linking two autocompleters

Hi all,

I resolved my problem using JSON.
Some links:
http://struts.apache.org/2.x/docs/ajax-and-javascript-recipes.html#AjaxandJavaScriptRecipes-Exampleaction
http://cwiki.apache.org/S2PLUGINS/json-plugin.html

Here's the code (Maybe it could help someone)
Dont forget to include json.jar in your classpath

struts.xml ---------------------------------------
<package name="ajax" extends="json-default">

        <action name="listarCidades"
class="br.com.sgvdba.actions.ajax.CidadesListAction">
            <result type="json" />
        </action>

    </package>

jsp ----------------------------------------------------
<s:action var="actListarUfs" name="listarUfs" namespace="/cliente" />
<s:action var="actListarCidades" name="listarCidades" namespace="/cliente"
/>
<s:url id="urlListCli" action="listarCidades"/>

        <sx:autocompleter list="#actListarUfs.ufs" listKey="cd"
listValue="sigla" label="UF"
            name="siglaUf" id="siglaUf" required="true"
cssClass="autoCompleter" forceValidOption="true"
valueNotifyTopics="/listCidades"/>

        <sx:autocompleter label="Cidade" id="cidades" name="cidades"
            list="cidades" listKey="cd" listValue="nome"
            href="%{urlListCli}" autoComplete="true" formId="formCli"
            listenTopics="/listCidades" forceValidOption="true"
preload="false" cssClass="autoCompleter"/>

action ------------------------------------------------------------
(note that the method's name is: get + the name attribute of your
autocompleter)
public class CidadesListAction extends ActionSupport{

    private static final long serialVersionUID = -5821147861362223337L;
    private String siglaUf;
    private CidadeFacade cidadeFacade;

   public final Map<String, String> getCidades(){

           Map<String, String> cidadesMap = new HashMap<String, String>();
        List<Cidade> cidadesList =
getCidadeFacade().recuperarPorUf(siglaUf);

        for (Iterator<Cidade> it = cidadesList.iterator(); it.hasNext();){
            Cidade cidade = (Cidade) it.next();
            cidadesMap.put(cidade.getNome(), cidade.getCd().toString());
        }
        return cidadesMap;
   }

    /**
     * GETTERS AND SETTERS
     */
    public final String getSiglaUf() {
        return siglaUf;
    }
    public final void setSiglaUf(String siglaUf) {
        this.siglaUf = siglaUf;
    }
    public CidadeFacade getCidadeFacade(){
        if (this.cidadeFacade == null){
            this.cidadeFacade = new CidadeFacade();
        }
        return this.cidadeFacade;
    }

}



2008/4/20, Márcio Gurgel <ma...@gmail.com>:
>
> Hi all,
>
> Here I'm with another problem..
> I know that I've been sent lots os questions, but before I aways give a
> check at documentation and showcase. I hope you can give me a hand.
> --
> I want to link two autocompleters. My action is setting the correct
> secound list. The problem is that it's never rendered on my jsp.
>
> The objective is to list all cities from a state.
>
> Here's my code:
>
> JSP -----------------------------------
> <s:url id="urlListCli" action="listarCidades" namespace="/cliente"/>
> <s:action var="actListarUfs" name="listarUfs" namespace="/cliente" />
>
>         <sx:autocompleter list="#actListarUfs.ufs" listKey="cd"
> listValue="sigla" label="UF"
>             name="siglaUf" id="siglaUf" required="true"
> cssClass="autoCompleter" forceValidOption="true"
> valueNotifyTopics="/listCidades"/>
>
>         <sx:autocompleter label="Cidade" id="idCidade" name="idCidade"
>             list="cidades" listKey="cd" listValue="nome"
>             href="%{urlListCli}" autoComplete="true" formId="formCli"
>             listenTopics="/listCidades" forceValidOption="true"
> preload="false"/>
>
> struts.xml ------------------------------
>
>     <action name="listarCidades" method="listarCidades"
> class="br.com.sgvdba.actions.cliente.ClienteAction">
>              <result></result>
>         </action>
>
> ActionClass -------------------------
>
>     public String listarCidades(){
>         this.cidades = getCidadeFacade().recuperarPorUf(siglaUf);
>         return Action.SUCCESS;
>     }
>
>
> I also tried to follow the example in show case (linking two
> autocompleters), so, on this way I just had success returning a simple array
> of string with one dimension, and I thought "ok, I have the correct
> description of the field, what about its id?". In this way my action as
> returning a .ftl.
>
> Tanks a lot!
>