java - Struts2 : How do I deactivate the default message of FieldValidators

标签 java struts2 validation


我正在使用 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”的字符串)时,我会收到消息,即我在注释中设置的消息“请输入有效的年龄”另一个默认情况下“字段“年龄”的字段值无效”,所以我的问题是如何停用第二条消息。

编辑:这张图片显示了我收到的消息。

enter image description here

谢谢。

最佳答案

首先阅读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, where xxx 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:

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/

相关文章:

java - 在 XmlAdapter 中使用 EJB

java - Android 应用程序在某些手机上强制半重启/崩溃

java - 使用二进制搜索在排序的多维数组中查找数字

java - 创建maven项目出错

java - 在 Struts 2 中的 request.getAttribute(..) 中获取 Null

javascript - 只允许 contenteditable 元素中的数字?

css - 使用CSS自动添加 'required field'星号形成输入

java - 使用 InjectionPoint 在注入(inject)和检索时传递参数

java - 如何将自定义 validator 与 dropwizard 一起使用?

json - 如何使用Struts 2 json插件在json响应中包括父类字段