Contact Us
  
 
 
    SearchSearch   RegisterRegister   Log inLog in 

DynaForm validation problem in struts not working

DynaForm validation problem in struts not working

 
Post new topic   Reply to topic     Forum Index -> Java,Struts framework, Velocity, JSF, Hibernate, Servlet, JSP, javaBeans
View previous topic :: View next topic  
Author Message
mitoon



Joined: 01 Mar 2007
Posts: 2

PostPosted: Thu Mar 01, 2007 1:42 pm    Post subject: DynaForm validation problem in struts not working Reply with quote




hi,
i am software programmer. I m new to struts little. I am developing a application in struts. Struts provides in built validation facility to catch client errors. I also tried to use validation framework. But it is not working properly. I want to use with dynaform with dynaform validation it tried with subclass of DynaValidatorActionForm and DynaValidatorForm, But no output

i have use dynaform
Code:
<form-bean name="Report" type="org.apache.struts.validator.DynaValidatorActionForm">
      <form-property name="location" type="java.lang.String" />
      <form-property name="Stock" type="java.lang.String" />
      <form-property name="method" type="java.lang.String" />
    </form-bean>


and then i did
in action

Code:
<action
      input="Report.jsp"
      path="/Report"
      name="Report"
      scope="request"
      parameter="method"
      type="com.actions.TransferAction">
      <forward name="success" path="/Report.jsp"/>
     
   </action>


after it i made a action class


Code:
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

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.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.LookupDispatchAction;
import org.apache.struts.validator.DynaValidatorActionForm;


public class TransferAction extends DynaValidatorActionForm {
   public ActionForward execute(
         ActionMapping mapping,
         ActionForm form,
         HttpServletRequest request,
         HttpServletResponse response) {
   
         HttpSession session = request.getSession(true);
         String target = "success";
                     DynaValidatorActionForm Report = (DynaValidatorActionForm)form;
                     return mapping.findForward(target);
      }   
}
   


also took plugin in struts-config.xml


Code:
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
        <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,
                                                  /WEB-INF/validation.xml"/>
      </plug-in>


and in validation.xml

Code:
 <formset>
      <form name="Report">
         <field property="location" depends="required">
            <arg0 key="error.location" />
         </field>
         <field property="Stock" depends="required">
             <arg0 key="error.Stock" />
          </field>
      </form>
</formset>


Still it is not working
it is showing error no instance can be created

help me!!!!!!!!!!!!!!!
Back to top
View user's profile Send private message
strutschamp



Joined: 04 Mar 2007
Posts: 4

PostPosted: Sun Mar 04, 2007 8:42 am    Post subject: DynaForm validation example with source code Reply with quote

It is difficult to handle validator framework, if you are new Very Happy to struts. I have also spent lot to time to learn it, so now finally I done it. I will give you step by step of using dyna form validation. It is simple server side dynaform action validation or and client side validation with source code.

1. Open struts-config.xml file and do entries
2. Make validation.xml file in WEB-INF folder
3. Create validator-rules.xml in WEB-INF folder.
4. Create action class according to your requirement.
5. Create ApplicationResources.properties file in your package.
6. Last finally create a jsp file where you want to show your errors.

This example using struts 1.2 jars. So do little care of DTD tld standards. And version of it struts 1.1 use different and struts 1.3 use different one.

1. create struts-config.xml

Code:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
  <data-sources />
  <form-beans >
    <form-bean name="DynaFormTest" type="org.apache.struts.validator.DynaValidatorForm">
      <form-property name="name" type="java.lang.String" />
      <form-property name="phone" type="java.lang.String" />
    </form-bean>

  </form-beans>

  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action
      attribute="DynaFormTest"
      input="input"
      name="DynaFormTest"
      path="/ValidateTest"
      scope="request"
      validate="true"
      type="com.sumit.struts.TestAction" >
       <forward name="input" path="/index.jsp" />
       <forward name="back" path="index.jsp" />
    </action>

  </action-mappings>
  <controller inputForward="true" />
  <message-resources parameter="com.sumit.struts.ApplicationResources" />
 
  <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
    <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
    <set-property property="stopOnFirstError" value="true" />
  </plug-in>
 
