Results 1 to 5 of 5

Thread: property editor question.

  1. #1
    Join Date
    Oct 2005
    Location
    São Paulo, Brazil
    Posts
    14

    Default property editor question.

    Hi,

    I'm having some dificulties to find a good example about a custom property edition that fits my needs.

    I have a bean Channel that has a property of its same type.

    Code:
    public class Channel {
    
    	private Long id;
    	private String name;
    	private String path;
       private Channel superChannel;
    
    }
    I have choosen a simpleformcontroller to handle the insertion form, I passed a collection of Channels to the view, and my jsp to list all the Channels is:

    Code:
    		<spring&#58;bind path="channel.superChannel">
    		<td><fmt&#58;message key="cms.channel.form.superchannel.text"/></td>
    		<td>
    			<select name="<core&#58;out value="$&#123;status.expression&#125;"/>" size="5" style="width&#58; 300px;">
                	<core&#58;forEach var="channel" items="$&#123;channels&#125;">
                        <option value="<core&#58;out value="$&#123;channel.id&#125;"/>"<core&#58;if test="$&#123;status.value == channel.id&#125;"> selected</core&#58;if>><core&#58;out value="$&#123;channel.path&#125;"/></option>
                	</core&#58;forEach>
            	</select>
            	<br />
            	<font color="red"><core&#58;out value="$&#123;status.errorMessage&#125;"/></font>
    		</td>
    		</spring&#58;bind>
    After fill the properties, choose an Channel and submit, I have this error message:

    Code:
    Failed to convert property value of type &#91;java.lang.String&#93; to required type &#91;br.com.atom.cms.business.Channel&#93; for property superChannel
    I have read a lot of posts and know that I have to create a custom property editor to convert from a string to Channel, but i don't see any example that help me in it.

    I know that i have to overide initBinder in my controller and register a custon editor, something like that:

    Code:
    protected void initBinder&#40;HttpServletRequest request,
            ServletRequestDataBinder binder&#41; &#123;
    
            SimpleDateFormat dateFormat = new SimpleDateFormat&#40;"MMM-yy"&#41;;
            dateFormat.setLenient&#40;false&#41;;
            binder.registerCustomEditor&#40;Date.class, null,
                new CustomDateEditor&#40;dateFormat, true&#41;&#41;;
        &#125;
    But in this case, and all that i have seen, there is a built in property editor involved.

    Shoud I use one of then, like ClassEditor?!?

    Could someone give me a simple example of a custon property editor that converts a string to another object, like my Channel bean?!?

    Tks.
    Regards

    Jeferson Santos

  2. #2
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    Could someone give me a simple example of a custon property editor that converts a string to another object
    Take a look at the source in the Spring src (in the distro.) in package org.springframework.beans.propertyeditors.

  3. #3
    Join Date
    Oct 2005
    Location
    São Paulo, Brazil
    Posts
    14

    Default

    Tks for the advice katentim. I have made some tests but I'm still having problems:

    I have created a custom property editor for Channel, here is it:

    Code:
    public class CustomChannelEditor extends PropertyEditorSupport &#123;
    	
    	private ChannelDao channelDao;
    	
    	public CustomChannelEditor&#40;ChannelDao channelDao&#41; &#123;
    		this.channelDao = channelDao;
    	&#125;
    
    	public void setAsString&#40;String text&#41; &#123;
    		System.err.println&#40;"&#58; " + text&#41;;
    		if &#40;StringUtils.hasText&#40;text&#41;&#41; &#123;
    			Channel channel = channelDao.load&#40;&#40;Long&#41; NumberUtils.parseNumber&#40;text, Long.class&#41;&#41;;
    			if&#40;channel == null&#41; &#123;
    				throw new IllegalArgumentException&#40;"Invalid Channel identification"&#41;;
    			&#125; 
    			setValue&#40;channel&#41;;
    		&#125; else &#123;
    			setValue&#40;null&#41;;
    		&#125;
    	&#125;
    	
    	public String getAsText&#40;&#41; &#123;
    		Channel value = &#40;Channel&#41; getValue&#40;&#41;;
    		return &#40;value != null ? value.toString&#40;&#41; &#58; ""&#41;;
    	&#125;
    &#125;
    I have passed a DAO via constructor to this class to load the Channel indexed by the id filled in form, that I guess is in text parameter.

    So in my controller I have overide the initBinder, here is it:
    Code:
    	protected void initBinder&#40;HttpServletRequest request, ServletRequestDataBinder binder&#41; throws ServletException &#123;
    		binder.registerCustomEditor&#40;Channel.class, "channel.superChannel", new CustomChannelEditor&#40;channelDao&#41;&#41;;
    	&#125;
    and again, here is my jsp:

    Code:
    <spring&#58;bind path="channel.superChannel">
    		<td><fmt&#58;message key="cms.channel.form.superchannel.text"/></td>
    		<td>
    			<select name="<core&#58;out value="$&#123;status.expression&#125;"/>" size="5" style="width&#58; 300px;">
                	<core&#58;forEach var="channel" items="$&#123;channels&#125;">
                        <option value="<core&#58;out value="$&#123;channel.id&#125;"/>"<core&#58;if test="$&#123;status.value == channel.id&#125;"> selected</core&#58;if>><core&#58;out value="$&#123;channel.path&#125;"/></option>
                	</core&#58;forEach>
            	</select>
            	<br />
            	<font color="red"><core&#58;out value="$&#123;status.errorMessage&#125;"/></font>
    		</td>
    		</spring&#58;bind>
    The problem remains, I still getting this message error:
    Code:
    Failed to convert property value of type &#91;java.lang.String&#93; to required type &#91;br.com.atom.cms.business.Channel&#93; for property superChannel
    Is there something that I have missed or that I have done wrong?

    Regards.
    Regards

    Jeferson Santos

  4. #4
    Join Date
    Aug 2004
    Location
    Sydney
    Posts
    503

    Default

    It's setAsText, not setAsString

  5. #5
    Join Date
    Oct 2005
    Location
    São Paulo, Brazil
    Posts
    14

    Default

    Tks gmatthews, you are right, I'm a dumb... :-)

    Here is my final code, it can help peolpe with the same problem, tks guys.


    My bean:
    Code:
    public class Channel &#123;
    
       private Long id;
       private String name;
       private String path;
       private Channel superChannel;
    
      //getters and setters
    
    &#125;
    The custom property editor:
    Code:
    public class CustomChannelEditor extends PropertyEditorSupport &#123;
    	
    	private ChannelDao channelDao;
    	
    	public CustomChannelEditor&#40;ChannelDao channelDao&#41; &#123;
    		this.channelDao = channelDao;
    	&#125;
    
    	public void setAsText&#40;String text&#41; &#123;
    		Channel channel = null;
    		if &#40;StringUtils.hasText&#40;text&#41;&#41; &#123;
    			channel = channelDao.load&#40;&#40;Long&#41; NumberUtils.parseNumber&#40;text, Long.class&#41;&#41;;
    			if&#40;channel == null&#41; &#123;
    				throw new IllegalArgumentException&#40;"Invalid Channel identification"&#41;;
    			&#125; 
    			setValue&#40;channel&#41;;
    		&#125; else &#123;
    			setValue&#40;null&#41;;
    		&#125;
    	&#125;
    	
    	public String getAsText&#40;&#41; &#123;
    		Channel value = &#40;Channel&#41; getValue&#40;&#41;;
    		return &#40;value != null ? value.toString&#40;&#41; &#58; ""&#41;;
    	&#125;
    &#125;
    My formcontroller:
    Code:
    	protected Map referenceData&#40;HttpServletRequest request&#41; throws Exception &#123;
    		Map<String, Object> refData = new HashMap<String, Object>&#40;&#41;;
    		Collection channels = channelDao.list&#40;&#41;;
    		refData.put&#40;"channels", channels&#41;;
    		return refData;
    	&#125;
    
    	protected void initBinder&#40;HttpServletRequest request, ServletRequestDataBinder binder&#41; throws ServletException &#123;
    		binder.registerCustomEditor&#40;Channel.class, new CustomChannelEditor&#40;channelDao&#41;&#41;;
    	&#125;
    Tks again.
    Regards

    Jeferson Santos

Similar Threads

  1. ERROR: Context initialization failed
    By makhlo in forum Architecture
    Replies: 8
    Last Post: Jul 11th, 2008, 01:41 AM
  2. Order of Bean definitions matters?
    By cfuser in forum Container
    Replies: 2
    Last Post: Oct 21st, 2005, 10:29 AM
  3. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  4. Replies: 4
    Last Post: Aug 17th, 2005, 04:42 AM
  5. Replies: 2
    Last Post: May 13th, 2005, 05:42 AM

Posting Permissions

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