I am using Dojo 1.9 with Grails 2.3.9.
The Dojo NumberTextBox widget - that I use in my forms - sets real number values (e.g.: 12.56) in a fixed format (the JavaScript base format) into the HTML form input fields (but displays/edits them according to the browser locale, so the user always sees properly formatted numbers).
Grails on the other hand expects the input fields to be formatted according to the browser locale.
This results in a conversion mismatch and the effect is that Grails loses the decimal places when the browser locale is not English and saves the record incorrectly to the database.
I was trying to override the value conversion in Grails by implementing a custom ValueConverter and registering it in the application context.
The request sent by the browser contains the real value correctly ("12.45")
The main problem is that my converter doesn't seem to be used at all.
How do I register it to override the defaut Double data conversion?
The converter:
package gefc.dojo.binding
import org.grails.databinding.converters.ValueConverter
import java.text.NumberFormat
/**
* Converter which allows that the doubles arrive
*/
class DojoDoubleValueConverter implements ValueConverter {
NumberFormat fmt
DojoDoubleValueConverter() {
// The number format sent by Dojo components
// English locale for the decimal separator
fmt = NumberFormat.getInstance(Locale.ENGLISH);
// no grouping
fmt.setGroupingUsed(false);
}
boolean canConvert(value) { value instanceof String }
def convert(value) {
Number n = fmt.parse(value)
return n.doubleValue()
}
Class<?> getTargetType() {
return Double.class
}
}
My registration in the application context (resources.groovy)
beans = {
// Dojo components send real values in a fixed, ISO format, while Grails
// expects them to be formatted according to client/browser locale
// So we need to override real value conversions
doubleConverter gefc.dojo.binding.DojoDoubleValueConverter
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…