hi all,

  I want to do Dynamic field check using validator framework.

I have done the following steps:

1.Add validator plugin in struts-config.xml

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

2.I have used index.jsp as my LOGIN page.

With the following code:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<html:html>
<HEAD>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="IBM Software Development Platform">
<META http-equiv="Content-Style-Type" content="text/css">
<LINK href="theme/Master.css" rel="stylesheet"
    type="text/css">
<TITLE></TITLE>
</HEAD>

<BODY>
<html:errors/>
<html:form action="/submitLogin.do">
    <TABLE border="0" cellpadding="0" cellspacing="0">
        <TBODY>
            <TR>
                <TD></TD>
                <TD></TD>
                <TD></TD>
                <TD></TD>
                <TD width="28"></TD>
                <TD width="54"></TD>
                <TD width="68"></TD>
                <TD width="77"></TD>
                <TD width="89"></TD>
                <TD width="59"></TD>
            </TR>
            <TR>
                <TD height="2"></TD>
                <TD height="2"></TD>
                <TD height="2"></TD>
                <TD height="2"></TD>
                <TD width="28" height="2"></TD>
                <TD width="54" height="2"></TD>
                <TD width="68" height="2"></TD>
                <TD width="77" height="2"></TD>
                <TD width="89" height="2"></TD>
                <TD width="59" height="2"></TD>
            </TR>
            <TR>
                <TD height="20"></TD>
                <TD height="20"></TD>
                <TD height="20"></TD>
                <TD height="20"></TD>
                <TD width="28" height="20"></TD>
                <TD width="54" height="20"></TD>
                <TD width="68" height="20"></TD>
                <TD width="77" height="20"></TD>
                <TD width="89" height="20"></TD>
                <TD width="59" height="20"></TD>
            </TR>
            <TR>
                <TD height="38"></TD>
                <TD height="38"></TD>
                <TD height="38"></TD>
                <TD height="38"></TD>
                <TD width="28" height="38"></TD>
                <TD width="54" height="38"></TD>
                <TD width="68" height="38"></TD>
                <TD width="77" height="38"></TD>
                <TD width="89" height="38"></TD>
                <TD width="59" height="38"></TD>
            </TR>
            <TR>
                <TD></TD>
                <TD></TD>
                <TD></TD>
                <TD></TD>
                <TD width="28"></TD>
                <TD width="54"></TD>
                <TD width="68">Name</TD>
                <TD width="77"><html:text property="username"></html:text></TD>
                <TD width="89"></TD>
                <TD width="59"></TD>
            </TR>
            <TR>
                <TD height="65"></TD>
                <TD height="65"></TD>
                <TD height="65"></TD>
                <TD height="65"></TD>
                <TD width="28" height="65"></TD>
                <TD width="54" height="65"></TD>
                <TD width="68" height="65">Password</TD>
                <TD width="77" height="65"><html:password 
property="password"></html:password></TD>
                <TD width="89" height="65"></TD>
                <TD width="59" height="65"></TD>
            </TR>
            <TR>
                <TD height="20"></TD>
                <TD height="20"></TD>
                <TD height="20"></TD>
                <TD height="20"></TD>
                <TD width="28" height="20"></TD>
                <TD width="54" height="20"></TD>
                <TD width="68" height="20"></TD>
                <TD width="77" height="20"></TD>
                <TD width="89" height="20"></TD>
                <TD width="59" height="20"></TD>
            </TR>
            <TR>
                <TD height="31"></TD>
                <TD height="31"></TD>
                <TD height="31"></TD>
                <TD height="31"></TD>
                <TD height="31" width="28"></TD>
                <TD height="31" width="54"></TD>
                <TD height="31" width="68"></TD>
                <TD height="31" width="77"></TD>
                <TD height="31" width="89"><html:submit 
value="Submit"></html:submit></TD>
                <TD height="31" width="59"></TD>
            </TR>
            <TR>
                <TD height="37"></TD>
                <TD height="37"></TD>
                <TD height="37"></TD>
                <TD height="37"></TD>
                <TD height="37" width="28"></TD>
                <TD height="37" width="54"></TD>
                <TD height="37" width="68"></TD>
                <TD height="37" width="77"></TD>
                <TD height="37" width="89"></TD>
                <TD height="37" width="59"></TD>
            </TR>
            <TR>
                <TD height="38"></TD>
                <TD height="38"></TD>
                <TD height="38"></TD>
                <TD height="38"></TD>
                <TD height="38" width="28"></TD>
                <TD height="38" width="54"></TD>
                <TD height="38" width="68"></TD>
                <TD height="38" width="77"></TD>
                <TD height="38" width="89"></TD>
                <TD height="38" width="59"></TD>
            </TR>
        </TBODY>
    </TABLE>
