Struts 2 com ajax

2 respostas
A

Olá pessoal!

Estou com problemas para implemetar ajax (dojo) na minha aplicação struts 2.
Será que alguém poderia me informar um exemplo prático. Assim. Eu tenho um form com uma displaytag que mostra o resultado de uma pesquisa. Gostaria de clicar sobre o registro e logo abaixo mostrar um outro form com os campos de edição. Abaixo está minha página:

<%@include file="/templates/header.jsp" %>

    <s:form id="formPesquisar" theme="simple" >
        <s:token/>
        <h3 align="left" >Pesquisa de Estabelecimento</h3>
        <table border="0" cellpadding="0" cellspacing="0" align="left" >
            <tr align="left">
                <td align="left"><s:label value="Nome" theme="simple" /></td>
            </tr>
            <tr align="left">
                <td>
                    <s:textfield value='%{entitySearch.nome}'
                                 name="entitySearch.nome"
                                 theme="simple"
                                 size="75"
                                 maxlength="100" />
                </td>
            </tr>
            <tr align="left">
                <td>
                    <s:submit value="Pesquisar" action="pesquisar" theme="simple"/>
                    <s:reset value="Limpar" theme="simple" />
                    <s:submit value="Novo" action="incluirnovo" theme="simple" />
                    <s:submit value="Excluir" onclick="return confirmacao('Deseja excluir os registros?');"
                              action="excluir" theme="simple" />
                </td>
            </tr>

            <tr>
                <td>
                    <display:table id="dataTableResultado" name="entityList"
                                   pagesize="2" defaultsort="2" defaultorder="ascending"
                                   class="its" requestURI="retorarEntityList" excludedParams="*" >

                       <display:setProperty name="basic.show.header" value="true">

                              <display:setProperty name="basic.empty.showtable" value="true"/>
                              <display:setProperty name="sort.amount" value="list"/>
                              <display:setProperty name="paging.banner.placement" value="bottom"/>
                              <display:setProperty name="paging.banner.item_name">registro</display:setProperty>
                              <display:setProperty name="paging.banner.items_name">registros</display:setProperty>
                              <display:setProperty name="paging.banner.no_items_found"> {0} não encontrados.</display:setProperty>
                              <display:setProperty name="paging.banner.one_item_found">Um {0} encontrado.</display:setProperty>
                              <display:setProperty name="paging.banner.all_items_found">Encontrados: {0} {1}, Exibindo todos.</display:setProperty>
                              <display:setProperty name="paging.banner.some_items_found">Encontrados: {0} {1}, Exibindo de {2} a {3}.</display:setProperty>
                              <display:setProperty name="paging.banner.group_size" value="8"/>
                              <display:setProperty name="paging.banner.full">
                                 <br>[<a href="{1}">Primeira</a>/<a href="{2}">Anterior</a>] {0}
                                 [<a href="{3}">Próxima</a>/<a href="{4}">Última</a>]
                              </display:setProperty>
                              <display:setProperty name="paging.banner.first">
                                 <br>[Primeira/Anterior] {0}
                                 [<a href="{3}">Próxima</a>/<a href="{4}">Última</a>]
                              </display:setProperty>
                              <display:setProperty name="paging.banner.last">
                                 <br>[<a href="{1}">Primeira</a>/<a href="{2}">Anterior</a>] {0}
                                 [Próxima/Última]
                              </display:setProperty>
                              <display:setProperty name="paging.banner.onepage">{0}</display:setProperty>
                              <display:setProperty name="paging.banner.page.selected" value="<b>{0}</b>"/>

                        </display:setProperty>

                        <display:column title="" style="width:3%;">
                            <s:checkbox name="selecionados" theme="simple" fieldValue="%{#attr.dataTableResultado.id}"></s:checkbox>
                        </display:column>
                        <display:column title="Nome" property="nome" sortProperty="nome"
                                        sortable="true" href="editar.action" paramId="id" paramProperty="id" />
                        <display:column title="Nome Fantasia" property="nomeFantasia" sortProperty="nomeFantasia"
                                        sortable="true" href="editar.action" paramId="id" paramProperty="id" />

                    </display:table>
                </td>
            </tr>
        </table>
    </s:form>

    <s:if test="#session.isEditando == true">
        <s:form id="formEditar" theme="simple" >
                <h3 align="left" >Edição de Estabelecimento</h3>
                <table border="0" cellpadding="0" cellspacing="0" align="left" >
                    <tr align="left">
                        <td><s:label value="Nome" theme="simple" /></td>
                        <td colspan="2"><s:label value="Nome fantasia" theme="simple" /></td>
                    </tr>
                    <tr align="left">
                        <td>
                            <s:textfield id="txtNome"
                                         maxlength="100"
                                         required="true"
                                         value="%{entity.nome}"
                                         requiredposition="left"
                                         name="entity.nome"
                                         theme="simple"
                                         size="75"/>
                        </td>
                        <td colspan="2">
                            <s:textfield maxlength="100"
                                         value="%{entity.nomeFantasia}"
                                         requiredposition="left"
                                         name="entity.nomeFantasia"
                                         theme="simple"
                                         size="75"/>
                        </td>
                    </tr>

                    <tr align="left">
                        <td><s:label value="Logradouro" theme="simple" /></td>
                        <td colspan="2"><s:label value="Bairro" theme="simple" /></td>
                    </tr>
                    <tr align="left">
                        <td>
                            <s:textfield maxlength="100"
                                         value="%{entity.logradouro}"
                                         requiredposition="left"
                                         name="entity.logradouro"
                                         theme="simple"
                                         size="75"/>
                        </td>
                        <td colspan="2">
                            <s:textfield maxlength="60"
                                         value="%{entity.bairro}"
                                         requiredposition="left"
                                         name="entity.bairro"
                                         theme="simple"
                                         size="75"/>
                        </td>
                    </tr>

                    <tr align="left">
                        <td><s:label value="Cidade" theme="simple" /></td>
                        <td><s:label value="UF" theme="simple" /></td>
                        <td><s:label value="CEP" theme="simple" /></td>
                    </tr>
                    <tr align="left">
                        <td>
                            <s:textfield maxlength="60"
                                         value="%{entity.cidade}"
                                         requiredposition="left"
                                         name="entity.cidade"
                                         theme="simple"
                                         size="75"/>
                        </td>
                        <td>
                            <s:textfield maxlength="2"
                                         value="%{entity.uf}"
                                         requiredposition="left"
                                         name="entity.uf"
                                         theme="simple"
                                         size="5"/>
                        </td>
                        <td>
                            <s:textfield maxlength="8"
                                         value="%{entity.cep}"
                                         requiredposition="left"
                                         name="entity.cep"
                                         theme="simple"
                                         size="10"/>
                        </td>
                    </tr>

                    <tr align="left">
                        <td><s:label value="Site para inscrição" theme="simple" /></td>
                        <td><s:label value="Email de contato" theme="simple" /></td>
                        <td><s:label value="Telefone de contato" theme="simple" /></td>
                    </tr>
                    <tr align="left">
                        <td>
                            <s:textfield maxlength="60"
                                         value="%{entity.urlSite}"
                                         requiredposition="left"
                                         name="entity.urlSite"
                                         theme="simple"
                                         size="75"/>
                        </td>
                        <td>
                            <s:textfield maxlength="100"
                                         value="%{entity.email}"
                                         requiredposition="left"
                                         name="entity.email"
                                         theme="simple"
                                         size="44"/>
                        </td>
                        <td>
                            <s:textfield maxlength="20"
                                         value="%{entity.telefone}"
                                         requiredposition="left"
                                         name="entity.telefone"
                                         theme="simple"
                                         size="20"/>
                        </td>
                    </tr>

                    <tr align="left">
                        <td colspan="3">
                            <s:submit value="Salvar" action="gravar" align="left" theme="simple" />
                            <s:submit value="Cancelar" action="cancelar" align="left" theme="simple" />
                        </td>
                    </tr>
                </table>
            </s:form>
    </s:if>

