Solved

Struts Validation not working  - Getting exception thrown

Posted on 2006-06-18
4
466 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Verbose logging is used to diagnose garbage collector problems. By default, -verbose:gc output is written to either native_stderr.log or native_stdout.log.   It is also possible to redirect the logs to a user-specified file. This article will de…
Most of the developers using Tomcat find it easy to configure the datasource in Server.xml and use the JNDI name in the code to get the connection.  So the default connection pool using DBCP (or any other framework) is made available and the life go…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now