girly49
girly49

Reputation: 57

validate form struts2 manage INPUT

I have a problem: upon validation of the form, I'll check in Java if the form is valid and if it is not valid, I return for errors.

Text of errors is displayed, but I can not validate my form and revive checking my form.

struts.xml :

<action name="validerInscription" class="utilisateurAction" method="validerInscription">
    <result name="success" type="redirect">home</result>
    <result name="error"   type="redirect">inscription</result>
    <result name="input"   type="redirect">inscription</result>
</action>

action:

public String validerInscription() {
    logger.info("VALIDATION INSCRIPTION");
    boolean isInscriptionReussie = false;

    if (verificationFormulaire() == true) {
        utilisateur.setImage("image/avatar/avatar1.png");
        isInscriptionReussie = utilisateurService.sauvegarderUtilisateur(utilisateur);
        session.put("user", utilisateur); // Ajouter utilisateur a la session
    } else {
        logger.info("--- INSCRIPTION => RETURN INPUT");
        return INPUT;
    }

    if (isInscriptionReussie) {
        logger.info("INSCRIPTION REUSSIE");
        return SUCCESS;
    } else {
        logger.info("INSCRIPTION ERROR");
        return ERROR;
    }
}

Upvotes: 1

Views: 397

Answers (1)

Andrea Ligios
Andrea Ligios

Reputation: 50203

You are completely bypassing the framework validation feature, that is ready-to-use, and works as follow:

When a JSP calls an action, the request passes through a stack of Interceptors before reaching the action; every interceptor does something, like setting parameters, validating them, etc..

If an interceptor has an error, it throws an exception, or an INPUT result, depending on the interceptor, and the action will NOT be reached, because the new response will start from that interceptor to the result mapped to the exception or the INPUT result.

The Validation Interceptor works with XML validation files, or validate() methods, or other ways, and if a fieldError is added (manually in the validate(), or automatically through XML or annotations), it will return the INPUT result.

The INPUT result should be a simple dispatcher result (default), not a redirect one.

Then you should do something like this:

<action name="validerInscription" class="utilisateurAction" method="validerInscription">
    <result name="success" >home.jsp</result>
    <result name="error"   >inscription.jsp</result>
    <result name="input"   >inscription.jsp</result>
</action>
// This will be run by the Validation Interceptor
public void validate(){
    logger.info("VALIDATION INSCRIPTION");
    // Here you need to do the checks done in verificationFormulaire(), 
    // adding fieldErrors in case they fails
    if (firstName==null){
        addFieldError("firstName", "First name can't be null");
    }
    if (lastName==null){
        addFieldError("lastName", "Last name can't be null");
    }
    // etc...
}

// If you reach this, validation has passed
public String validerInscription() {

    utilisateur.setImage("image/avatar/avatar1.png");
    session.put("user", utilisateur); // Ajouter utilisateur a la session

    if (utilisateurService.sauvegarderUtilisateur(utilisateur)) {
        logger.info("INSCRIPTION REUSSIE");
        return SUCCESS;
    } else {
        logger.info("INSCRIPTION ERROR");
        return ERROR;
    }
}

Upvotes: 1

Related Questions