Está en: Home >> ISValidator >> Examples >> Struts Completo
SourceForge Logo
ISValidator
Presentation
Architecture
Download
Examples
Changes
ISDirValidator
Presentation
In SF.net
Brief
Download
News
En Español
ISValidator

Página... Struts Completo

Examples of ISValidator

This page have one example of using ISValidator in Struts. You can look the rest of the examples in the distribution to download.

You have also the posibility to be informated of the new releases, bug, and everything about ISValidator in this group.

Situation

This is an example of the use of ISValidator with the Framework Struts of the Apache foundation. The Struts framework is focused in the flow control and the presentation and ISValidator is more focused in the data, and what we do with it: adquire, model, validate and process. In this situation the two products can be used together.

The ISValidator routines are still under developing, but it can be used with care.

Example Code

This example has been tested with Struts 1.0 and ISValidator 0.0.10

In this example we change the LogonForm of the example in Struts 1.0 (by Craig R. McClanahan) to use ISValidator

//added this imports
import com.inigoserrano.isvalidator.constraintsV01.*;
import com.inigoserrano.isvalidator.constraintsContainersV01.*;
import com.inigoserrano.isvalidator.constraintsExceptionsV01.*;
import com.inigoserrano.isvalidator.metaContainersV01.*;
import com.inigoserrano.isvalidator.inValidConstraintProcessors.*;

/**
 * Form bean for the user profile page.  This form has the following fields,
 * with default values in square brackets:
 * <ul>
 * <li><b>password</b> - Entered password value
 * <li><b>username</b> - Entered username value
 * </ul>
 *
 * @author Craig R. McClanahan
 * @version $Revision: 1.1.1.1 $ $Date: 2001/12/10 11:49:04 $
 */

public final class LogonForm extends ActionForm {

      private SimpleConstraintContainer password;
      private SimpleConstraintContainer username;

public String getPassword() {
	return (this.password.getValueToCheck());
}


public void setPassword(String password) {
try{
        this.password=new SimpleConstraintContainer(password,"password");
        this.password.addConstraint(new NotBlankConstraint());
} catch (InternalConstraintException e) {
        //do nothing
}
}

public String getUsername() {
	return (this.username.getValueToCheck());
}

public void setUsername(String username) {
try{
        this.username=new SimpleConstraintContainer(username,"username");
        this.username.addConstraint(new NotBlankConstraint());
} catch (InternalConstraintException e) {
        //do nothing
}
}


public void reset(ActionMapping mapping, HttpServletRequest request) {
        this.password = null;
        this.username = null;
}


public ActionErrors validate(ActionMapping mapping,
                                 HttpServletRequest request) {

        ActionErrors errors = new ActionErrors();
try{
        SimpleMetaContainer allParameters = new SimpleMetaContainer();
        allParameters.addContainer(password);
        allParameters.addContainer(username);
        if (!allParameters.checkConstraint()){
            allParameters.setInValidConstraintProcesor(new StrutsInValidConstraintProcesor(new StrutsInValidContraintProcesorContainer()));
            //return an ActionErrors
            return allParameters.getInValidConstraintProcesorContainer().getActionErrors();
        }else
        return errors;


}catch(InternalConstraintException e){
return null;
}
catch(ConstraintException e){
return null;
}
catch(InValidConstraintProcesorInternalException e){
return null;
}
}

}
                        

Code Description

Frist at all, the String have been sustituted by ConstraintContainers that modelized one data , in this case we use the SimpleConstraintContainer. In ISValidator it can be used other types of ConstraintContainers (for Command Line Argumets, Servlet Parameters, etc...) but here we use the SimpleConstraintContainer because it is the most generic and not expecialiced.

      private SimpleConstraintContainer password;
      private SimpleConstraintContainer username;
                        

The methods to get and set the values have been change also since now we haven't Strings When setting the values it also add the Constraint to each data, in this case the only constraint is that the data can be null or blank (The NotBlankConstraint also check the data to be not null) but it could be and EmailConstraint, RegularExpresionConstraint, etc...

public void setPassword(String password) {
try{
        this.password=new SimpleConstraintContainer(password);
        this.password.addConstraint(new NotBlankConstraint());
} catch (InternalConstraintException e) {
        //do nothing
}
}
                        

In the validation the changes are more strong. It has automatizated. Frist we used a metacontainer (a container of containers) that modelized all the data. We say to the metacontainer the two data to handle.

Second we check the data with its constraints.

If we have errors then we use one InValidConstraintProcessor to handle the parameters of the error.

In this case we use the StrutsInValidConstraintProcesor that return one string for each constraint not valid with the form: error.NameOfTheData.NameOfTheConstraint For example: error.username.NotBlankConstraint

So we must change the properties file

        SimpleMetaContainer allParameters = new SimpleMetaContainer();
        allParameters.addContainer(password);
        allParameters.addContainer(username);
        if (!allParameters.checkConstraint()){
            allParameters.setInValidConstraintProcesor(new StrutsInValidConstraintProcesor(new StrutsInValidContraintProcesorContainer()));
        }
        //return an ActionErrors
        return allParameters.getInValidConstraintProcesorContainer().getActionErrors();
                        

The process of validating data is more easy and automatizated.




Iñigo Serrano todos los derechos reservados.
Queda prohibida la reproducción total o parcial del contenido aquí expuesto.