edu_merckx 9 de jul. de 2010
como você está recuperandos estes dados no altera-aluno.jsp?
já tentou usar Servlet? por ex.: href=“alterarAluno?alunoId=${aluno.id}”
tiagofla 9 de jul. de 2010
No altera-aluno.jsp, estou fazendo desta forma:
<tr>
<td> Nome :</td>
<td><input type= "text" name= "nome" value= " ${ aluno . nome } "/ ></td>
</tr>
edu_merckx 9 de jul. de 2010
usa servlet para capturar esses dados enviados via get e redireciona para o jsp…
public class AlterarAlunoServlet extends HttpServlet {
@Override
protected void service ( HttpServletRequest req , HttpServletResponse res )
throws ServletException , IOException {
Aluno aluno = new Aluno ();
aluno . setId ( Long . parseLong ( req . getParameter ( "id" )));
aluno . setNome ( req . getParameter ( "nome" ));
...
req . setAttribute ( "aluno" , aluno );
RequestDispatcher rd = req . getRequestDispatcher ( "/altera-aluno.jsp" );
rd . forward ( req , res );
}
}
aí no link que tem na listagem coloca assim: <a href=“alterarAluno?id=${aluno.id}&nome=${aluno.nome}”…>Alterar
espero ter ajudado…
até mais…
tiagofla 9 de jul. de 2010
aí no link que tem na listagem coloca assim: <a href= "alterarAluno?id= ${ aluno . id } &nome= ${ aluno . nome } ".. > Alterar</a>
Esse “alterarAluno” logo no início do a href= é o mapeamento da url-pattern da web.xml???
edu_merckx 9 de jul. de 2010
isso… coloquei assim para simplificar…
tiagofla 9 de jul. de 2010
fiz dessa forma mas não está dando certo. Vou mostrar como está o form altera-aluno.jsp pra ver se estou fazendo da forma correta:
<center><form action= "alterarAluno" method= "post" >
<table bgcolor= "grey" width= "300" border= "0" >
<tr>
<td> Id :</td>
<td><input type= "text" name= "aluno.id" value= " ${ aluno . id } " disabled= "disabled"/ ></td>
</tr>
<tr>
<td> Nome :</td>
<td><input type= "text" name= "aluno.nome" value= " ${ aluno . nome } "/ ></td>
</tr>
<tr>
<td> CPF :</td>
<td><input type= "text" name= "aluno.cpf" value= " ${ aluno . cpf } "/ ></td>
</tr>
<tr>
<td> Telefone :</td>
<td><input type= "text" name= "aluno.telefone" value= " ${ aluno . telefone } "/ ></td>
</tr>
<tr>
<td> Endereço :</td>
<td><input type= "text" name= "aluno.endereco" value= " ${ aluno . endereco } "/ ></td>
</tr>
<tr>
<td> Bairro :</td>
<td><input type= "text" name= "aluno.bairro" value= " ${ aluno . bairro } "/ ></td>
</tr>
<tr>
<td> Cidade :</td>
<td><input type= "text" name= "aluno.cidade" value= " ${ aluno . cidade } "/ ></td>
</tr>
<tr>
<td> UF :</td>
<td><select name= "aluno.uf" >
<option value= " ${ aluno . uf } "/ > RJ
<option value= " ${ aluno . uf } "/ > SP
</select>
</td>
</tr>
</table><br/><br/>
<input type= "hidden" name= "altera" value= "AlteraAlunoServlet" >
<input type= "submit" name= "alterarAluno" value= "Alterar"/ >
</form></center>
No link fiz dessa forma:
<td><a href= "altera-aluno.jsp?aluno.id= ${ aluno . id } &aluno.nome= ${ aluno . nome } &aluno.cpf= ${ aluno . cpf } &aluno.telefone= ${ aluno . telefone } &aluno.endereco= ${ aluno . endereco } &aluno.bairro= ${ aluno . bairro } &aluno.cidade= ${ aluno . cidade } &aluno.uf= ${ aluno . uf } " > Alterar</a></td>
O link também coloquei assim e não deu certo:
<td><a href= "alterarAluno?aluno.id= ${ aluno . id } &aluno.nome= ${ aluno . nome } &aluno.cpf= ${ aluno . cpf } &aluno.telefone= ${ aluno . telefone } &aluno.endereco= ${ aluno . endereco } &aluno.bairro= ${ aluno . bairro } &aluno.cidade= ${ aluno . cidade } &aluno.uf= ${ aluno . uf } " > Alterar</a></td>
edu_merckx 9 de jul. de 2010
você verificou se os valores estão passando realmente?
estes dados estão em um banco de dados? se sim, seria interessante passar somente o id e recuperar o objeto no servlet…
tiagofla 10 de jul. de 2010
Pensei da seguinte maneira: Criei 2 novos métodos no meu AlunoDAO. Um para popular os campos do form (altera-aluno.jsp) e outro para fazer a busca do aluno por id. Os métodos são:
public Aluno buscaPorId(Long id){
try{
PreparedStatement stmt = this.connection.prepareStatement("select * from tb_aluno where id=?");
stmt.setLong(1, id);
ResultSet rs = stmt.executeQuery();
if(rs.next()){
return populaAluno(rs);
}
rs.close();
stmt.close();
return null;
}catch(SQLException e){
throw new RuntimeException(e);
}
}
private Aluno populaAluno(ResultSet rs) throws SQLException{
Aluno aluno = new Aluno();
aluno.setId(rs.getLong("id"));
aluno.setNome(rs.getString("nome"));
aluno.setCpf(rs.getString("cpf"));
aluno.setTelefone(rs.getString("telefone"));
aluno.setEndereco(rs.getString("endereco"));
aluno.setBairro(rs.getString("bairro"));
aluno.setCidade(rs.getString("cidade"));
aluno.setUf(rs.getString("uf"));
return aluno;
}
Esse é o servlet de alteração:
public class AlteraAlunoServlet extends HttpServlet {
protected void service ( HttpServletRequest req , HttpServletResponse res ) throws ServletException , IOException {
long id = Long . parseLong ( req . getParameter ( "id" ));
String nome = req . getParameter ( "nome" );
String cpf = req . getParameter ( "cpf" );
String telefone = req . getParameter ( "telefone" );
String endereco = req . getParameter ( "endereco" );
String bairro = req . getParameter ( "bairro" );
String cidade = req . getParameter ( "cidade" );
String uf = req . getParameter ( "uf" );
Aluno aluno = new Aluno ();
aluno . setId ( id );
aluno . setNome ( nome );
aluno . setCpf ( cpf );
aluno . setTelefone ( telefone );
aluno . setEndereco ( endereco );
aluno . setBairro ( bairro );
aluno . setCidade ( cidade );
aluno . setUf ( uf );
// req.getAttribute("id");
// req.setAttribute("aluno", aluno);
AlunoDAO dao = new AlunoDAO ();
// dao.buscaPorId(id);
dao . altera ( aluno );
res . sendRedirect ( "altera-aluno.jsp" );
}
}
Só que ainda tem um porém, estou na dúvida de como vou chamar o método buscarPorId() dentro do meu servlet de alteração ("AlteraAlunoServlet")...
Alguém tem uma sugestão??
edu_merckx 10 de jul. de 2010
para chamar o método buscaPorId() você pode fazer assim:
AlterarAlunoServlet.java
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
Long id = Long.parseLong(res.getParameter("id"));
AlunoDAO dao = new AlunoDAO();
req.setAttribute("aluno", dao.buscaPorId(id));
RequestDispatcher rd = req.getRequestDispatcher("/altera-usuario.jsp");
rd.forward(req, res);
}
tiagofla 10 de jul. de 2010
Fiz dessa maneira mas não deu certo. Será que o problema está no form altera-aluno.jsp???
Exemplo, será que a chamada do action está da maneira correta??
<form action="alterarAluno" method="post"> ** O alterarAluno é o mapeamento da url-pattern no web.xml.
E será que é necessario trabalhar com a expression language no value dos campos do form??
<input type= "text" name= "id" value= " ${ aluno . id } " disabled= "disabled"/ >
Parece simples mas estou pecando em algo…
edu_merckx 10 de jul. de 2010
tiagofla, se você está usando link em uma listagem, o action do form não interfere…
sobre usar EL no value, isso é necessário…
qualquer coisa me manda seu msn ou skype por mp…
até mais…