You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Lakshmi Saritha <sa...@yahoo.com> on 2006/08/30 06:35:00 UTC

Regarding Struts validator Framework

Hello everybody,
         
        I'm new to Java. I'm working on Struts now. I got this application from one struts PDF. I've one jsp for customer details as follows:
  CustomerDetails.jsp:
  <%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
  <html:html>
<head>
<html:base/>
</head>
  <body>
 <html:form action="submitCustomerForm" onsubmit="return validateCustomerForm(this);">
<bean:message key="custForm.firstName"/>
 <html:text property="firstName" size="16" maxlength="16"/>
<br>
<bean:message key="custForm.lastName"/>
 <html:text property="lastName" size="16" maxlength="16"/>
<br>
<html:submit value="Submit"/>
  </html:form>
</body>
</html:html>
   
  CustomerForm.java:
  import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.ValidatorForm;
import javax.servlet.http.HttpServletRequest;
 public class CustomerForm extends ValidatorForm
 {
  private String firstName;
  private String lastName;
    public CustomerForm(){
   firstName="";
   lastName="";
  }
  public String getFirstName(){
   return firstName;
  }
    public void setFirstName(String fn){
   this.firstName=fn;
  }
    public String getLastName(){
   return lastName;
  }
    public void setLastName(String ln){
   this.lastName=ln;
  } 
 }
   
  CustomerAction.java:
  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.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionMessage;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
  public class CustomerAction extends Action
{
 public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
  if(isCancelled(request)){
   System.out.println("Cancel Operation Performed");
   return mapping.findForward("mainpage");
  }
    CustomerForm custForm=(CustomerForm) form;
  String firstName=custForm.getFirstName();
  String lastName=custForm.getLastName();
  System.out.println("Customer First name is "+firstName);
  System.out.println("Custoemr Last name is "+lastName);
    ActionForward forward=mapping.findForward("success");
  return forward;
 }
}

  web.xml
  f 
  <?xml version='1.0' encoding='windows-1252'?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee">
  <description>Empty web.xml file for Web Application</description>
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
 <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>3</param-value>
    </init-param>
 <init-param>
 <param-name>application</param-name>
 <param-value>/WEB-INF/classes/App1Messages.properties</param-value>
 </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
    </init-param>
    <init-param>
      <param-name>validate</param-name>
      <param-value>true</param-value>
    </init-param>
   <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <session-config>
    <session-timeout>35</session-timeout>
  </session-config>
  <mime-mapping>
    <extension>html</extension>
    <mime-type>text/html</mime-type>
  </mime-mapping>
  <mime-mapping>
    <extension>txt</extension>
    <mime-type>text/plain</mime-type>
  </mime-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
     
  </welcome-file-list>
  <taglib>
    <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
  </taglib>
</web-app>
  
   
  struts-config.xml
   
  <?xml version="1.0" encoding="windows-1252" ?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
   
   
  <struts-config>
  <form-beans>
 <form-bean name="custForm" type="CustomerForm"/>
   
    </form-beans>
    <global-forwards>
  <forward name="mainpage" path="index.jsp"/>
    </global-forwards>
    <action-mappings>  
 <action path="/submitCustomerForm" type="CustomerAction" name="custForm" scope="request" validate="true" input="/CustomerDetails.jsp">
 <forward name="success" path="/Success.jsp"/>
 <forward name="failure" path="/Failure.jsp"/>
 </action>
   </action-mappings>
 <controller processorClass="org.apache.struts.action.RequestProcessor"/>
   <message-resources parameter="App1Messages"/>
     <!-- Validator Configuration -->
   <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
  <set-property property="pathnames"
      value="/WEB-INF/validator-rules.xml,/WEB-INF/validator.xml"/>
</plug-in>
  </struts-config>
   
  Upto now it's working fine. But it's not displaying the error message that I've mentioned in the properties file. This file is as follows and is in WEB-INF/classes.
   
  App1Messages.properties:
  custForm.firstName=First Name
custForm.lastName=Last Name 
errors.required=Required
 
  validator-rules.xml
  <!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>
        <validator name="required"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateRequired"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       org.apache.commons.validator.Validator,
                       javax.servlet.http.HttpServletRequest"
                  msg="errors.required"/>
  .
  .
  .

  validator.xml
  <!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>
<formset>
<form name="custForm">
<field property="firstName" depends="required"/>
<field property="lastName" depends="required"/>
 
</form>
</formset>
</form-validation>

  I've my success.jsp and failure.jsp.
   
  When I don't enter data into firstname and lastname, the form is displayed, but the error message is not displying. validator.xml is working fine because if I don't give any one or both of first name and last name it's displaying the empty form and if I give both, success.jsp is displaying. In some struts pdfs I've seen that if validation errors occur, the JSP u menthioned in input attribute is displayed with the errors taken from properties file. Is is true or not? If so, why did the author used the Failure.jsp again? Please clear me why the message is not coming from properties file. Is the error in properties file or validator-rules.xml or some other. 
   
  Thanks and regards,
         Saritha

 		
---------------------------------
Yahoo! Messenger with Voice. Make PC-to-Phone Calls to the US (and 30+ countries) for 2¢/min or less.

Re: Regarding Struts validator Framework

Posted by Wendy Smoak <ws...@gmail.com>.
On 8/29/06, Lakshmi Saritha <sa...@yahoo.com> wrote:

>  Please clear me why the message is not coming from properties file.
> Is the error in properties file or validator-rules.xml or some other.

Try adding an <html:errors /> tag to your CustomerDetails JSP, I don't
see it there.

-- 
Wendy

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Regarding Struts validator Framework

Posted by Sr...@3com.com.



In the jsp code u given    onsubmit="return validateCustomerForm(this); but
in validator-user.xml the form name is custForm, so replace that with
onsubmit="return validateCustForm(this);

