在使用Spring
2.5.6.SEC01的Web应用程序中,我实际上具有一个Integer字段,该字段需要一个数字来确定要滚动到的页面。需求已更改,我们不再希望显示错误消息,而只是在用户输入无效数字(例如“
adfadf”)时忽略用户的输入。
我读到你可以通过以下方式做到这一点:
TypeMismatch.property =一些新的错误信息
但是,尝试过之后,我们仍然会收到原始错误消息:java.lang.Integer.TypeMismatch = …
我只想为该给定属性禁用此消息。我怎样才能做到这一点?我仍然希望绑定自动发生,只是现在不想听到它。
沃尔特
根据DefaultMessageCodesResolver
如果是代码“ typeMismatch”,则对象名称为“ user”,字段为“ age”
因此,您应该得到(我想您的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;
}
}
和一个人控制器
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 html" target="_blank">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");
}
});
}
由于我有,我的应用程序将提示所有URL的证书,那么对于一些特定的URL(如或)我是否可以绕过URL?
我的控制器是 我对Spring完全陌生,请帮帮我怎么做?
Csrf筛选器验证从“验证”提交的Csrf令牌,当我从HTTP向https提交请求时,抛出无效令牌异常(403)。如何在这种情况下禁用csrf令牌身份验证?
问题内容: 最近,存在一个问题,即如何在特定终端的Python Flask中禁用日志记录(跳过一个终端的Flask日志记录?)。 例如,这对于您不希望使日志混乱的情况是有道理的。 我为Flask解决了这个问题,但是当使用Gunicorn运行Flask时,我的解决方案不再起作用。 如何使用Gunicorn实现此目的?我想要常规的日志记录行为,但是没有端点的任何日志。 问题答案: 我想通了-您需要重写
我需要忽略PKIX路径构建异常 我知道如何通过编写我自己的类实现来做到这一点,在那里我总是从返回true。 但这是相当广泛的。我的代码将成为更大项目的一部分。我不想因为我更换了信托经理而影响其他一切。 我总是要连接到一个固定的域名,即“www.myws.com”。我只想忽略连接到“www.myws.com”的。 这样的事情可能吗?
我是Jface数据绑定的新手。我正在尝试使用数据绑定来生成表。当任何一个人单击row时,在映射的文本字段中显示vales。当我这样做时,我得到异常。(“java.lang.IllegalArgumentException:Could not find property with name in class class com.swt.pro.model.employee”)下面是类结构。 我有3个
我有一个基于Spring Boot的库(使用spring-data-mongo),它创建了一个bean。碰巧实现了
问题内容: 我想捕获一个特定的ValueError,而不仅仅是任何ValueError。 我试过这样的事情: 但这会引发SyntaxError:无法分配给文字。 然后我尝试了: 但这引发了例外,即使这是我要避免的例外。 问题答案: 在,是例外,而不是字符串的实例。因此,当您测试不等于特定字符串时,该测试始终为False。尝试: 代替。 例: 通常,如果可以帮助您,您并不是真的希望依赖于错误消息-这