facebook

Problem using Struts validation

💡
Our Forums Have Moved

For help with installation, bugs reports or feature requests, please head over to our new forums.
Genuitec Community on GitHub

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

    vinodnn
    Member

    Dear friends,
    I am a beginner in Struts. I have encountered a problem using Struts validation. I want to validate email id on form submission. I don’t know how to display the user-entered value in the validator message. i.e., for example, if I enter ‘abc’ in the e-mail field, it should display “abc is an invalid e-mail address.”

    Here is the code for my application:

    1.validation.xml

    
    <?xml version="1.0" encoding="ISO-8859-1" ?>
    
    <!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>
    
    <!--
         This is a minimal Validator form file with a couple of examples.
    -->
    
        <global>
    
        </global>
        <formset>
    
            <!-- Register.do-->
            <form name="Registration">
                <field
                    property="emailId"
                    depends="required,email">
                        <arg0 key="Registration.emailId"/>
                </field>
            </form>
        </formset>
    </form-validation>
    

    2.struts-config.xml

    
    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <!DOCTYPE struts-config PUBLIC
              "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
              "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
    <!--
         This is a blank Struts configuration file with an example
         welcome action/page and other commented sample elements.
    
         Tiles and the Struts Validator are configured using the factory defaults
         and are ready-to-use.
    
         NOTE: If you have a generator tool to create the corresponding Java classes
         for you, you could include the details in the "form-bean" declarations.
         Otherwise, you would only define the "form-bean" element itself, with the
         corresponding "name" and "type" attributes, as shown here.
    -->
    
    <struts-config>
    
    <!-- ================================================ Form Bean Definitions -->
        <form-beans>
            <form-bean name="Registration" type="org.apache.struts.validator.DynaValidatorForm">
                <form-property name="emailId" type="java.lang.String"/>
            </form-bean>
        </form-beans>
    <!-- =========================================== Global Forward Definitions -->
    
        <global-forwards>
            <forward name="Register" path="/Register.do"/>
            <forward name="RegisterConfirm" path="/RegisterConfirm.do"/>
    
        </global-forwards>
    
    <!-- =========================================== Action Mapping Definitions -->
    
        <action-mappings>
            <action 
                path="/Register" 
                forward="register.jsp"/>
            <action 
                path="/RegisterConfirm" 
                type="proj.RegUserAction" 
                name="Registration" 
                input="register.jsp" 
                scope="request"
                validate="true">
                <forward name="success" path="register_sub.jsp"/>
            </action>
            
        </action-mappings>
    
    <!-- ============================================= Controller Configuration -->
    
        <controller
           processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
    
    <!-- ======================================== Message Resources Definitions -->
    
        <message-resources parameter="MessageResources" />
    
    <!-- =============================================== Plug Ins Configuration -->
    
      <!-- ======================================================= Tiles plugin -->
      <!--
         This plugin initialize Tiles definition factory. This later can takes some
         parameters explained here after. The plugin first read parameters from
         web.xml, thenoverload them with parameters defined here. All parameters
         are optional.
         The plugin should be declared in each struts-config file.
           - definitions-config: (optional)
                Specify configuration file names. There can be several comma
                separated file names (default: ?? )
           - moduleAware: (optional - struts1.1)
                Specify if the Tiles definition factory is module aware. If true
                (default), there will be one factory for each Struts module.
                If false, there will be one common factory for all module. In this
                later case, it is still needed to declare one plugin per module.
                The factory will be initialized with parameters found in the first
                initialized plugin (generally the one associated with the default
                module).
                  true : One factory per module. (default)
                  false : one single shared factory for all modules
           - definitions-parser-validate: (optional)
                Specify if xml parser should validate the Tiles configuration file.
                  true : validate. DTD should be specified in file header (default)
                  false : no validation
    
          Paths found in Tiles definitions are relative to the main context.
      -->
    
        <plug-in className="org.apache.struts.tiles.TilesPlugin" >
    
          <!-- Path to XML definition file -->
          <set-property property="definitions-config"
                           value="/WEB-INF/tiles-defs.xml" />
          <!-- Set Module-awareness to true -->
          <set-property property="moduleAware" value="true" />
        </plug-in>
    
      <!-- =================================================== Validator plugin -->
    
      <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
        <set-property
            property="pathnames"
            value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
      </plug-in>
    </struts-config>
    

    3.MessageResources.properties

    
    # -- standard errors --
    errors.header=<UL>
    errors.prefix=<LI>
    errors.suffix=</LI>
    errors.footer=</UL>
    # -- validator --
    errors.invalid={0} is invalid.
    errors.maxlength={0} can not be greater than {1} characters.
    errors.minlength={0} can not be less than {1} characters.
    errors.range={0} is not in the range {1} through {2}.
    errors.required={0} is required.
    errors.byte={0} must be an byte.
    errors.date={0} is not a date.
    errors.double={0} must be an double.
    errors.float={0} must be an float.
    errors.integer={0} must be an integer.
    errors.long={0} must be an long.
    errors.short={0} must be an short.
    errors.creditcard={0} is not a valid credit card number.
    errors.email={0} is an invalid e-mail address.
    # -- other --
    errors.cancel=Operation cancelled.
    errors.detail={0}
    errors.general=The process did not complete. Details should follow.
    errors.token=Request could not be completed. Operation is not in sequence.
    
    # -- Custom Errors --
    Registration.emailId={0}//Is this wrong????
    

    What should I do to solve this problem?

    I have another doubt. If there is more than one field to be validated, during form submission, it should display the error of only the current field.

    #251324

    Riyad Kalla
    Member

    Moving to OT > Soft Dev

    You might want to look at some tutorials that use the validation stuff to see how they get started with some basic stuff like login names and passwords, then go from there.

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

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