Announcement Announcement Module
Collapse
No announcement yet.
Custom Formatter<String> not working... Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Custom Formatter<String> not working...

    Hi,

    I am trying to apply a Spring 3 custom Formatter in SWF 2.1.1:

    Code:
    class AccountNumberFormatter implements Formatter<String> {
    
    	MaskFormatter mask;
    	
    	public AccountNumberFormatter(String pattern) {
    		try{
    			mask = new MaskFormatter(pattern);
    			mask.setValueContainsLiteralCharacters(false);
    		}catch(ParseException e) {
    			// TODO
    			e.printStackTrace();
    		}
    	}
    			
    	public String print(String object, Locale locale) {
    		try{
    			return mask.valueToString(object);
    		}catch(ParseException e){
    			return null;
    		}
    	}
    
    	public String parse(String text, Locale locale) throws ParseException {
    		return (String)mask.stringToValue(text);
    	}
    	
    }
    It is used through a custom annotation:

    Code:
    @AccountNumberFormat
    private String accountNumber;
    While other formatters work fine, this one does not. However in plain Spring 3 MVC, it works as expected.

    When I have a look at SFW code, I see the following in class BindingModel:

    Code:
    	private Object getFormattedValue(String field) {
    		Expression fieldExpression = parseFieldExpression(field, true);
    		Class valueType = fieldExpression.getValueType(boundObject);
    		if (isCustomConverterConfigured(field) || avoidConversion(valueType)) {
    			fieldExpression = parseFieldExpression(fieldExpression.getExpressionString(), false);
    		}
    		Object value = fieldExpression.getValue(boundObject);
    		if ((value instanceof String) == false) {
    			if (avoidConversion(valueType) == false) {
    				PropertyEditor editor = findSpringConvertingPropertyEditor(field, valueType);
    				if (editor != null) {
    					editor.setValue(value);
    					value = editor.getAsText();
    				}
    			}
    		}
    		return value;
    	}
    If I override the class and remove the test on the String class, everything works fine.
    So what is the reason behind this test ?
    I had a look at the previous post but it did not help...

  • #2
    Any solution to this problem?

    Comment


    • #3
      Any solution to this problem?

      How can I use annotation based formatter in spring web flow without specifying strictly in flow code?

      I finally got it working. Read here:
      http://forum.springframework.org/sho...t=83220&page=2
      Last edited by digz6666; Jan 14th, 2011, 03:07 AM.

      Comment

      Working...
      X