Pessoal,
Estou com um problema aqui, fui fazer um formulário e um action do Struts para alterar os dados dos usuarios, os dados dos usuarios que podem ser alterados sao senha e nome (login e tipo nao podem), mas asim carrego todos esses dados para dar updates no BD, o que acontece é que se colocosession.merge
session.update
alterarUsuario.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><bean:message key="catalogo.titulo"/></title>
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/catalogo.css" type="text/css" />
</head>
<body>
<div id="topo"></div>
<jsp:include page="../uteis/menu.jsp"></jsp:include>
<div id="conteudo">
<br /><br />
<form action="${pageContext.request.contextPath}/alterarUsuario.do" method="post" name="AlterarUsuarioForm">
<h2>Alterar Dados de Usuário</h2>
<table border="0">
<tr>
<td width="20"> </td>
<td width="120"><label>Nome:</label></td>
<td width="380"><input type="text" name="nome" size="60" maxlength="60" value="${usuario.nome}" /></td>
</tr>
<tr>
<td> </td>
<td><label>Login:</label></td>
<td><input type="text" name="login" size="20" maxlength="20" readonly="true" value="${usuario.login}" />
<span id="fonte10">Login não pode ser alterado.</span></td>
</tr>
<tr>
<td> </td>
<td><label>Nova Senha:</label></td>
<td><input type="password" name="senha" size="21" maxlength="20" />
<span id="fonte10">A senha deve ser diferente de 1234.</span></td>
</tr>
<tr>
<td> </td>
<td><label>Confirme a senha:</label></td>
<td><input type="password" name="senha2" size="21" maxlength="20" /></td>
</tr>
</table>
<input type="hidden" name="tipo" value="1" />
<br />
<div id="botoes">
<html:submit><bean:message key="catalogo.botao.alterar"/></html:submit>
<input type="button" value="Cancelar" onclick="javascript:window.location='${pageContext.request.contextPath}/'" />
</div>
</form>
</div>
</body>
</html>
package catalogo.controle.forms;
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 AlterarUsuarioForm extends org.apache.struts.action.ActionForm {
private String login, nome, senha;
private int tipo;
public AlterarUsuarioForm() {
super();
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getSenha() {
return senha;
}
public void setSenha(String senha) {
this.senha = senha;
}
public int getTipo() {
return tipo;
}
public void setTipo(int tipo) {
this.tipo = tipo;
}
/**
* This is the action called from the Struts framework.
* @param mapping The ActionMapping used to select this instance.
* @param request The HTTP Request we are processing.
* @return
*/
/*public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (getName() == null || getName().length() < 1) {
errors.add("name", new ActionMessage("error.name.required"));
// TODO: add 'error.name.required' key to your resources
}
return errors;
}*/
}
package catalogo.controle.actions;
import catalogo.controle.forms.AlterarUsuarioForm;
import catalogo.controle.forms.CadastroUsuarioForm;
import catalogo.dao.GenericDAO;
import catalogo.modelo.Usuario;
import catalogo.util.HibernateUtil;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class AlterarUsuarioAction extends org.apache.struts.action.Action {
private String retorno;
/**
* This is the action called from the Struts framework.
* @param mapping The ActionMapping used to select this instance.
* @param form The optional ActionForm bean for this request.
* @param request The HTTP Request we are processing.
* @param response The HTTP Response we are processing.
* @throws java.lang.Exception
* @return
*/
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
AlterarUsuarioForm usuarioForm = (AlterarUsuarioForm) form;
Usuario usuario = new Usuario();
BeanUtils.copyProperties(usuario, usuarioForm);
request.setAttribute("usuario", usuario);
Session sessao = HibernateUtil.getSession();
Transaction transacao = sessao.beginTransaction();
GenericDAO<Usuario> dao = new GenericDAO(sessao, Usuario.class);
//UsuarioDAO usuarioDao = new UsuarioDAO(sessao, Usuario.class);
try
{
dao.Atualizar(usuario);
transacao.commit();
sessao.flush();
retorno = "usuarioAlterado";
}
catch (Exception e) {
System.out.println(e.getMessage());
transacao.rollback();
retorno = "erroAlterarUsuario";
}
finally {
sessao.close();
}
return mapping.findForward(retorno);
}
}
Pelo que parece o xml do struts esta certo, pois faz o direcionamento para a pagina de erro.
Se precisarem de mais alguma coisa, é só falar
Desde ja agradeço