gpt4 book ai didi

spring-mvc - Spring MVC Controller,如何在清空表单值的同时保持BindingResult错误

转载 作者:行者123 更新时间:2023-12-03 11:32:33 25 4
gpt4 key购买 nike

我有一个使用Spring MVC Controller的Web表单。该表格已通过Spring验证。
当出现验证错误时,Spring将显示相同的表单,并预先填写用户输入的值和验证错误。

出于安全原因,我不希望表单中预先填写用户输入的值,但是我确实需要显示验证错误。

我怎样才能做到这一点?

通过查看Spring MVC源代码并查看BINDING_RESULT_KEY是如何构建的,我已经实现了此行为。这是源代码。

但是,这是一个hack,它可能会停止在Spring MVC的新版本上工作。

我如何正确地做到这一点?

package com.nespresso.ecommerce.naw.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.ObjectError;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.validation.Valid;


@Controller
@RequestMapping("/my_form")
public class MyFormController extends FrontEndController {
final String MY_FORM_OBJECT_NAME = "myForm";
final String BINDING_RESULT_KEY = BindingResult.MODEL_KEY_PREFIX + "myForm";

@RequestMapping(method = RequestMethod.POST)
public String post(@Valid @ModelAttribute("myForm") MyForm myForm, Errors errors, Model model) {
if (errors.hasErrors()) {
emptyMyFormWhileKeepingFormErrors(model);
return "my_form";
}

return "redirect:/confirmation";
}

@ModelAttribute("myForm")
public MyForm myForm() {
return new MyForm();
}

private void emptyMyFormWhileKeepingFormErrors(Model model) {
BeanPropertyBindingResult bindingResult = (BeanPropertyBindingResult) model.asMap().get(BINDING_RESULT_KEY);
if (bindingResult == null) {
return;
}
MyForm emptyForm = myForm();

// set the empty form, so the form is not pre-filled with the previous values.
// However, this clears the validation errors also
model.addAttribute(MY_FORM_OBJECT_NAME, emptyForm);

// re-attach the validation errors, and empty the rejectedValue
BeanPropertyBindingResult updatedBindingResult = new BeanPropertyBindingResult(emptyForm, MY_FORM_OBJECT_NAME);
for (ObjectError oe : bindingResult.getAllErrors()) {
if (!(oe instanceof FieldError)) {
updatedBindingResult.addError(oe);
} else {
FieldError fieldError = (FieldError) oe;

String rejectedValue = null; // that's the point, create a copy of the FieldError, emptying the rejectedValue;

FieldError updatedFieldError = new FieldError(
MY_FORM_OBJECT_NAME,
fieldError.getField(),
rejectedValue,
fieldError.isBindingFailure(),
fieldError.getCodes(),
fieldError.getArguments(),
fieldError.getDefaultMessage());
updatedBindingResult.addError(updatedFieldError);
}
}

model.addAttribute(BINDING_RESULT_KEY, updatedBindingResult);
}
}

最佳答案

编辑:添加另一个解决方案

您可以拥有一个空的模型对象,同时只需通过org.springframework.beans.BeanUtils将新值(来自新初始化的对象)复制到模型属性中,即可保留先前的错误:

@RequestMapping(method = RequestMethod.POST)
public String post(@Valid @ModelAttribute("myForm") MyForm myForm, Errors errors, Model model) {
if (errors.hasErrors()) {
MyForm emptyForm = new MyForm();
BeanUtils.copyProperties(emptyForm, myForm);
return "my_form";
}

return "redirect:/confirmation";
}

这样,您仍然可以显示以前的错误,并且表单为空。

还有一个警告:如果在使用 <input>字段时工作正常,但在使用 <form:input>拒绝的值的 errors Spring 增强字段中效果不佳。

如果您希望继续使用 <form:input>字段,则必须创建另一个 BindingResult对象并使用当前错误对其进行初始化,只需将拒绝值设置为null。您甚至有可能重置所有字段或仅重置错误的字段:
@RequestMapping(value = "/form", method=RequestMethod.POST)
public String update(@Valid @ModelAttribute Form form, BindingResult result, Model model) {
if (result.hasErrors()) {
// uncomment line below to reset all fields - by default only offending ones are
//form = new Form();
BeanPropertyBindingResult result2 = new BeanPropertyBindingResult(form, result.getObjectName());
for(ObjectError error: result.getGlobalErrors()) {
result2.addError(error);
}
for (FieldError error: result.getFieldErrors()) {
result2.addError(new FieldError(error.getObjectName(), error.getField(), null, error.isBindingFailure(), error.getCodes(), error.getArguments(), error.getDefaultMessage()));
}
model.addAllAttributes(result2.getModel());
return "my_form";
}

return "redirect:/confirmation";
}

关于spring-mvc - Spring MVC Controller,如何在清空表单值的同时保持BindingResult错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25326591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com