<%@include file="/templates/footer.jsp" %>

2 Respostas

diogoprosoft

Bom implementar ajax com o dojo é tranquilo o problema e quando se trata de implementar struts com o dojo + ajax, mais blz faz assim
vc nao podera usar submit tudo tem que ser em ajax entao para popular o grid vc tera que usar um cara chamado ajax:displaytag

http://ajaxtags.sourceforge.net/usage.html

mais derepente vc tara que usar um outro grid tipo os que contem no dojo mesmo ou um Flexigrid

espero ter ajudado :slight_smile:

A

É! Parece-me uma solução mais adequada pelo que pesquisei na internet, mas sabe quando agente “encasqueta” com uma coisa, rsrsrs. Só queria fazer um exemplo, mesmo que simples. Criei uma action simples, que retorna uma string que leio através do sessioMap e mostro o resultado em uma DIV, mas o botão não dá submit, veja:

ACTION

public abstract class extends ActionSupport
                              implements Serializable,
                                                ModelDriven,
                                                Preparable,
                                                SessionAware,
                                                ServletRequestAware,
                                                ServletResponseAware,
                                                ServletContextAware{
    private String frase;

    ...

    public String getFrase(){
        return this.frase;
    }

    public setFrase(String frase){
        this.frase = frase;
    }

    public String teste(){
        frase = "Teste com Ajax!"
        sessionMap.put("frase", frase);
        return SUCCESS;
    }

}

Trecho da página:

<s:form id="formPesquisar" theme="simple" >
...

    </s:form>

    <s:url id="url" action="teste" />
    <s:url id="url2" action="teste2" />

    <sx:div id="div1" >
        <s:label value='%{frase}' />
    </sx:div>

    <sx:submit value="Submit" id="submit" formId="formPesquisar" href="%{#url}" targets="div1" />
    <sx:submit value="Limpar" id="submit2" formId="formPesquisar" href="%{#url2}" targets="div1" />
Criado 8 de março de 2009
Ultima resposta 9 de mar. de 2009
Respostas 2
Participantes 2