Solved

Struts Validation not working  - Getting exception thrown

Posted on 2006-06-18
4
488 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

-Xmx and -Xms are the two JVM options often used to tune JVM heap size.   Here are some common mistakes made when using them:   Assume BigApp is a java class file for the below examples. 1.         Missing m, M, g or G at the end …
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

733 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