Hi all,

I really need a big help in validation with Struts, I dont know why but I just cant validate a very simple form I have, with 2 simple fields (login and password), I have follown all tutorials I got but til now no success.

I am not able to validate either in server side or client side.

I am using Struts 1.2.7 - Is there any big difference in validating with this version and the previous ones?

Here are the stpeds I did:

1) created the DynaValidatorForm in struts-config and mapped the Actions it there too.
//-------------------------------------------------------------------------------------
....
<form-beans>
<form-bean name="loginForm" type="org.apache.struts.validator.DynaValidatorForm">
          <form-property name="login" type="java.lang.String" />
          <form-property name="senha" type="java.lang.String" />
          <form-property name="senha2" type="java.lang.String" />
      </form-bean>
  </form-beans>
....
<!-- this one is called to show the page with the fields -->
<action path="/prepararLogin"
          type="br.com.gerente.SuccessAction">
          <forward name="success" path="/paginas/login.jsp"/>
</action>

<!-- this one is processed when the submit button is pressed -->
<action path="/Login"
          type="br.com.gerente.ProcessaLoginDynaAction"
          name="loginForm"
          scope="request"
          input="input"
          validate="true">
          <forward name="success" path="/paginas/clienteCadastro.jsp"/>
          <forward name="input" path="/prepararLogin.do" />
</action>
...
<message-resources parameter="br/com/gerente/Resources"/>   ...
<!-- ========================= 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>

2) create the action to call the login.jsp (and others): SuccessAction.java
//-------------------------------------------------------------------------------------
package br.com.gerente;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class SuccessAction extends Action {
    /** Creates a new instance of SuccessAction */
  public SuccessAction() {
      super();
  }
    /**
   * Returns the <code>ActionForward</code> named "success" if one is
   * configured or <code>null</code>if it cannot be found.
   *
   * Searches first for a local forward, then a global forward.
   *
   * @param mapping The ActionMapping used to select this instance
   * @param form The optional ActionForm bean for this request (if any)
   * @param request The HTTP request we are processing
   * @param response The HTTP response we are creating
   *
   * @exception Exception if mapping.findForward throws an Exception
   *
   * @return the "success" ActionForward, or null if it cannot be found
   */
  public ActionForward execute(
          ActionMapping mapping,
          ActionForm form,
          HttpServletRequest request,
          HttpServletResponse response)
          throws Exception {
            return mapping.findForward("success");
        }
}

3) created the action: ProcessaLoginDynaAction.java
package br.com.gerente;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.DynaValidatorForm;

public class ProcessaLoginDynaAction extends Action {
    /** Creates a new instance of ProcessaLoginDynaAction */
  public ProcessaLoginDynaAction() {
      super();
  }
    /**
   * Process the request and return an <code>ActionForward</code> instance
   * describing where and how control should be forwarded, or
   * <code>null</code>if the response has already been completed.
   *
   * @param mapping The ActionMapping used to select this instance
   * @param form The optional ActionForm bean for this request (if any)
   * @param request The HTTP request we are processing
   * @param response The HTTP response we are creating
   *
   * @exception Exception if the application logic throws an exception
   *
   * @return the ActionForward for the next view
   */
  public ActionForward execute(
          ActionMapping mapping,
          ActionForm form,
          HttpServletRequest request,
          HttpServletResponse response)
          throws Exception {
// Se o usuario pressionar o botao 'Cancelar' volta para o inicio
      if (isCancelled(request)) {
          return mapping.findForward("inicio");
      }
      DynaValidatorForm dynaForm = (DynaValidatorForm)form;
      //dynaForm.set("login","nome do login");
      //System.out.println("Login: "+dynaForm.get("login") );
      // Forward to result page
      return mapping.findForward("success");
  } }

4) created 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/>
      <link rel="stylesheet" type="text/css" href="estilo.css" />
  </head>
  <body>
      <center>
          <h1><bean:message key="login.titulo"/></h1>
                    <html:errors property="login"/>
                        <table border="0" width="50%">
<html:form action="/Login.do" focus="login" method="post" onsubmit="return validateformLogin(this)">
                  <thead>
                      <tr>
                          <th></th>
                          <th></th>
                      </tr>
                  </thead>
                  <tbody>
                      <tr>
<td class="into"><bean:message key="login.nome"/></td>
                          <td>
<html:text property="login" size="20" maxlength="20" />
                          </td>
                      </tr>
                      <tr>
                          <td><bean:message key="login.senha"/></td>
<td><html:password property="senha" size="20" maxlength="10"/></td>
                      </tr>
                      <tr>
                          <td></td>
                          <td>
                              <html:submit>
                                  <bean:message key="botao.enviar" />
                              </html:submit>
                              <html:cancel>
                                  <bean:message key="botao.cancelar" />
                              </html:cancel>
                          </td>
                      </tr>
                  </tbody>
              </html:form>
          </table>                              </center>          </body>
<html:javascript formName="loginForm" dynamicJavascript="true" staticJavascript="false"/>
</html:html>

5) The resources file: Resources.properties
//-------------------------------------------------------------------------------------
#--- erro padrao --
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.
welcome.title=Struts Application
sistema.titulo=Sistema de Gerenciamento dos Clientes da Kooky
cliente.titulo.cadastro=Cadastro de Clientes
cliente.titulo.dados=Cadastro de Clientes - Dados
welcome.heading=Struts Applications in Netbeans!
welcome.message=It's easy to create Struts applications with NetBeans.

login.titulo=Login de Acesso ao Sistema [G]erente
login.nome=Nome de acesso
login.senha=Senha de acesso

erro.data=Data Invalida
erro.obrigatorio=Dados obrigatorio
erro.email=Email errado

# -- campos obrigatorios --
id.obrigatorio=O Indentificador é obrigatório

# -- formatando --
formato.data=dd/MM/yyyy
formato.moeda=R$ #.##0,00;R$ (#.##0,00)

# -- botoes --
botao.enviar=Enviar Dados
botao.cancelar=Cancelar
botao.confirmar=Confirmar
botao.limpar=Limpar
botao.salvar=Salvar

6) the validation.xml:
//-------------------------------------------------------------------------------------
<formset language="pt" country="BR" >

      <constant>
          <constant-name>postalCode</constant-name>
          <constant-value>^[0-9a-zA-Z]*$</constant-value>
      </constant>

      <form name="loginForm">
          <field property="login" depends="required">
              <arg key="login.nome"/>
          </field>
          <field property="senha" depends="required">
              <arg key="login.senha"/>
</field> </form>
</formset>

//-------------------------------------------------------------------------------------
So, according to the mapping action when I press the submit button with the fileds empty it should show me 2 messages in the page, right? But it does not show anything, just call the next page.
What am I doing wrong?
I really would appreciate any help from you guys.

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]

Reply via email to