validação struts e recuperar valores invalidos

0 respostas
R

Pessoal tenho uma tela de cadastro.jsp e estou fazendo a validação de valores dos campos no form, como faço para recuperar os valores que estão preenchido incorreto nos campos da minha jsp e mostrar uma menssagem para o usuário de valor incorreto.

Form

/*

  • Created on Jun 9, 2008
  • TODO To change the template for this generated file go to
  • Window - Preferences - Java - Code Style - Code Templates
    */
    package br.com.locadora.locadora1.form;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.action.ActionMessage;

import org.apache.struts.action.ActionMessages;

import br.com.locadora.locadora1.Util.Validacao;

public class CadastroClienteForm extends ActionForm {

private String acao;

private String codigocli;

private String nome = “”;

private String endereco;

private String rg;

private String cpf;

private String telfixo;

private String cel;

private String bairro;

public String getBairro() {

return bairro;

}
public void setBairro(String bairro) {

this.bairro = bairro;

}
public String getCel() {

return cel;

}
public void setCel(String cel) {

this.cel = cel;

}
public String getCodigocli() {

return codigocli;

}
public void setCodigocli(String codigocli) {

this.codigocli = codigocli;

}
public String getCpf() {

return cpf;

}
public void setCpf(String cpf) {

this.cpf = cpf;

}
public String getEndereco() {

return endereco;

}
public void setEndereco(String endereco) {

this.endereco = endereco;

}
public String getNome() {

return nome;

}
public void setNome(String nome) {

this.nome = nome;

}
public String getRg() {

return rg;

}
public void setRg(String rg) {

this.rg = rg;

}
public String getTelfixo() {

return telfixo;

}
public void setTelfixo(String telfixo) {

this.telfixo = telfixo;

}

public void reset(ActionMapping mapping , HttpServletRequest request ) {

super.reset(mapping , request );

acao = “”;

}
public String getAcao() {

return acao;

}
public void setAcao(String acao) {

this.acao = acao;

}

/*

  • (non-Javadoc)
  • @see org.apache.struts.action.ActionForm#validate(org.apache.struts.action.ActionMapping,
  • javax.servlet.http.HttpServletRequest)
    */
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {

ActionErrors messages = new ActionErrors();
boolean error = false;

CadastroClienteForm form = (CadastroClienteForm) request.getSession().getAttribute(“CadastroClienteForm”);

if (!Validacao.isNull(form.acao)) {

if (Validacao.isNull(form.codigocli) || Validacao.isInteger(form.codigocli)) {

messages.add(ActionErrors.GLOBAL_ERROR, new ActionMessage(“Codigo Cliente Vazio!”));

error = true;

}

if (Validacao.isNull(form.nome)) {

messages.add(ActionErrors.GLOBAL_ERROR, new ActionMessage(“Nome Cliente Vazio!”));

error = true;

}

if (Validacao.isNull(form.endereco)) {

messages.add(ActionErrors.GLOBAL_ERROR, new ActionMessage(“Endereço Vazio!”));

error = true;

}

if (Validacao.isNull(form.rg)) {

messages.add(ActionErrors.GLOBAL_ERROR, new ActionMessage(“Rg Vazio!”));

error = true;

}

if (Validacao.isNull(form.cpf)) {

messages.add(ActionErrors.GLOBAL_ERROR, new ActionMessage(“CPF Vazio!”));

error = true;

}

if (Validacao.isNull(form.telfixo)) {

messages.add(ActionErrors.GLOBAL_ERROR, new ActionMessage(“Telefone Fixo Vazio!”));

error = true;

}

if (Validacao.isNull(form.cel)) {

messages.add(ActionErrors.GLOBAL_ERROR, new ActionMessage(“Celular Vazio!”));

error = true;

}

if (Validacao.isNull(form.bairro)) {

messages.add(ActionErrors.GLOBAL_ERROR, new ActionMessage(“Bairro Vazio!”));

error = true;

}

}

request.setAttribute(“erros”,messages);

if (error) {

return messages;

} else {

return null;

}

/* Enumeration session = request.getSession().getAttributeNames();

while (session.hasMoreElements()) {

String element = (String) session.nextElement();

System.err.println(session:+ element);

}

Enumeration req = request.getAttributeNames();

while (req.hasMoreElements()) {

String element = (String) req.nextElement();

System.err.println(req:+ element);

}*/

}

}

Minha JSP

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix=html%>

<html>

<head>

<title>Pagina de Login</title>

<script language=“JavaScript”>

function validaForm(){
d = document.cadcliente;

if (d.codigocli.value == “”){

alert(O campo " + d.codigocli.name + " deve ser preenchido!);

d.codigocli.focus();

return false;

}
if (d.nome.value == “”){

alert(O campo " + d.nome.name + " deve ser preenchido!);

d.nome.focus();

return false;

}

if (d.endereco.value == “”){

alert(O campo " + d.endereco.name + " deve ser preenchido!);

d.endereco.focus();

return false;

}
if (d.rg.value == “”){

alert(O campo " + d.rg.name + " deve ser preenchido!);

d.rg.focus();

return false;

}

if (d.cpf.value == “”){

alert(O campo " + d.cpf.name + " deve ser preenchido!);

d.cpf.focus();

return false;

}
if (d.telfixo.value == “”){

alert(O campo " + d.telfixo.name + " deve ser preenchido!);

d.telfixo.focus();

return false;

}

if (d.cel.value == “”){

alert(O campo " + d.cel.name + " deve ser preenchido!);

d.cel.focus();

return false;

}
if (d.bairro.value == “”){

alert(O campo " + d.bairro.name + " deve ser preenchido!);

d.bairro.focus();

return false;

}

return true;

}

</script>
</head>

<form name=“cadcliente” Reset="" method=“post” onSubmit=“return validaForm()”>

<body bgcolor=“lightblue”>

<h2 align=“CENTER”>Cadastrar Clientes</h2>




<html:form action="/cadastroCliente" method=“post”>

<html:hidden property=“operation” value="" />

<table border=2 align=“center”>
<tr>
<td>Codigo Cliente:</td>
<td><input type=“text” name=“codigocli” size=“30” /></td>
</tr>
<tr>
<td>Nome:</td>
<td><input type=“text” name=“nome” size=“30” /></td>
</tr>

<tr>

<td>Endereço:</td>

<td><input type=“text” name=“endereco” size=“30” /></td>

</tr>

<tr>

<td>RG:</td>

<td><input type=“text” name=“rg” size=“30” /></td>

</tr>

<tr>

<td>CPF:</td>

<td><input type=“text” name=“cpf” size=“30” /></td>

</tr>

<tr>

<td>Telefone:</td>

<td><input type=“text” name=“telfixo” size=“30” /></td>

</tr>

<tr>

<td>Celular:</td>

<td><input type=“text” name=“cel” size=“30” /></td>

</tr>

<tr>

<td>Bairro:</td>

<td><input type=“text” name=“bairro” size=“30” /></td>

</tr>

</table>




<table border=0 align=“center”>

<tr>

<td><input type=submit name=“acao” value=“Cadastrar”></td>

<td><INPUT TYPE=“reset” NAME=“limpar” VALUE=“Limpar”></td>

<td><input type=“button” value=“voltar” onclick=javascript:window.history.back()></td>

</td>

</tr>
</html:form>
</table>
</body>
</FORM>
</html>

Criado 23 de junho de 2008
Respostas 0
Participantes 1