Passar valor em link utilizando Struts

8 respostas
W

Olá pessoal,

Exite alguma maneira para passar um valor em um link no Struts? Semelhante ao que faço no exemplo abaixo utilizando JSP.

<a href="edita.jsp?id=<%= contato.getIdContato() %>">Editar</a>

e receberia na outra página assim:

String idContato = request.getParameter("id")

Se ajudar a página que quero colocar o Link é esta abaixo:

<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<%@ page errorPage="erro.jsp" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Consulta Contato</title>
    </head>
    <body>
        <h1>Resultado da Consulta</h1>
        <table border="1">
            <thead>
                <tr>
                    <th>Editar</th>                  
                    <th>ID</th>
                    <th>Nome</th>
                    <th>Sobrenome</th>
                    <th>Empresa</th>
                    <th>Endereco</th>
                    <th>Cidade</th>
                    <th>Estado</th>
                    <th>Email</th>
                    <th>DDD</th>
                    <th>Telefone</th>
                    <th>Celular</th>
                </tr>
            </thead>           
            <logic:iterate name="contatosnome" id="contatos" >
                    <tbody>
                    <tr>
                        <td>Link Aqui!!! passando o valor de idContato</td>
                        <td><bean:write name="contatos" property="idContato"/></td>
                        <td><bean:write name="contatos" property="nomeContato"/></td>
                        <td><bean:write name="contatos" property="sobrenomeContato"/></td>
                        <td><bean:write name="contatos" property="empresaContato"/></td>
                        <td><bean:write name="contatos" property="enderecoContato"/></td>
                        <td><bean:write name="contatos" property="cidadeContato"/></td>
                        <td><bean:write name="contatos" property="estadoContato"/></td>
                        <td><bean:write name="contatos" property="emailContato"/></td>
                        <td><bean:write name="contatos" property="dddContato"/></td>
                        <td><bean:write name="contatos" property="telefoneContato"/></td>
                        <td><bean:write name="contatos" property="celularContato"/></td>
                    </tr>
            </logic:iterate>          
            </tbody>
        </table>
    </body>
</html>

Grato
Wallfox

8 Respostas

bland

Wallfox,

Você já tentou fazer como você fez em JSP? Creio que não muda muita coisa não. Tipo, ao invés de você usar as tags do JSP &lt;%= %&gt; você passa a utilizar o &lt;bean:write&gt;

Ex.:&lt; a href=&quot;edita.jsp?id=&lt; bean:write name=&quot;contatos&quot; property=&quot;nomeContato&quot; /&gt;&quot;&gt; Editar</a>
Se eu não me engano isso funcionaria perfeitamente. Caso não funcione, posta aqui o erro para tentarmos te ajudar a resolver.

Abraço!

W

Ok. Realmente assim funciona, mas e se eu tiver que colocar usando a tag do Struts?

<html:link href=   ></html:link>

Como faço?

Grato.
Wallfox

bland

Eu não me recordo como deveria ser, mas você já tentou da mesma maneira?

&lt;html:link href= "edita.jsp?id=&lt; bean:write name="contatos" property="nomeContato" /&gt; " &gt;&lt;/html:link&gt;

Abraço!

W

Já tentei sim, mas não rolou. ao colocar o <bean:write… é entendido como o dome da variável ex: id=<
Como devo fazer para entender que após o sinal = é o tag do Struts?

Grato.
Wallfox

W

Olá pessoal,

Consegui resolver. Fiz o seguinte:

<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<%@ page errorPage="erro.jsp" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Consulta Contato</title>
    </head>
    <body>
        <h1>Resultado da Consulta</h1>
        <table border="1">
            <thead>
                <tr>
                    <th>Editar</th>
                    <th>Excluir</th>
                    <th>ID</th>
                    <th>Nome</th>
                    <th>Sobrenome</th>
                    <th>Empresa</th>
                    <th>Endereco</th>
                    <th>Cidade</th>
                    <th>Estado</th>
                    <th>Email</th>
                    <th>DDD</th>
                    <th>Telefone</th>
                    <th>Celular</th>
                </tr>
            </thead>           
            <logic:iterate name="contatosnome" id="contatos" >
                <html:form action="exibeContato">
                    <tbody>
                    <tr>
                        <td><html:link page="/editarContato.do" paramId="idContato" paramName="contatos" paramProperty="idContato"> Editar</html:link></td>
                        <td> </td>
                        <td><bean:write name="contatos" property="idContato"/></td>
                        <td><bean:write name="contatos" property="nomeContato"/></td>
                        <td><bean:write name="contatos" property="sobrenomeContato"/></td>
                        <td><bean:write name="contatos" property="empresaContato"/></td>
                        <td><bean:write name="contatos" property="enderecoContato"/></td>
                        <td><bean:write name="contatos" property="cidadeContato"/></td>
                        <td><bean:write name="contatos" property="estadoContato"/></td>
                        <td><bean:write name="contatos" property="emailContato"/></td>
                        <td><bean:write name="contatos" property="dddContato"/></td>
                        <td><bean:write name="contatos" property="telefoneContato"/></td>
                        <td><bean:write name="contatos" property="celularContato"/></td>
                    </tr>
                </html:form>
            </logic:iterate>          
            </tbody>
        </table>
    </body>
