简体   繁体   English

Struts2:如何停用FieldValidators的默认消息

[英]Struts2 : How do I deactivate the default message of FieldValidators


I'm working on JAVA EE project using Struts2, which i have a jsp file with to fields name & age: 我正在使用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>

And there apropriat Action With annotation validators: 还有带有注释验证器的适当操作:

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;
    }
}

Everything works fine, but when i enter a not valid age (a string for example "test") i get to messages, the one i set in the annotation "Please enter a valid age" and an other one by default "Invalid field value for field "age"" , so my question is how can i desactive it the second message. 一切正常,但是当我输入无效的年龄(例如字符串“ test”)时,我收到消息,我在注释“请输入有效的年龄”中设置了一个,默认情况下另一个在“无效字段值”中设置对于字段“ age”” ,因此我的问题是如何停用第二条消息。

Edit: This picture shows the messages i get. 编辑:这张图片显示了我收到的消息。

在此处输入图片说明

Thank you. 谢谢。

Firs read how the workflow about conversion and validation errors works ; 冷杉阅读有关转换和验证错误的工作流程的工作原理 ;

Then read Type Conversion Error Handling from the official documentation (bolds are mine): 然后从官方文档中阅读类型转换错误处理 (粗体字属于我):

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. 例如,报告输入的“ abc”无法转换为数字可能很重要。 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. 默认情况下,所有转换错误都使用通用i18n键xwork.default.invalid.fieldvalue ,您可以在全局i18n中覆盖该键(默认文本是“ xxx”字段的无效字段值,其中xxx是字段名称)资源包。

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. 您可以通过使用模式invalid.fieldvalue.xxx (其中xxx是字段名称)添加仅与您的动作(Action.properties)相关联的i18n键来实现。

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. 而是将它们添加到ActionContext中的一个名为conversionErrors的映射中。 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. 默认情况下,转换拦截器包含在默认堆栈的struts-default.xml中。 To keep conversion errors from reporting globally, change the interceptor stack, and add additional validation rules. 为了防止转换错误全局报告,请更改拦截器堆栈,并添加其他验证规则。

Then what is happening in your case ? 那么您的情况又如何呢?

You are using a default stack (or a custom stack with the same behaviour), that is running the ConversionErrors Interceptor before the Validation Interceptor. 您正在使用默认堆栈(或具有相同行为的自定义堆栈),该堆栈在Validation Interceptor 之前运行ConversionErrors Interceptor。

Then, since you are defining a ConversionErrorFieldValidator , you are using both the mechanisms, while you should use only one of them. 然后,由于定义了ConversionErrorFieldValidator ,因此您将同时使用这两种机制,而应仅使用其中一种。

You can go with any of the three following way: 您可以采用以下三种方式中的任何一种:

Global resource way 全球资源方式

  • remove your ConversionErrorFieldValidator ; 删除您的ConversionErrorFieldValidator ;
  • create a global i18n resource bundle and add the key 创建一个全局i18n资源束并添加密钥

     xwork.default.invalid.fieldvalue = Please enter a valid "{0}" 

Local resource way 本地资源方式

  • remove your ConversionErrorFieldValidator ; 删除您的ConversionErrorFieldValidator ;
  • create an action resource bundle (for each action / field with this need) and add 创建一个动作资源包(针对有此需要的每个动作/字段)并添加

     invalid.fieldvalue.age = Please enter a valid age 

Custom way 定制方式

  • remove the ConversionError Interceptor from your stack, for all the actions with this need. 对于需要执行的所有操作,请从您的堆栈中删除ConversionError Interceptor。

With the first two solutions, you can't shortcircuit it, then you will have both the conversion AND the (first, if short-circuit) validator error. 使用前两种解决方案时,您无法使其短路,那么将同时发生转换和(首先是短路)验证器错误。 But the values will be preserved and repopulated automatically (eg. inserting abc into an int field, will repopulate abc in the page). 但是这些值将被保留并自动填充(例如,将abc插入int字段中,将在页面中重新填充abc )。

With the last solution, your message will be shortcircuited, but you will have to handle manually the repopulation of the fields, AND you will need to remember to use a custom @ConversionErrorFieldValidator for every field, because unless the conversion error messages will be swallowed. 使用最后一种解决方案,您的消息将被短路,但是您将必须手动处理字段的重新填充,并且您将需要记住为每个字段使用自定义@ConversionErrorFieldValidator ,因为除非吞下转换错误消息。

Personally, I would go with solution 1. It is the safest, and it costs basically zero work. 就我个人而言,我会选择解决方案1。这是最安全的方法,而且工作成本基本上为零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM