Results 1 to 3 of 3

Thread: Custom Formatter<String> not working...

Hybrid View

  1. #1
    Join Date
    Apr 2005
    Posts
    8

    Default 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. #2
    Join Date
    Jul 2007
    Posts
    6

    Default

    Any solution to this problem?

  3. #3
    Join Date
    Dec 2008
    Location
    Ulaanbaatar, Mongolia
    Posts
    123

    Default

    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 at 03:07 AM.
    Let's care our nature!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •