English 中文(简体)
是否太晚了,无法处理由一门习俗的春瓦底人约束到一个投入领域的长期问题?
原标题:Is it too late to handle a Long-Value bound to an Input field by a custom Spring Validator?

Parts of the (Java, Spring-mvc, JSP) Form input is checked by Javascript. The rest is done by a custom Validator. The problem is when the Javascript validation is prevented, such an error may be printed:

Failed to convert property value of type java.lang.String to required type java.lang.Long for property xxx; nested exception is java.lang.NumberFormatException: For input string: "x"

是否有更好的办法处理转换问题。 当贾瓦-瓦解者接手时,它已经遇到转换错误,或者现在还有可能加以核对?


编辑:主计长内部的协调人代码。

@InitBinder
public void initBinder(WebDataBinder binder) {
   SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
   dateFormat.setLenient(false);

   binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
   binder.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, true));
}

但其真正公正的标准功能(org. Cerframework.beans.propertyeditors.CustomNumberEditor)和我认为必须有一个简单的解决办法,因为它似乎是一个共同的东西,可以长期反对某些杂质,并试图将其排除在外(或赶走)。

最佳回答

if someone s interested .. the code of the custom Binder that worked in the end follows..

  // prevent alphanumeric/special chars in numeric string (for backing field of type Long)
  binder.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, true) {
     @Override
     public void setAsText(String value) {
        if (value != null && !value.isEmpty() && CustomValidator.isNumeric(value)) {
           super.setAsText(value);
        } else {
           super.setAsText(null);
        }
     }
  });

虽然你仍然必须调整其验证方法,使其对所输入、移除和改成无效的案文具有意义。

问题回答

Seems that you need to implement a binder in your controller to convert from String to Long. See if this thread helps, it gives you to code to convert String to Long.

Update

binder.registerCustomEditor(Long.class, "myFieldName", new  PropertyEditorSupprt(){
    @Override
    public void setValue(Object value) {
        if(value instanceof String)
           super.setValue(new Long(value));      
    }

});




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签