Thanks,
Srinivas.



                                                                           
             Lakshmi Saritha                                               
             <saripls@yahoo.co                                             
             m>                                                         To 
                                       user@struts.apache.org              
             08/30/2006 10:05                                           cc 
             AM                                                            
                                                                   Subject 
                                       Regarding Struts validator          
             Please respond to         Framework                           
               "Struts Users                                               
               Mailing List"                                               
             <user@struts.apac                                             
                  he.org>                                                  
                                                                           
                                                                           




Hello everybody,

        I'm new to Java. I'm working on Struts now. I got this application
  from one struts PDF. I've one jsp for customer details as follows:
  CustomerDetails.jsp:
  <%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
  <html:html>
<head>
<html:base/>
</head>
  <body>
 <html:form action="submitCustomerForm" onsubmit="return
 validateCustomerForm(this);">
<bean:message key="custForm.firstName"/>
 <html:text property="firstName" size="16" maxlength="16"/>
<br>
<bean:message key="custForm.lastName"/>
 <html:text property="lastName" size="16" maxlength="16"/>
<br>
<html:submit value="Submit"/>
  </html:form>
</body>
</html:html>

  CustomerForm.java:
  import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.ValidatorForm;
import javax.servlet.http.HttpServletRequest;
 public class CustomerForm extends ValidatorForm
 {
  private String firstName;
  private String lastName;
    public CustomerForm(){
   firstName="";
   lastName="";
  }
  public String getFirstName(){
   return firstName;
  }
    public void setFirstName(String fn){
   this.firstName=fn;
  }
    public String getLastName(){
   return lastName;
  }
    public void setLastName(String ln){
   this.lastName=ln;
  }
 }

  CustomerAction.java:
  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.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionMessage;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
  public class CustomerAction extends Action
{
 public ActionForward execute(ActionMapping mapping,ActionForm
  form,HttpServletRequest request,HttpServletResponse response) throws
  Exception{
  if(isCancelled(request)){
   System.out.println("Cancel Operation Performed");
   return mapping.findForward("mainpage");
  }
    CustomerForm custForm=(CustomerForm) form;
  String firstName=custForm.getFirstName();
  String lastName=custForm.getLastName();
  System.out.println("Customer First name is "+firstName);
  System.out.println("Custoemr Last name is "+lastName);
    ActionForward forward=mapping.findForward("success");
  return forward;
 }
}

  web.xml
  f
  <?xml version='1.0' encoding='windows-1252'?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4" xmlns="
  http://java.sun.com/xml/ns/j2ee">
  <description>Empty web.xml file for Web Application</description>
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
 <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>3</param-value>
    </init-param>
 <init-param>
 <param-name>application</param-name>
 <param-value>/WEB-INF/classes/App1Messages.properties</param-value>
 </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
    </init-param>
    <init-param>
      <param-name>validate</param-name>
      <param-value>true</param-value>
    </init-param>
   <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <session-config>
    <session-timeout>35</session-timeout>
  </session-config>
  <mime-mapping>
    <extension>html</extension>
    <mime-type>text/html</mime-type>
  </mime-mapping>
  <mime-mapping>
    <extension>txt</extension>
    <mime-type>text/plain</mime-type>
  </mime-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>
  <taglib>
    <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
  </taglib>
</web-app>


  struts-config.xml

  <?xml version="1.0" encoding="windows-1252" ?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts
Configuration 1.1//EN" "
http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">


  <struts-config>
  <form-beans>
 <form-bean name="custForm" type="CustomerForm"/>

    </form-beans>
    <global-forwards>
  <forward name="mainpage" path="index.jsp"/>
    </global-forwards>
    <action-mappings>
 <action path="/submitCustomerForm" type="CustomerAction" name="custForm"
 scope="request" validate="true" input="/CustomerDetails.jsp">
 <forward name="success" path="/Success.jsp"/>
 <forward name="failure" path="/Failure.jsp"/>
 </action>
   </action-mappings>
 <controller processorClass="org.apache.struts.action.RequestProcessor"/>
   <message-resources parameter="App1Messages"/>
     <!-- Validator Configuration -->
   <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
  <set-property property="pathnames"
      value="/WEB-INF/validator-rules.xml,/WEB-INF/validator.xml"/>
</plug-in>
  </struts-config>

  Upto now it's working fine. But it's not displaying the error message
  that I've mentioned in the properties file. This file is as follows and
  is in WEB-INF/classes.

  App1Messages.properties:
  custForm.firstName=First Name
custForm.lastName=Last Name
errors.required=Required

  validator-rules.xml
  <!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>
        <validator name="required"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateRequired"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       org.apache.commons.validator.Validator,
                       javax.servlet.http.HttpServletRequest"
                  msg="errors.required"/>
  .
  .
  .

  validator.xml
  <!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>
<formset>
<form name="custForm">
<field property="firstName" depends="required"/>
<field property="lastName" depends="required"/>

</form>
</formset>
</form-validation>

  I've my success.jsp and failure.jsp.

  When I don't enter data into firstname and lastname, the form is
  displayed, but the error message is not displying. validator.xml is
  working fine because if I don't give any one or both of first name and
  last name it's displaying the empty form and if I give both, success.jsp
  is displaying. In some struts pdfs I've seen that if validation errors
  occur, the JSP u menthioned in input attribute is displayed with the
  errors taken from properties file. Is is true or not? If so, why did the
  author used the Failure.jsp again? Please clear me why the message is not
  coming from properties file. Is the error in properties file or
  validator-rules.xml or some other.

  Thanks and regards,
         Saritha


 ---------------------------------
Yahoo! Messenger with Voice. Make PC-to-Phone Calls to the US (and 30+
countries) for 2¢/min or less.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org