Announcement Announcement Module
Collapse
No announcement yet.
PropertyEditorSupport problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • PropertyEditorSupport problem

    Hi, I'm trying to convert a comma-seperated string of keywords (like keyword1,keyword2 etc) to Set<Keywords> using property editors. I know that I am doing something wrong....can You please advise?
    first I wanted do implement ...binder.registerCustomEditor(Set.class... and create my Set<Keyword> object from my initial string there, but spring complained that It doesn't see the Keyword property editor....I'm a little confused....


    I keep getting the following error:
    Code:
    Field error in object 'command' on field 'keywords': rejected value [key1,key2]; codes [typeMismatch.command.keywords,typeMismatch.keywords,typeMismatch.java.util.Set,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [command.keywords,keywords]; arguments []; default message [keywords]]; default message [Failed to convert property value of type [java.lang.String] to required type [java.util.Set] for property 'keywords'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [com...Keyword] for property 'keywords[0]': no matching editors or conversion strategy found]
    My Command object :
    Code:
    public class ComplexArticleCommand {
    	....
    
    	private Set<Keyword> keywords;
    .......
    getters setters
    }
    part of my jsp:
    Code:
    <form:form>
    	......
    			
    				<form:textarea path="keywords" rows="1" />
    		......
    my initBiner logic
    Code:
    protected void initBinder(HttpServletRequest req,
    			ServletRequestDataBinder binder) {
    		try {
    			super.initBinder(req, binder);
    		} catch (Exception e) {
    			throw new RuntimeException(e);
    		}
    		binder.registerCustomEditor(Set.class, null,
    				new PropertyEditorSupport() {
    					@Override
    					public void setAsText(String text)
    							throws IllegalArgumentException {
    
    						String keywords[] = text.split(",");
    						Set<Keyword> keywordSet = new HashSet<Keyword>();
    						for (String keyword : keywords) {
    							keywordSet.add(new Keyword(keyword));
    						}
    						setValue(keywordSet);
    
    					}
    				});
    
    		binder.registerCustomEditor(Keyword.class, null,
    				new PropertyEditorSupport() {
    					@Override
    					public void setAsText(String text)
    							throws IllegalArgumentException {
    						setValue(new Keyword(text));
    					};
    
    					@Override
    					public String getAsText() {
    						return ((Keyword) getValue()).getName();
    					}
    				});
    	}

  • #2
    Ok,I think I got it:
    This code works fine. But anyway can You guys please double check weather it is an accepted solution for this kink of problem. How Can I improve the code?
    Oh, and another thing, when I inspect my HashSet Here -
    Code:
    ((ComplexArticleCommand) command)
                        .getKeywords();
    , I see that its part of some Map, Its KeySet actually. This is very weird. It might be the fact that the setAsText method is able to set only one Keyword instance at a time and than internally connvert it to Set<Keyword> and give it to me.


    Code:
    protected void initBinder(HttpServletRequest req,
                ServletRequestDataBinder binder) {
            try {
                super.initBinder(req, binder);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            binder.registerCustomEditor(Set.class, "keywords",
                    new CustomCollectionEditor(Set.class) {
                        @Override
                        public void setAsText(String text) {
                            String keywords[] = text.split(",");
                            Set<Keyword> keywordSet = new HashSet<Keyword>();
                            for (String keyword : keywords) {
                                keywordSet.add(new Keyword(keyword));
                            }
                            setValue(keywordSet);
                        }
    
                    });
    
    
        }
    Last edited by yarco; May 31st, 2009, 07:47 AM.

    Comment

    Working...
    X