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

查看:68
本文介绍了如何检查用户是否已将先前的表单正确提交到考虑了某些步骤的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()的命令对象步骤方法已正确设置(有效),这意味着用户已完成上一个注册步骤.

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天全站免登陆