我如何检查用户是否正确地将前面的表单提交到考虑了一些步骤的SpringMVC应用程序中?
我在春季MVC是个新手,我有以下情况 我正在开发一个SpringMVC应用程序,它实现了一个用户注册过程。该过程分为4个步骤。在每个步骤中,用户将一些信息插入到表单中,表单将被提交,并由相关方法处理到控制器类中。这些控制器方法中的每一个都采用包含所提交表单信息的相关命令对象 所以我有这样的想法:我如何检查用户是否正确地将前面的表单提交到考虑了一些步骤的SpringMVC应用程序中?,spring,spring-mvc,architecture,Spring,Spring Mvc,Architecture,我在春季MVC是个新手,我有以下情况 我正在开发一个SpringMVC应用程序,它实现了一个用户注册过程。该过程分为4个步骤。在每个步骤中,用户将一些信息插入到表单中,表单将被提交,并由相关方法处理到控制器类中。这些控制器方法中的每一个都采用包含所提交表单信息的相关命令对象 所以我有这样的想法: @Controller public class RegistrazioneController { // This is the first step and show a view tha
@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";
}
}
所以它工作得很好。我的问题是,当进入注册步骤时,申请必须检查之前的步骤是否已完成(之前的表格已编译并提交)
因此,我认为我必须这样做,例如:*当进入注册步骤3()时,我必须检查上一个注册步骤2()步骤方法的命令对象是否设置正确(它是有效的),因此这意味着用户已经完成了上一个注册步骤
应用程序必须防止用户尝试从一个步骤开始访问注册,而没有完成注册过程的前面步骤
实施此行为的最佳方式是什么?我曾参与过一些Sap Hybris项目,该平台建议使用以下流程: Step1Form、Step2Form和Step3Form,如果您的1步表单中有名字和姓氏,那么在Step1Form类中,您将拥有与属性相同的名字和姓氏 对于每个类创建一个验证器,在下一步中,如果前一步无效,控制器u必须验证前一步,并将用户重定向到前一步 您已经有了注册步骤1、注册步骤2和注册步骤3 让我们为注册创建验证程序步骤1:
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");
}
}
}
我曾参与过一些Sap Hybris项目,该平台建议使用以下流程: Step1Form、Step2Form和Step3Form,如果您的1步表单中有名字和姓氏,那么在Step1Form类中,您将拥有与属性相同的名字和姓氏 对于每个类创建一个验证器,在下一步中,如果前一步无效,控制器u必须验证前一步,并将用户重定向到前一步 您已经有了注册步骤1、注册步骤2和注册步骤3 让我们为注册创建验证程序步骤1:
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");
}
}
}
在我的例子中,特定步骤的command对象包含上一步骤的command对象作为子对象,因此您只需为RegistrationStep1、RegistrationStep2、,注册步骤3我将编辑我的应答在我的情况下,特定步骤的命令对象包含上一步骤的命令对象作为子对象,因此您只需为注册步骤1、注册步骤2创建验证程序,注册步骤3我将编辑我的答案尝试在上述情况下使用spring wizard controller,这是spring Web Flow的优点。在上述情况下使用spring wizard controller,这是spring Web Flow的优点