</html:form>
</BODY>
</html:html>

3.Now I have added submitLogin action in the struts-config.xml file 

<action-mappings>
    <action path="/submitLogin" scope="request" 
type="com.ibm.dynamicform.resources.LoginAction" 
    name="LoginForm" validate="false" input="/index.jsp">
    <forward name="success" path="/done.jsp"></forward>
    </action>

4.Now the LoginAction Action class is like this

package com.ibm.dynamicform.resources;


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.DynaValidatorForm;


public class LoginAction extends Action {

public ActionForward execute(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
    
    ActionForward forward=new ActionForward();
    DynaValidatorForm dynaform = (DynaValidatorForm) form;
    String username=(String)dynaform.get("username");
    String password=(String)dynaform.get("password");
    
    if(username.equalsIgnoreCase("")||(password.equalsIgnoreCase(""))){
        System.out.println("null username");
        forward=mapping.getInputForward();    
    }
    else{
        System.out.println(username+":"+password);
        forward=mapping.findForward("success");    
    }
    
    
    return forward;
    

}


}


5.My validation.xml looks like
<!DOCTYPE form-validation 
PUBLIC "-//Apache Software Foundation//
       DTD Commons Validator Rules
       Configuration 1.0//EN"
       "http://jakarta.apache.org/
      commons/dtds/validator_1_0.dtd">


<form-validation>
  <formset>
    <form name="LogonForm">
      <field property="username"
            depends="required">
       <arg0 key="prompt.username"/> 
      </field>
      <field property="password"
            depends="required">
        <arg0 key="prompt.password"/>
      </field>
    </form>
  </formset>
</form-validation>

6.My validator-rules.xml looks like

<!DOCTYPE form-validation PUBLIC
          "-//Apache Software Foundation//DTD Commons
           Validator Rules Configuration 1.0//EN"
          "http://jakarta.apache.org/commons/dtds/validator_1_0.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.ActionErrors,
                     javax.servlet.http.HttpServletRequest"
                msg="errors.required">
      <javascript>
        <![CDATA[
        function validateRequired(form) {
          var isValid = true;
          var focusField = null;
          var i = 0;
          var fields = new Array();
          oRequired = new required();
          for (x in oRequired) {
            var field = form[oRequired[x][0]];

            if (field.type == 'text' ||
                field.type == 'textarea' ||
                field.type == 'file' ||
                field.type == 'select-one' ||
                field.type == 'radio' ||
                field.type == 'password') {

              var value = '';

              // get field's value
              if (field.type == "select-one") {
                var si = field.selectedIndex;
                if (si >= 0) {
                  value = field.options[si].value;
                }
              } else {
                value = field.value;
              }

              if (trim(value).length == 0) { {
                if (i == 0) {
                  focusField = field;
                }
                fields[i++] = oRequired[x][1];
                isValid = false;
              }
            }
          }

          if (fields.length > 0) {
            focusField.focus();
            alert(fields.join('\n'));
          }

          return isValid;
        }

        // Trim whitespace from left and right sides of s.
        function trim(s) {
          return s.replace( /^\s*/, "" ).replace( /\s*$/, "" );
        }

        ]]>
      </javascript>
    </validator>
  </global>
</form-validation>

My web app is able to handle the login actions but no error is displayed in the 
index.jsp when i submit the form without username and password.

Please help me why I am getting this situation..
Also my ApplicationResources.properties looks like this
.....
# Optional header and footer for <errors/> tag.
#errors.header=<ul>
#errors.footer=</ul>
# Error messages for Validator framework validations
errors.required={0} is required.
errors.minlength={0} cannot be less than {1} characters.
errors.maxlength={0} cannot be greater than {2} characters.
errors.invalid={0} is invalid.
errors.byte={0} must be a byte.
errors.short={0} must be a short.
errors.integer={0} must be an integer.
errors.long={0} must be a long.0.   errors.float={0} must be a float.
errors.double={0} must be a double.
errors.date={0} is not a date.
errors.range={0} is not in the range {1} through {2}.
errors.creditcard={0} is not a valid credit card number.
errors.email={0} is an invalid e-mail address.

#ERROR KEYS

prompt.username=Username
prompt.password=Password
NOTE:i hve used <html:errors/> in my index.jsp where i am expecting the error 
to be displayed.


Warm Regards,
Atul 

                
---------------------------------
 All-new Yahoo! Mail - Fire up a more powerful email and get things done faster.

Reply via email to