我正在使用 Struts2 开发 JAVA EE 项目,我有一个包含字段名称和年龄的 jsp 文件:
<s:form action="login" method="post">
<s:textfield name="name" label="Name" size="20" />
<s:textfield name="age" label="Age" size="20" />
<s:submit label="Submit" align="center" />
</s:form>
还有带有注释 validator 的适当操作:
public class LoginAction extends ActionSupport {
private String name;
private int age;
@Override
public String execute() throws Exception {
return SUCCESS;
}
@RequiredStringValidator(message = "String is empty", shortCircuit=true)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ConversionErrorFieldValidator(message = "Please enter a valid age", shortCircuit = true)
@IntRangeFieldValidator(message = "Age must be in between 28 and 65",min = "29", max = "65",shortCircuit=true)
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
一切正常,但是当我输入无效的年龄(例如“test”的字符串)时,我会收到消息,即我在注释中设置的消息“请输入有效的年龄”另一个默认情况下“字段“年龄”的字段值无效”,所以我的问题是如何停用第二条消息。
编辑:这张图片显示了我收到的消息。
谢谢。
最佳答案
首先阅读how the workflow about conversion and validation errors works ;
然后阅读Type Conversion Error Handling来自官方文档(粗体是我的):
Type conversion error handling provides a simple way to distinguish between an input validation problem and an input type conversion problem.
Any error that occurs during type conversion may or may not wish to be reported. For example, reporting that the input "abc" could not be converted to a number might be important. On the other hand, reporting that an empty string, "", cannot be converted to a number might not be important - especially in a web environment where it is hard to distinguish between a user not entering a value vs. entering a blank value.
By default, all conversion errors are reported using the generic i18n key
xwork.default.invalid.fieldvalue
, which you can override (the default text is Invalid field value for field "xxx", where xxx is the field name) in your global i18n resource bundle.However, sometimes you may wish to override this message on a per-field basis. You can do this by adding an i18n key associated with just your action (Action.properties) using the pattern
invalid.fieldvalue.xxx
, wherexxx
is the field name.It is important to know that none of these errors are actually reported directly. Rather, they are added to a map called conversionErrors in the ActionContext. There are several ways this map can then be accessed and the errors can be reported accordingly.
There are two ways the error reporting can occur:
- Globally, using the Conversion Error Interceptor
- On a per-field basis, using the conversion validator
By default, the conversion interceptor is included in struts-default.xml in the default stack. To keep conversion errors from reporting globally, change the interceptor stack, and add additional validation rules.
那么您的情况是怎样的呢?
您正在使用默认堆栈(或具有相同行为的自定义堆栈),该堆栈在验证拦截器之前运行 ConversionErrors 拦截器。
然后,由于您正在定义 ConversionErrorFieldValidator
,因此您正在使用两种机制,而您应该只使用其中一种机制。
您可以采用以下三种方式中的任何一种:
全局资源方式
- 删除您的
ConversionErrorFieldValidator
; 创建全局 i18n 资源包并添加 key
xwork.default.invalid.fieldvalue = Please enter a valid "{0}"
本地资源方式
- 删除您的
ConversionErrorFieldValidator
; 创建一个操作资源包(针对具有此需求的每个操作/字段)并添加
invalid.fieldvalue.age = Please enter a valid age
自定义方式
- 从堆栈中删除 ConversionError 拦截器,以执行所有需要此操作的操作。
使用前两个解决方案,您不能将其短路,然后您将同时遇到转换和(第一个,如果短路) validator 错误。但这些值将被保留并自动重新填充(例如,将 abc
插入 int
字段,将在页面中重新填充 abc
)。
使用最后一个解决方案,您的消息将被短路,但您必须手动处理字段的重新填充,并且您需要记住为每个字段使用自定义的@ConversionErrorFieldValidator
,因为除非转换错误消息会被吞掉。
就我个人而言,我会选择解决方案 1。它是最安全的,而且基本为零工作成本。
关于java - Struts2 : How do I deactivate the default message of FieldValidators,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26331985/