如何检查用户是否已将先前的表单正确提交到考虑某些步骤的 Spring MVC 应用程序中? [英] How can I check if the user have correctly submitted the previous form into a Spring MVC application that contemplate some steps?

查看:21
本文介绍了如何检查用户是否已将先前的表单正确提交到考虑某些步骤的 Spring MVC 应用程序中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Spring MVC 的新手,遇到以下情况.

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

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

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.

所以我有这样的事情:

@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).

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

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?

推荐答案

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

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

Step1Form、Step2Form 和 Step3Form,如果您在 1 步表单中有名字和姓氏,那么您在 Step1Form 类中将具有相同的属性.

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.

您已经有了 RegistrationStep1、RegistrationStep2 和 RegistrationStep3让我们为 RegistrationStep1 创建一个验证器:

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

这篇关于如何检查用户是否已将先前的表单正确提交到考虑某些步骤的 Spring MVC 应用程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