</html>

No action eu faço o seguinte:

package com.myapp.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;


public class EditaContatoAction extends Action {

    private final static String SUCCESS = "editarContato";

    public ActionForward execute(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        HttpSession session = request.getSession();
        String idContato = request.getParameter("idContato");  
        Contato contato = new Contato();
        ContatoDAO contatoDAO = new ContatoDAO();
        int temp = Integer.parseInt(idContato);
        contato = contatoDAO.findContatoByIdContato(temp);
        request.setAttribute("contato", contato);
        contatoDAO.close();
        return mapping.findForward(SUCCESS);   
    }
}

Bom, esta ai, caso sirva pra alguem. o espirito é este!

Grato
Wallfox

K

Como eu faço p passar mais de um parametro…
exemplo

editarContato.do?acao=excluir&id=1
ou
editarContato.do?acao=editar&id=1

obs: ja tentei colocar o seguinte código

<html:link page="/editarContato.do" paramId="action=excluir" paramName="contato" paramProperty="id"><bean:message key="contato.selecionado"/></html:link>

só q meu link fica errado ele fica assim, tanto no IE quanto no Firefox

action%3Dexcluir=1

ele dá erro, pq nao reconhece o sinal de =

[]´s

W

kubanacan:
Como eu faço p passar mais de um parametro…
exemplo

editarContato.do?acao=excluir&id=1
ou
editarContato.do?acao=editar&id=1

obs: ja tentei colocar o seguinte código

<html:link page="/editarContato.do" paramId="action=excluir" paramName="contato" paramProperty="id"><bean:message key="contato.selecionado"/></html:link>

só q meu link fica errado ele fica assim, tanto no IE quanto no Firefox

action%3Dexcluir=1

ele dá erro, pq nao reconhece o sinal de =

[]´s

Olá amigo,
Eu fiz da seguinte maneira:

<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<%@ page errorPage="erro.jsp" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Consulta Contato</title>
    </head>
    <body>
        <h1>Resultado da Consulta</h1>
        <table border="1">
            <thead>
                <tr>
                    <th>Editar</th>
                    <th>Excluir</th>
                    <th>ID</th>
                    <th>Nome</th>
                    <th>Sobrenome</th>
                    <th>Empresa</th>
                    <th>Endereco</th>
                    <th>Cidade</th>
                    <th>Estado</th>
                    <th>Email</th>
                    <th>DDD</th>
                    <th>Telefone</th>
                    <th>Celular</th>
                </tr>
            </thead>           
            <logic:iterate name="contatos" id="contatos" >
                <tbody>
                <tr>
            
                    <td>
                        <html:form action="editarContato">
                            <html:hidden name="contatos" property="idContato"/>
                            <html:hidden name="contatos" property="nomeContato"/>
                            <html:hidden name="contatos" property="sobrenomeContato"/>
                            <html:hidden name="contatos" property="empresaContato"/>
                            <html:hidden name="contatos" property="enderecoContato"/>
                            <html:hidden name="contatos" property="cidadeContato"/>
                            <html:hidden name="contatos" property="estadoContato"/>
                            <html:hidden name="contatos" property="emailContato"/>
                            <html:hidden name="contatos" property="dddContato"/>
                            <html:hidden name="contatos" property="telefoneContato"/>
                            <html:hidden name="contatos" property="celularContato"/>
                            <html:submit value="Editar"/>
                        </html:form>
                    </td>
                    <td>
                        <html:form action="excluirContato">
                            <html:hidden name="contatos" property="idContato"/>
                            <html:submit value="Excluir"/>
                        </html:form>
                    </td>
                    <td><bean:write name="contatos" property="idContato"/></td>
                    <td><bean:write name="contatos" property="nomeContato"/></td>
                    <td><bean:write name="contatos" property="sobrenomeContato"/></td>
                    <td><bean:write name="contatos" property="empresaContato"/></td>
                    <td><bean:write name="contatos" property="enderecoContato"/></td>
                    <td><bean:write name="contatos" property="cidadeContato"/></td>
                    <td><bean:write name="contatos" property="estadoContato"/></td>
                    <td><bean:write name="contatos" property="emailContato"/></td>
                    <td><bean:write name="contatos" property="dddContato"/></td>
                    <td><bean:write name="contatos" property="telefoneContato"/></td>
                    <td><bean:write name="contatos" property="celularContato"/></td>

                </tr>
            </logic:iterate>          
            </tbody>
        </table>
    </body>
</html>

Caso vc não entenda, volte a perguntar.

Espero ter ajudado.
Wallfox

K

sakei!! vlw brother!! :lol:

Criado 31 de julho de 2006
Ultima resposta 5 de set. de 2006
Respostas 8
Participantes 3