如何检查用户是否已将上一个表单正确提交到考虑某些步骤的Spring MVC应用程序中?

时间:2016-04-15 10:05:11

标签: spring spring-mvc architecture

我是Spring MVC的新手,我有以下情况。

我正在开发一个实现用户注册过程的Spring MVC应用程序。该任务分为4个步骤。在每个步骤中,用户将一些信息插入到提交的表单中,并由相关方法处理到控制器类中。这些控制器方法中的每一个都采用包含提交表单信息的相关命令对象。

所以我有这样的事情:

@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";
     }

}

所以它工作得很好。我的问题是应用程序检查,当进入注册步骤时,前面的步骤已完成(前一个表单已编译并提交)。

所以我认为我必须做这样的事情,例如:**进入 registrationStep3()时必须检查前一个 registrationStep2()的命令对象步骤方法已正确设置(有效),因此表示用户已完成上一个注册步骤。

应用程序必须防止用户尝试从步骤开始访问注册,而无需完成注册过程的先前步骤。

实现此行为的最佳方法是什么?

1 个答案:

答案 0 :(得分:2)

我参与了一些Sap Hybris项目,该平台建议使用以下过程:

Step1Form,Step2Form和Step3Form,如果您的1步表单中有名字和姓氏,则Step1Form类中的名称与属性相同。

并为每个类创建一个验证器,在下一步控制器中,如果无效,则必须验证上一步,将用户重定向到上一步。

您已经拥有RegistrationStep1,以及RegistrationStep2和RegistrationStep3  让我们为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");
        }
    }

}

//稍后在您的控制器中

@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";
     }