Announcement Announcement Module
Collapse
No announcement yet.
Spring 3 formatter and error messages Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring 3 formatter and error messages

    I have implemented a custom formatter:
    Code:
    @Component
    public class LensParamSignedDecimalFormatter implements Formatter<LensParamValueSignedDecimal> {
    
    	private static final Logger LOGGER = Logger.getLogger(
    		LensParamSignedDecimalFormatter.class.getName() );
    	public static final String LENS_PARAM_SIGNED_DECIMAL_FORMAT = "+#0.00;(-#0.00)";
    
    	@Override
    	public String print( LensParamValueSignedDecimal lensParamValueDecimal,
    						 Locale locale )
    	{
    		DecimalFormat decimalFormatter = ( DecimalFormat ) NumberFormat.getInstance(
    			locale );
    		decimalFormatter.applyPattern( LENS_PARAM_SIGNED_DECIMAL_FORMAT );
    
    		return decimalFormatter.format( lensParamValueDecimal.getValue() );
    	}
    
    
    	@Override
    	public LensParamValueSignedDecimal parse( String string, Locale locale ) throws ParseException
    	{
    		DecimalFormat decimalFormatter =
    			( DecimalFormat ) NumberFormat.getInstance( locale );
    		decimalFormatter.applyPattern( LENS_PARAM_SIGNED_DECIMAL_FORMAT );
    
    		LensParamValueSignedDecimal value = new LensParamValueSignedDecimal();
    		value.setValue( new BigDecimal(
    			decimalFormatter.parse( string ).doubleValue() ) );
    
    		return value;
    	}
    
    }
    The formatter is registered. When I debug my project I see that my Spring Webflow application makes us of the formatter.

    If I enter for instance a string in a input field, which is bounded to a bean property (spring binding mechanism is used) the formatter will obviously throw a parse exception.

    In my JSP page I want to inform the user, that he entered a wrong value. I have implemented the following:
    Code:
    <spring:hasBindErrors name="defineLensViewModel">	
       <c:if test="${errors.errorCount > 0 }">
          <font color="red">
             <c:forEach items="${errors.fieldErrors}" var="error">
                <c:out value="${error.field} - ${error}" />
             </c:forEach>
          </font>
       </c:if>
    </spring:hasBindErrors>
    The code above in the JSP page is just for debugging purpose. I want to use the error code in connection with a ReloadableResourceBundleMessageSource. Unfortunately the spring framework does not provide the FieldError Object with an error code and that is exactly what I do not understand.

    In my resource bundle message source I want to define the following:
    typeMismatch.defineLensViewModel.param1=Please enter a valid decimal value with a sign e.g. +2.00 or -4.30

    The output of <c:out value="${error.field} - ${error}" /> is as follows:
    param1 - Field error in object 'defineLensViewModel' on field 'param1': rejected value [null]; codes []; arguments []; default message [typeMismatch on param1]

    Why is the attribute "codes" empty? What do I have to do that spring uses an error code which I can use for my message source.
Working...
X