Solved

Struts Validation not working  - Getting exception thrown

Posted on 2006-06-18
4
479 Views
Last Modified: 2013-12-10
Hi Experts,

Currently I'm working on a Struts Project which requires that I enter two dates : "fromdate" and "todate" values into text boxes in an html:form tag. The Action Form looks like this :

package com.telus.insight.agent.forms;

import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
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.validator.ValidatorActionForm;
import org.apache.struts.action.ActionMapping;

public class DatePPOFilterForm extends ValidatorActionForm {

      
      private String period = null;
      private String fromdate = null;
      private String todate = null;
      private String defaultFromDate = null;
      private String defaultToDate = null;
      
      
      public void setPeriod(String period){
            this.period = period;
      }
      
      public String getPeriod(){
            return period;
      }
      
      public void setFromdate(String from){
            this.fromdate = from;
      }
      
      public String getFromdate(){
            return fromdate;
      }
      
      public void setTodate(String to){
            this.todate = to;
      }
      
      public String getTodate(){
            return todate;
      }
      
      public void setDefaultFromDate(String date){
            this.defaultFromDate = date;
      }
      
      public String getDefaultFromDate(){
            return defaultFromDate;
      }
      
      public void setDefaultToDate(String date){
            this.defaultToDate = date;
      }
      
      public String getDefaultToDate(){
            return defaultToDate;
      }
}

I've defined validation for the specific action using this validation in the validation.xml file. This is what it looks like :

form-validation>
 <global/>
       <formset>
             <constant>
                   <constant-name>dateFormat</constant-name>
                   <constant-value>yyyy-MM-dd</constant-value>
            </constant>
              <form name="/agentdatefilter">
               <field property="fromdate" depends="date">
                      <var>
                       <var-name>datePatternStrict</var-name>
                       <var-value>${dateFormat}</var-value>
                      </var>
               </field>
               <field property="todate" depends="date" >
                      <var>
                       <var-name>datePatternStrict</var-name>
                       <var-value>${dateFormat}</var-value>
                      </var>
               </field>
              </form>
      </formset>
</form-validation>

The jsp which using this validation for some reason won't output the error messages. When I click the submit button, I'm getting an exception which makes sense because when I put in an invalid date, the next Action shouldnt be called in the first place. The Validation error should be outputted to the screen. This is not happening. On submitting the form, it appears that Struts is attempting to send the invalid info onwards. What am I doing wrong ?

NB. I've included the plugin in the Struts-config.xml file.

Your help would be appreciated !!!

Pia

0
Comment
Question by:piampri
4 Comments
 
LVL 3

Accepted Solution

by:
anumalas earned 500 total points
ID: 17057551
Hi

In your jsp file include the following lines at approprite places and try once again

<%@ taglib uri="/tags/struts-html" prefix="html" %>


<html:form action="/YourValiditonAction" method="post" onsubmit="return validatorActionForm(this);">


<html:javascript formName="ValidatorActionForm"/>

In your struts-config.xml this particular mapping will look like bellow:

<action
     path="/YourValidationAction"
     type="com.telus.insight.agent.actions.YourValidationAction"
     name="ValidatorActionForm"
     scope="request"
     validate="true"
     input="/jsp/yourjsp.jsp">
     <forward name="success" path="/jsp/success.jsp"/>
     <forward name-"failure" path="/jsp/yourjsp.jsp"/>
</action>

The main porblem is while mapping your jsp  with action your doing small mistake to invoke validator frame work.

For further reference please go thru URL @   http://www.roseindia.net/struts/address_struts_validator.shtml   its a nice one

Hope this will help :)
0
 

Author Comment

by:piampri
ID: 17132040
Thanks Anumalas,

Your suggestion worked. I guess I was skipping a few steps.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Configure Web Service (server application) I. Configure security for Web Services methods First, we need to protect Session bean which implements the service: 1. Open EJB deployment descriptor (ejb-jar.xml) in the EJB project that contains you…
There are numerous questions about how to setup an IBM HTTP Server to be administered from WebSphere Application Server administrative console. I do hope this article will wrap things up and become a reference for this task. You need three things…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question