No método validate da classe ActionForm acontece uma primeira validação verificando se os campos foram preenchidos corretamente, e ela faz isso perfeitamente, só que acontece uma coisa chata que estou procurando resolver a semanas e ainda não consegui, o método validate válida os campos mas limpa os restante do formulário.
Ex: Um formulario que contenha os campos Nome, Telefone e Email, e que estes campos são obrigatórios o preenchimento, a pessoa preenche o campo Nome e Telefone e deixa o campo Email em branco o que acontece, o usuário submete o formulário a classe action form valida os campos, apresenta a mensagem que tal campo é necessário mas limpa todos os campos do formulário.
É isso que está acontecendo.
Se algum colega ja passou por isso por favor me ajude.
posta o codigo ai amigo
Essa classe é o ActionForm
[code]package digital;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
/**
*
-
@author jader
*/
public class AdminClienteActionForm extends org.apache.struts.action.ActionForm {private String idcliente;
private String nome;
private String endereco;
private String bairro;
private String cidade;
private String cep;
private String estado;
private String telefone;
private String fax;
private String celular;
private String email;
private String usuario;
private String senha;
private String contato;
private String datacadastro;public String getIdcliente() {
return idcliente;
}public void setIdcliente(String idcliente) {
this.idcliente = idcliente;
}public String getNome() {
return nome;
}public void setNome(String nome) {
this.nome = nome;
}public String getEndereco() {
return endereco;
}public void setEndereco(String endereco) {
this.endereco = endereco;
}public String getBairro() {
return bairro;
}public void setBairro(String bairro) {
this.bairro = bairro;
}public String getCidade() {
return cidade;
}public void setCidade(String cidade) {
this.cidade = cidade;
}public String getCep() {
return cep;
}public void setCep(String cep) {
this.cep = cep;
}public String getEstado() {
return estado;
}public void setEstado(String estado) {
this.estado = estado;
}public String getTelefone() {
return telefone;
}public void setTelefone(String telefone) {
this.telefone = telefone;
}public String getFax() {
return fax;
}public void setFax(String fax) {
this.fax = fax;
}public String getCelular() {
return celular;
}public void setCelular(String celular) {
this.celular = celular;
}public String getEmail() {
return email;
}public void setEmail(String email) {
this.email = email;
}public String getUsuario() {
return usuario;
}public void setUsuario(String usuario) {
this.usuario = usuario;
}public String getSenha() {
return senha;
}public void setSenha(String senha) {
this.senha = senha;
}public String getDatacadastro() {
return datacadastro;
}public void setDatacadastro(String datacadastro) {
this.datacadastro = datacadastro;
}public String getContato() {
return contato;
}public void setContato(String contato) {
this.contato = contato;
}/**
*
*/
public AdminClienteActionForm() {
super();
// TODO Auto-generated constructor stub
}@Override
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (getNome() == null || getNome().length() < 1) {
errors.add(“nome”, new ActionMessage(“error.nome.required”));
// TODO: add ‘error.name.required’ key to your resources
}
if (getEndereco() == null || getEndereco().length() < 1) {
errors.add(“endereco”, new ActionMessage(“error.endereco.required”));
// TODO: add ‘error.name.required’ key to your resources
}
if (getBairro() == null || getBairro().length() < 1) {
errors.add(“bairro”, new ActionMessage(“error.bairro.required”));
// TODO: add ‘error.name.required’ key to your resources
}
if (getCep() == null || getCep().length() < 1) {
errors.add(“cep”, new ActionMessage(“error.cep.required”));
// TODO: add ‘error.name.required’ key to your resources
}
if (getCidade() == null || getCidade().length() < 1) {
errors.add(“cidade”, new ActionMessage(“error.cidade.required”));
// TODO: add ‘error.name.required’ key to your resources
}
if (getEstado() == null || getEstado().length() < 1) {
errors.add(“estado”, new ActionMessage(“error.estado.required”));
// TODO: add ‘error.name.required’ key to your resources
}
if (getEmail() == null || getEmail().length() < 1) {
errors.add(“email”, new ActionMessage(“error.email.required”));
// TODO: add ‘error.name.required’ key to your resources
}
if (getUsuario() == null || getUsuario().length() < 1) {
errors.add(“usuario”, new ActionMessage(“error.usuario.required”));
// TODO: add ‘error.name.required’ key to your resources
}
if (getSenha() == null || getSenha().length() < 1) {
errors.add(“senha”, new ActionMessage(“error.senha.required”));
// TODO: add ‘error.name.required’ key to your resources
}
return errors;
}
}
[/code]
Esse é o struts-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="AdminClienteActionForm" type="digital.AdminClienteActionForm"/>
<form-bean name="ProdutoCategoriaActionForm" type="digital.ProdutoCategoriaActionForm"/>
<form-bean name="ProdutoStrutsActionForm" type="digital.ProdutoStrutsActionForm"/>
<form-bean name="CategoriaForm" type="digital.CategoriaActionForm"/>
<form-bean name="LoginAdminActionForm" type="digital.LoginAdminActionForm"/>
<form-bean name="LoginActionForm" type="digital.LoginActionForm"/>
<form-bean name="ClienteActionForm" type="digital.ClienteActionForm"/>
</form-beans>
<global-exceptions>
</global-exceptions>
<global-forwards>
<forward name="altcliadmin" path="/listagemClientes.jsp" redirect="true"/>
<forward name="prodcat" path="/cadastroProdutoCategoria.jsp" redirect="true"/>
<forward name="atuProduto" path="/listagemProduto.jsp" redirect="true"/>
<forward name="CadProduto" path="/listagemProduto.jsp" redirect="true"/>
<forward name="success" path="/login.jsp" redirect="true"/>
<forward name="atuCat" path="/listagemCategoria.jsp" redirect="true"/>
<forward name="cat" path="/listagemCategoria.jsp" redirect="true"/>
<forward name="logado" path="/index.jsp" redirect="true"/>
<forward name="falha" path="/paginaErro.jsp" redirect="true"/>
<forward name="altera" path="/meusPedidos.jsp" redirect="true"/>
<forward name="admin" path="/admin.jsp" redirect="true"/>
<action forward="digital.index" path="/index"/>
<!-- <forward name="success" path="/login.do" scope="request" redirect="true"/>-->
</global-forwards>
<action-mappings>
<action input="/alteraClienteAdmin.jsp" name="AdminClienteActionForm" path="/alterarclienteadmin" scope="request" type="digital.AlterarClienteActionAdmin"/>
<action input="/cadastroProdutoCategoria.jsp" name="ProdutoCategoriaActionForm" path="/produtoCategoria" scope="request" type="digital.ProdutoCategoriaStrutsAction"/>
<action input="/alteraProduto.jsp" name="ProdutoStrutsActionForm" path="/atualizaProduto" scope="request" type="digital.AtualizaProdutoAction" />
<action input="/cadastroProduto.jsp" name="ProdutoStrutsActionForm" path="/cadastraProduto" scope="request" type="digital.ProdutoStrutsAction"/>
<action input="/alteraCategorias.jsp" name="CategoriaForm" path="/atualizaCategoria" scope="request" type="digital.AtualizarCategoriaAction"/>
<action input="/cadastroCategorias.jsp" name="CategoriaForm" path="/categoria" scope="request" type="digital.CategoriaStrutsAction"/>
<action input="/admin.jsp" name="LoginAdminActionForm" path="/loginadmin" scope="request" type="digital.LoginAdminAction"/>
<action input="/login.jsp" name="LoginActionForm" path="/login" scope="request" type="digital.LoginStrutsAction"/>
<action input="/index.jsp" name="LoginActionForm" path="/logincliente" scope="request" type="digital.LoginStrutsAction"/>
<action input="/cliente.jsp" name="ClienteActionForm" path="/cadastrocliente" scope="request" type="digital.ClientesAction"/>
<action input="/alteraCliente.jsp" name="ClienteActionForm" path="/alterar" scope="request" type="digital.AlterarClienteAction"/>
</action-mappings>
<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
<message-resources parameter="com/myapp/struts/ApplicationResource"/>
<plug-in className="org.apache.struts.tiles.TilesPlugin" >
<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
<set-property property="moduleAware" value="true" />
</plug-in>
<!-- ========================= Validator plugin ================================= -->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>
Esse é o formulario serve atualizar o cadastro do cliente
<%@page language="java"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ include file="/WEB-INF/jspf/acesso.jspf" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<title>Digitallist</title>
<sql:query var="cliente" dataSource="${con}">
SELECT * FROM cliente
where idcliente="<c:out value="${param.alt}"/>"
</sql:query>
<link href="CSS/padrao.css" rel="stylesheet" type="text/css">
<center><b>Altera cadastro de cliente pelo administrador</b></center>
<table class="content" width="80%" border="0" align="center">
<tbody>
<html:form action="/alterarclienteadmin" focus="nome" method="post">
<tr>
<tr>
<td></td>
<td><html:hidden property="idcliente" name="idcliente" value="${cliente.rowsByIndex[0][0]}"/></td>
</tr>
<td><b>Nome:*</b></td>
<td><html:text property="nome" value="${cliente.rowsByIndex[0][1]}" size="60"/></td>
</tr>
<tr>
<td><b>Endereço:*</b></td>
<td><html:text property="endereco" value="${cliente.rowsByIndex[0][2]}" size="60"/></td>
</tr>
<tr>
<td><b>Bairro:*</b></td>
<td><html:text property="bairro" value="${cliente.rowsByIndex[0][3]}" size="50"/></td>
</tr>
<tr>
<td><b>CEP:*</b></td>
<td><html:text property="cep" value="${cliente.rowsByIndex[0][5]}" size="10" /><br></td>
</tr>
<tr>
<td><b>Cidade:*</b></td>
<td><html:text property="cidade" value="${cliente.rowsByIndex[0][4]}" size="60"/></td>
</tr>
<tr>
<td><b>Estado:</b></td>
<td><html:text property="estado" value="${cliente.rowsByIndex[0][6]}" size="2" /> </td>
</tr>
<tr>
<td>Telefone:</td>
<td><html:text property="telefone" value="${cliente.rowsByIndex[0][7]}" size="13" maxlength="13"/></td>
</tr>
<tr>
<td>Fax:</td>
<td><html:text property="fax" value="${cliente.rowsByIndex[0][9]}" size="13" maxlength="13"/></td>
</tr>
<tr>
<td>Celular:</td>
<td><html:text property="celular" value="${cliente.rowsByIndex[0][8]}" size="13" maxlength="13"/></td>
</tr>
<tr>
<td><b>E-mail:*</b></td>
<td><html:text property="email" value="${cliente.rowsByIndex[0][10]}" size="60"/></td>
</tr>
<tr>
<td><b>Usuário:*</b></td>
<td><html:text property="usuario" value="${cliente.rowsByIndex[0][11]}" readonly="readonly"/></td>
</tr>
<tr>
<td><b>Senha:*</b></td>
<td><html:password property="senha" value="${cliente.rowsByIndex[0][12]}"/></td>
</tr>
<tr>
<td>Contato:</td>
<td><html:text property="contato" value="${cliente.rowsByIndex[0][14]}" size="60"/></td>
</tr>
<tr>
<td colspan="2" rowspan="1">
<html:submit value="Enviar"></html:submit>
</tr>
</html:form>
<tr>
<td><td colspan="2" rowspan="1"><html:errors/></td>
</tr>
</tbody>
</table>
</body>
</html>