Craig Johnston opened SPR-12154 and commented
I have a form on a web page that takes various values for doing a search and makes a GET
request to the server on submission. One of these field is an int field which can be empty. If it is empty I wish to set it to Integer.MAX_VALUE
which signifies that no particular value is required in the search.
I registered a Formatter
implementation with the FormatterRegistry
.
registry.addFormatterForFieldType(Integer.class, new Formatter<Integer>() {
@Override
public Integer parse(String text, Locale locale) throws ParseException {
if (StringUtils.isBlank(text)) {
return Integer.MAX_VALUE;
}
return Integer.parseInt(text);
}
@Override
public String print(Integer value, Locale locale) {
if (Integer.MAX_VALUE == value) {
return "";
}
return value.toString();
}
});
However when a blank query parameter value is included in the request to the server, Spring is unable to call my formatter's parse method due to this section of FormattingConversionService.ParserConverter
.
@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
String text = (String) source;
if (!StringUtils.hasText(text)) {
return null;
}
null
will be returned from this. TypeConverterDelegate
does then attempt to fallback to PropertyEditors. My understanding however is that PropertyEditors are now discouraged and don't seem to play well with Web Flow.
My feeling is that it should be possible for custom Formatter
implementations to handle the case of an empty parameter.
Affects: 4.0.6
Comment From: spring-projects-issues
Bulk closing outdated, unresolved issues. Please, reopen if still relevant.
Comment From: sbrannen
- duplicate of #12969