简体   繁体   English

在Spring中自定义PropertyEditorSupport

[英]Customizing PropertyEditorSupport in Spring

I'm working with Spring 3.2. 我正在使用Spring 3.2。 In order to validate double values globally, I use CustomNumberEditor . 为了全局验证double值,我使用CustomNumberEditor The validation is indeed performed. 确实进行了验证。

But when I input a number like 1234aaa , 123aa45 and so forth, I expect the NumberFormatException to be thrown but it doesn't. 但是当我输入一个像1234aaa123aa45等数字时,我希望抛出NumberFormatException ,但事实并非如此。 The docs says, 文档说,

ParseException is caused, if the beginning of the specified string cannot be parsed 如果无法解析指定字符串的开头,则会导致ParseException

Therefore, such values as mentioned above are parsed up to they are represented as numbers and the rest of the string is then omitted. 因此,解析上述值,将它们表示为数字,然后省略字符串的其余部分。

To avoid this, and to make it throw an exception, when such values are fed, I need to implement my own Property Editor by extending the PropertyEditorSupport class as mentioned in this question . 为了避免这种情况,并使它抛出异常,当这些值被提供时,我需要通过扩展此问题中提到的PropertyEditorSupport类来实现我自己的Property Editor。

package numeric.format;

import java.beans.PropertyEditorSupport;

public final class StrictNumericFormat extends PropertyEditorSupport
{
    @Override
    public String getAsText() 
    {
        System.out.println("value = "+this.getValue());
        return ((Number)this.getValue()).toString();
    }

    @Override
    public void setAsText(String text) throws IllegalArgumentException 
    {
        System.out.println("value = "+text);
        super.setValue(Double.parseDouble(text));
    }
}

The editors I have specified inside a method annotated with the @InitBinder annotation are as follows. 我在使用@InitBinder注释注释的方法中指定的编辑器如下所示。

package spring.databinder;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.Format;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.context.request.WebRequest;

@ControllerAdvice
public final class GlobalDataBinder 
{
    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request)
    {
        DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        dateFormat.setLenient(false);
        binder.setIgnoreInvalidFields(true);
        binder.setIgnoreUnknownFields(true);
        //binder.setAllowedFields("startDate");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));

        //The following is the CustomNumberEditor

        NumberFormat numberFormat = NumberFormat.getInstance();
        numberFormat.setGroupingUsed(false);
        binder.registerCustomEditor(Double.class, new CustomNumberEditor(Double.class, numberFormat, false));
    } 
}

Since I'm using Spring 3.2, I can take advantage of @ControllerAdvice 由于我使用的是Spring 3.2,我可以利用@ControllerAdvice


Out of curiosity, the overridden methods from the PropertyEditorSupport class in the StrictNumericFormat class are never invoked and the statements that redirect the output to the console as specified inside of those methods ( getAsText() and setAsText() ) don't print anything on the server console. 出于好奇,从不调用StrictNumericFormat类中的PropertyEditorSupport类的重写方法,并且在这些方法( getAsText()setAsText() )中指定的将输出重定向到控制台的语句不会在服务器控制台

I have tried all the approaches described in all the answers of that question but none worked for me. 我曾试图在所有的答案中描述的所有方法的问题 ,但没有为我工作。 What am I missing here? 我在这里错过了什么? Is this required to configure in some xml file(s)? 这是否需要在某些xml文件中配置?

Clearly you have nowhere passed the StrictNumericFormat reference. 很显然,你没有通过StrictNumericFormat引用。 You should register your editor like: 你应该注册你的编辑器:

 binder.registerCustomEditor(Double.class, new StrictNumericFormat());

BTW Spring 3.X introduced a new way achieving conversion: Converters BTW Spring 3.X引入了一种实现转换的新方法: 转换器

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

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