</struts-config>



2. In this step make validation.xml file in WEB-INF

Code:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN" "validator_1_0.dtd" >
<form-validation>
 <formset>
 
    <form name="DynaFormTest">
       
      <field property="name" depends="required">
            <msg name="required" key="error.name"/>
           
      </field>
      <field property="phone" depends="required,integer">
        <msg name="required" key="error.phone"/>
        <msg name="integer" key="error.phone.integer"/>
      </field>
     
    </form>

  </formset>
</form-validation>




3. Copy or create validator-rules.xml in WEB-INF, but this file create automatically when we true validate=�true� in action mapping tag in struts-config.xml


4. Create an action class according to your requirement.
But still I am giving a code as you can use it with using subclass of dynaform. I am using a package path com.sumit.struts

Code:
 

package com.sumit.struts;

import javax.servlet.http.HttpServletRequest;
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.action.ActionMapping;
import org.apache.struts.validator.DynaValidatorActionForm;
import org.apache.struts.validator.DynaValidatorForm;
import org.apache.struts.actions.ForwardAction;

public class TestAction extends DynaValidatorActionForm {
   
   public ActionForward execute(ActionMapping mapping, ActionForm form,
         HttpServletRequest request, HttpServletResponse response) {
      DynaValidatorActionForm DynaFormTest = (DynaValidatorActionForm) form;
      DynaFormTest.get("name");
      DynaFormTest.get("phone");
      return mapping.findForward("back");
   }
}




5. Create ApplicationResources.properties file for internationalize errors
This is also in com.sumit.struts package

Code:
 

   error.name= Name should not empty
   error.phone= Phone should not empty
   error.phone.integer=Phone should be integer




6. This is final and last step to use dynaform validation


This is server side validations


Code:
 

<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
 
<html>
   <head>
      <title>JSP for DynaValidatorActionForm form</title>
   </head>
   <body>
       
      <html:form action="/ValidateTest">
             name : <html:text property="name" /><html:errors property="name" /><br/>
            phone : <html:text property="phone"/><html:errors property="phone" /><br/>
         <html:submit/><html:cancel/>
         
      </html:form>
      
   </body>
</html>




If you want to do javascript client side validation. Then change this jsp page with this one


Code:


<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
 
<html>
   <head>
      <title>DynaValidatorActionForm form</title>
      <html:javascript formName="DynaFormTest" dynamicJavascript="true"/>
   </head>
   <body>
       
      <html:form action="/ValidateTest" onsubmit="return validateDynaFormTest(this)">
         name : <html:text property="name" /><br/>
         phone : <html:text property="phone"/><br/>
             
         <html:submit/><html:cancel/>
         
      </html:form>
      
   </body>
</html>



Now restart your tomcat and check the output good luck. Surprised Surprised
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Java,Struts framework, Velocity, JSF, Hibernate, Servlet, JSP, javaBeans All times are GMT
Page 1 of 1

 
Jump to:  
  Quick Post a Reply Topic
     
  Name (required)  
     
  Email Address (will not be published) (required)  
     
  Subject of topic (required)  
     
   
     
   
     
  Advertisement  
     
 
     


  More Links  
 
JSP Introduction
JSP Setup
Simple JSP Example
JSP Variable
Array Declaration in JSP
JSP Forms and User Input
Form Validation
JSP Cookies
JSP Session Object
Loop, content collection, iterator, conditional check in JSP
JSP Application Object
JSP Include
JSP Response Object
JSP Request Object
JSP Error Handling
JSP File Handling Object
JSP Action Tag
JSP Implicit Objects
JSP Declaration
JSP Directive
JDBC Introduction
JDBC Driver
JDBC Connection
JDBC Prepared/Statement
JDBC ResultSet
JDBC Close Connection object
JDBC Insert
JDBC Display Records
JDBC Update
JDBC Delete
JDBC Sorting
JDBC Query
JDBC Exception
JDBC Add Batch
JDBC Transaction/RollBack