struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Lakshmi Saritha <sari...@yahoo.com>
Subject Regarding Struts validator Framework
Date Wed, 30 Aug 2006 04:35:00 GMT
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.
Mime
  • Unnamed multipart/alternative (inline, 8-Bit, 0 bytes)
View raw message