7.Validator FrameWork : "errors.required" Rule

1.Required 

<formset>
        
        <form name="LoginForm" >
            <field property="uname" depends="required" >
                <arg position="0" key="l.uname"/>            
                
            </field>
                  
    </formset>



The key that we provide for arg0 in the validation.xml file definitions will substitute the proper value in place of {0} 



if an error occurs. (In the ApplicationResources file you will see label.user and label.pwd defined and those values will be substituted for {0} in the appropriate validation). 





________________________________________________________
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>

<%-- 
    Document   : login
    Created on : Oct 12, 2013, 11:47:04 AM
    Author     : Satya<satyanarayana@greenbuds.co.in & satyajohnny@live.com>
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Login</h1>
        <html:errors/>
            
        <html:form action="login">
            <bean:message key="l.uname"/><html:text property="uname"/>
            <bean:message key="l.pwd"/><html:password property="pwd"/>
            <html:submit value="OK"/>
        </html:form>
    </body>
</html>
___________________________________________

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Satya<satyanarayana@greenbuds.co.in & satyajohnny@live.com>
 */
public class LoginAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
LoginForm ob =  (LoginForm) form;
        System.out.println("iam __________________ excute");

if(ob.getUname().toString().equalsIgnoreCase("a")&&ob.getPwd().toString().equalsIgnoreCase("a"))
{
    System.out.println("iam ok Before ");
return mapping.findForward("ok");
}else{
     System.out.println("iam Fsail");
return mapping.findForward("fail");
}
    }
}
___________________________________________________________________________________

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts;

/**
 *
 * @author Satya<satyanarayana@greenbuds.co.in & satyajohnny@live.com>
 */
public class LoginForm extends org.apache.struts.validator.ValidatorForm {
    
    private String uname,pwd;

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
     
}





___________________________________________________________________________________

# Resource Bundle file.
#
key=value
l.uname = Username
l.pwd = Password
errors.required = {0} is Required
___________________________________________________________________________________
<?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>

    <!--
         This is a minimal Validator form file with a couple of examples.
    -->

    <global>

        <!-- An example global constant
        <constant>
            <constant-name>postalCode</constant-name>
            <constant-value>^\d{5}\d*$</constant-value>
        </constant>
        end example-->

    </global>

    <formset>
        
        <form name="LoginForm" >
            <field property="uname" depends="required" >
                <arg position="0" key="l.uname"/>            
                
            </field>
            
            <field property="pwd" depends="required" >
                <arg position="0" key="l.pwd"></arg>
                
                
            </field>
            
            
            
        </form>
        
        
    </formset>

</form-validation>
___________________________________________________________________________________
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">


<struts-config>
    <form-beans>
        <form-bean name="LoginForm" type="com.myapp.struts.LoginForm"  />
         
    
    </form-beans>
    
    <global-exceptions>
    
    </global-exceptions>

    <global-forwards>
        <forward name="ok"  path="/ok.jsp"/>
        <forward name="fail"  path="/login.jsp"/>
    </global-forwards>

    <action-mappings>
        <action input="/login.jsp" name="LoginForm" path="/login" scope="session" type="com.myapp.struts.LoginAction" validate="true"/>
        <action path="/Welcome" forward="/welcomeStruts.jsp"/>
    </action-mappings>
    
    <controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>

    <message-resources parameter="com/myapp/struts/ApplicationResource"/>     <message-resources parameter="com/myapp/struts/satya"/>    
    
   
    <plug-in className="org.apache.struts.tiles.TilesPlugin" >
        <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />      
        <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>

____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Post a Comment

Thank You

Previous Post Next Post