gpt4 book ai didi

java - Spring - 禁用绑定(bind)异常(针对特定属性)

转载 作者:搜寻专家 更新时间:2023-10-31 20:16:12 25 4
gpt4 key购买 nike

在我正在使用 Spring 2.5.6.SEC01 的 Web 应用程序中,我基本上有一个整数字段,它使用一个数字来确定要滚动到哪个页面。要求发生了变化,我们不再希望显示错误消息,而是在用户输入无效数字(例如“adfadf”)时忽略用户的输入。

我在读到您可以通过以下方式做到这一点:

TypeMismatch.property=一些新的错误信息

但是,在尝试之后,我们仍然收到原始错误消息:java.lang.Integer.TypeMismatch=...

我只想为给定的属性禁用此消息。我怎样才能做到这一点?我仍然希望绑定(bind)自动发生,我只是不想现在听到它。

沃尔特

最佳答案

根据 DefaultMessageCodesResolver

如果代码“typeMismatch”,对象名称“user”,字段“age”

  • typeMismatch.user.age
  • typeMismatch.age
  • 类型不匹配.int
  • 类型不匹配

所以你应该得到(我想你的 commandName 被称为 command 并且你的属性是 age)根据你的代码进行调整

typeMismatch.command.age
typeMismatch.age
typeMismatch.java.lang.Integer
typeMismatch

注意第三段代码

typeMismatch.java.lang.Integer

它会解决你想要的

更新

我创建了一个 Person 命令类

public class Person implements Serializable {

private Integer age;

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

}

和个人 Controller

public class PersonController extends SimpleFormController {

public PersonController() {
setCommandClass(Person.class);
setValidator(new Validator() {
public boolean supports(Class clazz) {
return clazz.isAssignableFrom(Person.class);
}

public void validate(Object command, Errors errors) {
rejectIfEmpty(errors, "age", "Age is required");
}
});
}

@Override
protected ModelAndView onSubmit(Object command) throws Exception {
return new ModelAndView();
}

}

这是我的 myMessages.properties(类路径的根目录)

typeMismatch.command.age=typeMismatch.command.age
typeMismatch.age=typeMismatch.age
typeMismatch.java.lang.Integer=typeMismatch.java.lang.Integer
typeMismatch=typeMismatch

所以,我做了下面的测试

public class PersonControllerTest {

private PersonController personController;
private MockHttpServletRequest request;

private MessageSource messageSource;

@Before
public void setUp() {
request = new MockHttpServletRequest();
request.setMethod("POST");

personController = new PersonController();

messageSource = new ResourceBundleMessageSource();
((ResourceBundleMessageSource) messageSource).setBasename("myMessages");
}

@Test
public void failureSubmission() throws Exception {
/**
* Ops... a bindException
*
* Age can not be a plain String, It must be a plain Integer
*/
request.addParameter("age", "not a meaningful age");

ModelAndView mav = personController.handleRequest(request, new MockHttpServletResponse());

BindingResult bindException = (BindingResult) mav.getModel().get(BindingResult.MODEL_KEY_PREFIX + "command");
for (Object object : bindException.getAllErrors()) {
if(object instanceof FieldError) {
FieldError fieldError = (FieldError) object;

assertEquals(fieldError.getField(), "age");

/**
* outputs typeMismatch.command.age
*/
System.out.println(messageSource.getMessage((FieldError) object, null));
}
}
}

}

如果你想要第二个,你必须摆脱 typeMismatch.command.age 关键资源包

typeMismatch.age=typeMismatch.age
typeMismatch.java.lang.Integer=typeMismatch.java.lang.Integer
typeMismatch=typeMismatch

或者自己编写 MessageCodesResolver 的实现

public class MyCustomMessageCodesResolver implements MessageCodesResolver {

private DefaultMessageCodesResolver defaultMessageCodesResolver = new DefaultMessageCodesResolver();

public String [] resolveMessageCodes(String errorCode, String objectName) {
if(errorCode.equals("age"))
/**
* Set up your custom message right here
*/
return new String[] {"typeMismatch.age"};

return defaultMessageCodesResolver.resolveMessageCodes(String errorCode, String objectName);
}

public void String[] resolveMessageCodes(String errorCode, String objectName, String field, Class fieldType) {
if(errorCode.equals("age"))
/**
* Set up your custom message right here
*/
return new String[] {"typeMismatch.age"};

return defaultMessageCodesResolver.resolveMessageCodes(String errorCode, String objectName, String field, Class fieldType);
}
}

并设置你的 PersonController

public class PersonController extends SimpleFormController {

public PersonController() {
setMessageCodesResolver(new MyCustomMessageCodesResolver());
setCommandClass(Person.class);
setValidator(new Validator() {
public boolean supports(Class clazz) {
return clazz.isAssignableFrom(Person.class);
}

public void validate(Object command, Errors errors) {
rejectIfEmpty(errors, "age", "Age is required");
}
});
}

关于java - Spring - 禁用绑定(bind)异常(针对特定属性),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2835037/

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