How can I check if the user have correctly submitt

2019-09-11 02:50发布

问题:

I am pretty new in Spring MVC and I have the following situation.

I am working on a Spring MVC application that implement a user registration process. The prcess is divided into 4 steps. In each step the user insert some information into a form that is submitted and that is handled by the related method into the controller class. Each of these controller method take the related command object that contains the information of the submitted form.

So I have something like this:

@Controller
public class RegistrazioneController {

    // This is the first step and show a view that contain the first form:
    @RequestMapping(value = "/registrationStep1")
    public String registrationStep1(Model model) {
        return "/registrazione/registration-step1";
    }

    @RequestMapping(value = "/registrationStep2", method = RequestMethod.POST)
    public String registrationStep2(@ModelAttribute RegistrationStep1 registrationStep1, Model model) throws APIException {
        .......................................................
        .......................................................
        .......................................................
        return "/registrazione/registration-step2";
     }

    @RequestMapping(value = "/registrationStep3", method = RequestMethod.POST)
    public String registrationStep3(@ModelAttribute RegistrationStep3 registrationStep3, Model model) throws APIException {
        .......................................................
        .......................................................
        .......................................................
        return "/registrazione/registration-step3";
     }

     // This method return the final view after the completation of the user registration:
     @RequestMapping(value = "/registrationStep4", method = RequestMethod.POST)
    public String registrationStep2(@ModelAttribute RegistrationStep4 registrationStep4, Model model) throws APIException {
        .......................................................
        PERFORM THE USER REGISTRATION
        .......................................................
        return "/registrazione/registration-step4";
     }

}

So it works pretty fine. My problem is that the application have tho check that, when enter into a registration step, the previous steps are completed (the previous form was compiled and submitted).

So I think that I have to do something like this, for example: ** when enter into the registrationStep3() have to check if the command object of the previous registrationStep2() step method was correctly setted (it is valid), so it means that the user have completed the previous registration step.

The application have to prevent that the user try to acces the registration starting from a step without having complete the previous steps of the registration process.

What is the best way to implement this behavior?

回答1:

I have worked in some Sap Hybris projects and this platform suggest to use the following process :

Step1Form, Step2Form and Step3Form, if you have first name and last name in your 1 step form you ll have the same in Step1Form class as attributes.

and for each class create a validator, in the next step controller u have to validate the previous step if it is not valid redirect the user to the previous step.

you already have RegistrationStep1, and RegistrationStep2 and RegistrationStep3 lets create a validator for RegistrationStep1 :

import org.apache.commons.validator.routines.EmailValidator;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

@Component(value = "registrationStep1Validator")
public class RegistrationStep1Validator implements Validator
{

    @Override
    public boolean supports(final Class<?> aClass)
    {
        return RegistrationStep1.class.equals(aClass);
    }

    @Override
    public void validate(final Object object, final Errors errors)
    {
        final RegistrationStep1 step1= (RegistrationStep1) object;
        final String name = step1.getName();
        final String email = step1.getEmail();

        if (email.isEmpty or email == null)
        {
            errors.reject("email", "Email must not be blank or null");
        }

        if (name.isEmpty or name== null)
        {
            errors.reject("name", "Name must not be blank");
        }



        if (!EmailValidator.getInstance().isValid(email))
        {
            errors.reject("email", "Email must be valid");
        }
    }

}

//later in your controller

@RequestMapping(value = "/registrationStep2", method = RequestMethod.POST)
    public String registrationStep2(@ModelAttribute RegistrationStep1 registrationStep1,final BindingResult bindingResult, Model model) {

        registrationStep1Validator.validate(registrationStep1,bindingResult);
        if (bindingResult.hasErrors())
        {
            return "/registrazione/registration-step1";
        }
        return "/registrazione/registration-step2";
     }