facebook

Struts validation fails

  1. MyEclipse IDE
  2.  > 
  3. Off Topic
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #268113 Reply

    davout
    Participant

    I’ve added Struts validation to my application and I’m getting an exception when I try to use the ‘required’ validation type.

    Here’s the code…

    The ‘validation.xml’ file…

     
     <?xml version="1.0" encoding="UTF-8"?>
     <!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="impactNegationEditForm">
             <field property="title" depends="required">
                <arg0 key="label.title"/>
             </field>
          </form>
       </formset>
     </form-validation>
    

    the start of the ‘validation-rules.xml’ file…

     
     <!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"/>
    

    the related action class…

    
     public class ImpactNegationEditAction extends AbstractImpactNegationAction
           implements EntityIDs, ImpactNegationIDs {
     
        public void save_onClick(FormActionContext aCtx) {
           ImpactNegationEditForm aForm = (ImpactNegationEditForm) aCtx.form();
           aCtx.addErrors(aForm.validate(aCtx.mapping(),aCtx.request()));
           if (aCtx.hasErrors() == false) {
              // is add?
              if (aForm.getImpactNegationIDInt() == ID_UNASSIGNED) {
                 // verify title is unique
                 try {
                    ImpactNegation anOther = getManager(aCtx).findByTitle(aForm.getTitle());
                    if (anOther != null) {
                       addFieldError(aCtx,"title","error.duplicateField","Title");
                    }
    

    the related form…

     
     public abstract class AbstractSorForm extends FWValidatorForm {
     }
     
     public class ImpactNegationEditForm extends AbstractSorForm {
        
        private static final long serialVersionUID = 3604637580339030809L;
        
        private String fImpactNegationID;
        private String fTitle;
        private String fDesc;
        private String fDisplayOrder;
        private String fPercent;
        private String fCaptionDetail = null;
        private String fFormTitle = null;
     
        public ImpactNegationEditForm() {
           // TODO Auto-generated constructor stub
        }
        
        public String getDesc() {
           return fDesc;
        }
     
        public void setDesc(String aDesc) {
           fDesc = aDesc;
        }
     
        public String getDisplayOrder() {
           return fDisplayOrder;
        }
        public int getDisplayOrderInt() {
           return Integer.parseInt(fDisplayOrder);
        }
     
        public void setDisplayOrder(String aDisplayOrder) {
           fDisplayOrder = aDisplayOrder;
        }
     
        public String getImpactNegationID() {
           return fImpactNegationID;
        }
     
        public int getImpactNegationIDInt() {
           return Integer.parseInt(fImpactNegationID);
        }
     
        public void setImpactNegationID(String aId) {
           fImpactNegationID = aId;
        }
     
        public String getPercent() {
           return fPercent;
        }
        public double getPercentDouble() {
           return Double.parseDouble(fPercent);
        }
     
        public void setPercent(String aPercent) {
           fPercent = aPercent;
        }
     
        public String getTitle() {
           return fTitle;
        }
     
        public void setTitle(String aTitle) {
           fTitle = aTitle;
        }
     
        public String getCaptionDetail() {
           return fCaptionDetail;
        }
     
        public void setCaptionDetail(String aCaptionDetail) {
           fCaptionDetail = aCaptionDetail;
        }
     
        public String getFormTitle() {
           return fFormTitle;
        }
     
        public void setFormTitle(String aFormTitle) {
           fFormTitle = aFormTitle;
        }
     }
     
    

    the JSP body file…

    
     <ctrl:headline caption="Impact Negation" detail="${impactNegationEditForm.captionDetail}" width="60%"/>
     
     <html:form action="sor/admin/impact_negation/impact_negation_edit" >
     
        <html:hidden property="impactNegationID"/>
     
        <forms:message  caption="Error"  severity="error"  formid="err"/>
        <forms:message  caption="Information"  severity="information"  formid="info"/>
     
        <forms:form name="impactNegationEditForm"
                    type="edit"
                    caption="${impactNegationEditForm.formTitle}"
                    formid="impactNegationEditForm" width="60%">
           <forms:text label="Title" property="title" required="true"/>
           <forms:textarea label="Description" property="desc" cols="60" rows="6"/>
           <forms:text label="Display order" property="displayOrder"/>
           <forms:text label="Percent" property="percent"/>
     
           <forms:buttonsection>
               <forms:button styleId="btnBack" name="btnCancel" text="Cancel" />
               <forms:button styleId="btnBack" name="btnRestore" text="Restore" />
               <forms:button styleId="btnBack" name="btnSave" text="Save" />
           </forms:buttonsection>
     
        </forms:form>
     
     </html:form>
    
    
    the exception thrown when the form is validated...
     ERROR: 18:54:18: org.apache.struts.validator.ValidatorForm: org.apache.struts.validator.FieldChecks.validateRequired(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)
     org.apache.commons.validator.ValidatorException: org.apache.struts.validator.FieldChecks.validateRequired(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)
       at org.apache.commons.validator.ValidatorAction.loadValidationMethod(ValidatorAction.java:627)
       at org.apache.commons.validator.ValidatorAction.executeValidationMethod(ValidatorAction.java:557)
       at org.apache.commons.validator.Field.validateForRule(Field.java:811)
       at org.apache.commons.validator.Field.validate(Field.java:890)
       at org.apache.commons.validator.Form.validate(Form.java:174)
       at org.apache.commons.validator.Validator.validate(Validator.java:367)
       at org.apache.struts.validator.ValidatorForm.validate(ValidatorForm.java:112)
       at com.eis.sor.struts.action.impactnegation.ImpactNegationEditAction.save_onClick(ImpactNegationEditAction.java:84)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at com.cc.framework.adapter.struts.ActionUtil.handleFormAction(Unknown Source)
       at com.cc.framework.adapter.struts.FWAction.handleFormAction(Unknown Source)
       at com.cc.framework.adapter.struts.ActionUtil.execute(Unknown Source)
       at com.cc.framework.adapter.struts.FWAction.execute(Unknown Source)
       at com.eis.sor.struts.action.common.AbstractSorAction.execute(AbstractSorAction.java:30)
       at com.cc.framework.adapter.struts.FWAction.execute(Unknown Source)
       at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
       at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
       at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
       at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
    

    Ideas???

    #268143 Reply

    Riyad Kalla
    Member

    Moving to OT > Soft Dev

Viewing 2 posts - 1 through 2 (of 2 total)
Reply To: Struts validation fails

You must be logged in to post in the forum log in