質問

Spring CustomNumberEditorエディターを使用してフロート値をバインドしていますが、値が数字でない場合は、値を解析し、エラーが返されない場合があります。

  • 番号= 10 ......その後数は10で、エラーはありません
  • 番号= 10a ......次に、番号は10で、エラーはありません
  • 番号= 10A25 ......次に、番号は10で、エラーはありません
  • 番号= a ......番号が無効であるためエラー

したがって、編集者は値を可能にするまで値を解析し、残りを省略しているようです。このエディターを構成する方法はありますか?検証が厳格になります(10Aや10A25のような数字の結果はエラーになります)、またはカスタム実装をビルドする必要がありますか?私はCustomDateEditor/dateFormatでFalseに寛大に設定するようなものを探しているので、日付を最も可能性の高いものに解析することはできません。

編集者の登録方法は次のとおりです。

@InitBinder
public void initBinder(WebDataBinder binder){
    NumberFormat numberFormat = NumberFormat.getInstance();
    numberFormat.setGroupingUsed(false);
    binder.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, numberFormat, true));
}

ありがとう。

役に立ちましたか?

解決

Numberformatクラスに依存しているため、最初の無効な文字で入力文字列の解析を停止するため、Numberformatクラスを拡張する必要があると思います。

最初の赤面はそうでしょう

public class StrictFloatNumberFormat extends NumberFormat {

  private void validate(in) throws ParseException{
     try {
       new Float(in);
     }
     catch (NumberFormatException nfe) {
       throw new ParseException(nfe.getMessage(), 0);     
  }


  public Number parse(String in) throws ParseException {
    validate(in);
    super.parse(in);
  }
  ..... //any other methods
}

他のヒント

あなたはこれを実行することはできませんnumberformat。

ドキュメントはこの事実について明確です。

/**
 * Parses text from the beginning of the given string to produce a number.
 * The method may not use the entire text of the given string.
 * <p>
 * See the {@link #parse(String, ParsePosition)} method for more information
 * on number parsing.
 *
 * @param source A <code>String</code> whose beginning should be parsed.
 * @return A <code>Number</code> parsed from the string.
 * @exception ParseException if the beginning of the specified string
 *            cannot be parsed.
 */
public Number parse(String source) throws ParseException {

このAPIをACCEEPTすると、必要なことを行い、NumberFormatインターフェイスを実装するパーサーを書くことさえ無効になります。これは、代わりに自分のプロパティエディターを実装する必要があることを意味します。

/* untested */
public class StrictNumberPropertyEditor extends PropertyEditorSupport {

    @Override
    public void setAsText(String text) throws IllegalArgumentException {
       super.setValue(Float.parseFloat(text));
    }

    @Override
    public String getAsText() {
        return ((Number)this.getValue()).toString();
    }    
}

最もエレガントなアプローチは使用することだと思います NumberFormat.parse(String,ParsePosition), 、 このようなもの:

public class MyNumberEditor extends PropertyEditorSupport {
    private NumberFormat f;
    public MyNumberEditor(NumberFormat f) {
        this.f = f;
    }

    public void setAsText(String s) throws IllegalArgumentException {
        String t = s.trim();
        try {
            ParsePosition pp = new ParsePosition(0);
            Number n = f.parse(t, pp);
            if (pp.getIndex() != t.length()) throw new IllegalArgumentException();
            setValue((Float) n.floatValue());
        } catch (ParseException ex) {
            throw new IllegalArgumentException(ex);
        }
    }

    ...
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top