I really dont get it, I might be being so stupid not figuring out what
is going on wrong here, but I dont.
Clearing a little. I decided not to use DynaForm and go back to the
usual ActionForm and gave up using Client-side validation.
When I implement the validate method of the Bean (as it is now) I get
the error messages as expected, but I had to create a protected method
to check if the field was null or blank, then put the error message in
the ActionMessage.
But I dont see any sense in doing such thing like that, why should I do
this inside validate method if I have already said in validation.xml
that those fields are REQUIRED, at least that message should be shown to
the user. Correct me if I am wrong.
If I comment the validate method nothing happen, it is not able to check
if the field was left blank.
Please be pacience and have a look again:
struts-config.xml:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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>
<!-- This is it -->
<form-bean name="LoginUsuarioForm"
type="br.com.gerente.bean.LoginFormBean"/>
<form-bean name="GetClienteForm"
type="br.com.gerente.bean.GetClienteFormBean"/>
</form-beans>
<global-exceptions>
</global-exceptions>
<global-forwards>
<forward name="welcome" path="/Clientes.do"/>
<forward name="inicio" path="/index.jsp" redirect="true"/>
</global-forwards>
<action-mappings>
<!-- This is an action just to call the .jsp file with the form -->
<action path="/prepararLogin"
type="br.com.gerente.action.SuccessAction">
<forward name="success" path="/paginas/login.jsp"/>
</action>
<!-- This is my (first) problem in validation -->
<action name="LoginUsuarioForm"
input="/paginas/login.jsp"
path="/LoginUsuario"
scope="request"
type="br.com.gerente.action.LoginUsuarioAction"
validate="true">
<forward name="sucesso" path="/paginas/clienteCadastro.jsp"/>
</action>
<!-- This is just the next form to be processed after validating
the previous -->
<action path="/Clientes" forward="/paginas/clienteCadastro.jsp"/>
<!-- This is another form with the data from the previous inputs -->
<action name="GetClienteForm"
input="/paginas/clienteCadastro.jsp"
path="/clientes"
scope="request"
type="br.com.gerente.action.GetClienteFormAction">
<forward name="mostrandoDadosCliente"
path="/paginas/clienteDados.jsp"/>
</action>
</action-mappings>
<controller
processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
<message-resources parameter="br/com/gerente/Resources"/>
<!-- ========================= Tiles plugin
===============================-->
<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"/>
<set-property property="stopOnFirstError" value="true" />
</plug-in>
</struts-config>
validation.xml:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules
Configuration 1.1.3//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">
<form-validation>
<global>
</global>
<formset >
<constant>
<constant-name>postalCode</constant-name>
<constant-value>^[0-9a-zA-Z]*$</constant-value>
</constant>
<form name="LoginUsuarioForm">
<field property="usuario" depends="required">
<arg key="login.usuario"/>
</field>
<field property="senha" depends="required">
<arg key="login.senha"/>
</field>
</form>
</formset>
</form-validation>
//---------------------------------------------------------------------------------------------------------------
/*
* LoginFormBean.java
*
* Created on 21 de Setembro de 2005, 08:25
*
*/
package br.com.gerente.bean;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;
public class LoginFormBean extends
org.apache.struts.validator.ValidatorForm {
private String usuario;
private String senha;
private String senhaConfirmada;
public LoginFormBean() {
}
public void reset(ActionMapping actionMapping, HttpServletRequest
request) {
}
public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest request) {
ActionErrors errors = super.validate( actionMapping, request );
if (errors == null) {
errors = new ActionErrors( );
}
if( nullOrBlank( (String)this.getUsuario()) ) {
errors.add("usuario", new
ActionMessage("login.usuario.requerido"));
}
if( nullOrBlank( (String)this.getSenha()) ) {
errors.add("senha", new ActionMessage("login.senha.requerido"));
}
return errors;
}
protected boolean nullOrBlank(String str) {
return ((str == null) || (str.length() == 0));
}
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 getSenhaConfirmada() {
return senhaConfirmada;
}
public void setSenhaConfirmada(String senhaConfirmada) {
this.senhaConfirmada = senhaConfirmada;
}
}
//---------------------------------------------------------------------------------------------------------------
/*
* LoginUsuarioAction.java
*
* Created on 21 de Setembro de 2005, 09:50
*
*/
package br.com.gerente.action;
import br.com.gerente.bean.GetClienteFormBean;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class LoginUsuarioAction extends org.apache.struts.action.Action {
/** Creates a new instance of LoginUsuarioAction */
public LoginUsuarioAction() {
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
return mapping.findForward("sucesso");
}
}
//---------------------------------------------------------------------------------------------------------------
Resources.properties:
#--- erro padrao / default errors --
errors.header=<font color="red">Erro! Por favor verifique a(s)
seguinte(s) mensagem(ns):<ul>
errors.footer=</ul></font>
errors.prefix=<LI>
errors.suffix=</LI>
errors.invalid={0} is invalid.
errors.maxlength={0} can not be greater than {1} characters.
errors.minlength={0} can not be less than {1} characters.
errors.range={0} is not in the range {1} through {2}.
errors.required={0} é obrigatório.
errors.byte={0} must be an byte.
errors.date={0} is not a date.
errors.double={0} must be an double.
errors.float={0} must be an float.
errors.integer={0} must be an integer.
errors.long={0} must be an long.
errors.short={0} must be an short.
errors.creditcard={0} is not a valid credit card number.
errors.email={0} is an invalid e-mail address.
errors.cancel=Operation cancelled.
errors.detail={0}
errors.general=The process did not complete. Details should follow.
errors.token=Request could not be completed. Operation is not in sequence.
login.titulo=Login de Acesso ao Sistema [G]erente
login.usuario=Nome de acesso
login.senha=Senha de acesso
login.usuario.requerido=Nome de acesso obrigatório
login.senha.requerido=Senha de acesso obrigatório
# -- botoes/buttons --
botao.enviar=Enviar Dados
botao.cancelar=Cancelar
botao.confirmar=Confirmar
botao.limpar=Limpar
botao.salvar=Salvar
//---------------------------------------------------------------------------------------------------------------
login.jsp:
<%@ 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" %>
<html:html locale="true">
<head>
<title><bean:message key="login.titulo"/></title>
<html:base/>
</head>
<body>
<center>
<h1><bean:message key="login.titulo"/></h1>
<table width="50%">
<html:form action="/LoginUsuario.do" focus="login"
method="post">
<thead>
<tr>
<th ><bean:message key="login.usuario"/></th>
<th ><bean:message key="login.senha"/></th>
</tr>
</thead>
<tbody>
<tr>
<td><html:text property="usuario" size="20"
maxlength="20" /></td>
<td><html:password property="senha"
size="20" maxlength="10"/></td>
</tr>
<tr>
<td>
<html:submit>
<bean:message key="botao.enviar" />
</html:submit>
</td>
<td>
<html:cancel>
<bean:message key="botao.cancelar" />
</html:cancel>
</td>
</tr>
</tbody>
</html:form>
</table>
</center>
<html:errors />
</body>
<html:javascript formName="LoginUsuarioForm"
dynamicJavascript="true" staticJavascript="false"/>
</html:html>
Thanks in advance.
Francisco
_______________________________________________________
Novo Yahoo! Messenger com voz: ligações, Yahoo! Avatars, novos emoticons e muito mais. Instale agora!
www.yahoo.com.br/messenger/
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]