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

  • #46
    converter not working

    Hi guys,

    I've been reading this thread back and forth and all info I could find elsewhere about custom converters in Spring. It seems like everyone here has/had similar problems only I can't find a solution for my specific case. I must still be missing something. I get this error:

    org.springframework.binding.convert.ConversionExec utorNotFoundException:
    Custom ConversionExecutor with id 'userToString' cannot convert from sourceClass [nl.coremail.User] to targetClass [java.lang.String]


    My converter userToString is found but spring thinks it can't do the required conversion.

    this is my converter:

    Code:
    public class UserToString extends StringToObject {
    	
    	public UserToString() {
    		super(String.class);
    	}
    
    	@Override
    	public Object toObject(String arg0, Class arg1) throws Exception {
    		return new User();
    	}
    
    	@Override
    	public String toString(Object arg0) throws Exception {
    		return "";
    	}	   
    }
    my conversion service:

    Code:
    @Component("webflowConversionService")
    public class ApplicationConversionService extends DefaultConversionService {
    	
        @Override
        protected void addDefaultConverters() {
    
            super.addDefaultConverters();
            addConverter("userToString", new UserToString());
        }
    }

    webflow config contains:

    Code:
    <webflow:flow-builder-services id="flowBuilderServices" conversion-service="webflowConversionService" development="true" />
    
    <webflow:flow-registry id="flowRegistry" base-path="/WEB-INF/flows" flow-builder-services="flowBuilderServices">
    <webflow:flow-location-pattern value="/**/*-flow.xml" />
    </webflow:flow-registry>

    flow contains:

    Code:
    <view-state id="editContact" model="contact">
    <binder>
    	<binding property="user" converter="userToString" required="true" />			
    </binder>
    ...

    jsp contains:

    Code:
    <form:input path="user" />

    Can anyone please give me a hand?

    Spring 3, also tried 2.5.6.SEC01
    Webflow 2.0.8

    Comment


    • #47
      wow, minutes after posting the above I found the problem.

      changed:
      public UserToString() {
      super(String.class);
      }

      to

      public UserToString() {
      super(User.class);
      }

      that was all..

      Comment


      • #48
        Following is working for me:

        http://forum.springsource.org/showthread.php?p=315661

        Comment


        • #49
          Originally posted by MartinCz View Post
          Hi.

          I swear I tried to solve this problem by searching the forum, but I failed.

          The CUSTOM DATE conversion only works one way for me. It only work when the form is submitted (then the date string is properly parsed and converted into java.util.Date). The other way (converting a java.util.Date instance into a formatted String for displaying in a form) does not work.
          I've go exactly the same problem, although the versions I use are different:

          Spring Framework 3.0.4
          Spring Web Flow 2.2.0
          Liferay (this is a portlet)

          View technology is plain JSP, using the Spring Form tag library.

          The converter I'm trying to use is
          Code:
          StringToDate dateConverter = new StringToDate();
          dateConverter.setPattern("dd/MM/yyyy");
          addConverter(dateConverter);
          Yes, I have the
          Code:
          conversion-service="applicationConversionService"
          in my webflow configuration.

          If I debug the StringToDate class, I can see the "toObject" method being called on submit, but not the "toString" one on rendering.

          Has anyone understood why it works in one direction and not in the other?
          Is it perhaps a known bug?

          Regards,
          Gabriele

          Comment


          • #50
            I answer to myself, as I found the solution.

            See also my comment on bug 1056 .

            The problem was that in my flow definition, I had the model bean specified as "flowScope.searchModel"; unfortunately, you must not use this notation but only use the bean name ("searchModel"), because SWF does not handle scope specifiers in the model attribute in view-states.

            Comment


            • #51
              using converters for hackproofing

              in 2.x, there is the converter attribute that can be customized. How can I reuse StringTrimmerEditor for example using the "converter" attribute during binding? I know I had to extend defaultconversionservice and the config associated with it but I've not seen sample code reusing existing property editors like stringtrimmereditor and adding them in the default converters.

              A sample code snippet would help...

              Comment


              • #52
                oh well...anyone please

                Comment


                • #53
                  There are a couple of potential responses, depending on the technology stack you are using:

                  With JSF, you can use some of the f: tags to force an explicit conversion for the h:input fields.

                  If it is a custom type and needs to be converted every time, I recommend registering a bean for the type that tells web flow to use the converter for the type every time a form is submitted. For example:

                  PHP Code:
                  public class ApplicationConversionService extends DefaultConversionService {    
                      public 
                  ApplicationConversionService() {
                          
                  addDefaultConverters();
                          
                  addDefaultAliases();
                          
                  addConverter("customConverter", new CustomConverter());        
                      }

                  This can also be done in an old way with XML bean registrations, but I can't seem to find an example of it since it is rarely used now.

                  To create a custom converter, you'll need to use the JSF template as detailed with this link:

                  PHP Code:
                  public class CustomConverter
                      
                  public Object getAsObject(FacesContext facesContextUIComponent uiComponentString param) {
                          
                  // Take the String and look it up in the database to get an object representation
                      
                  }
                   
                      public 
                  String getAsString(FacesContext facesContextUIComponent uiComponentObject obj) {
                          
                  // Convert `obj` and pull out the relevant information to turn it into a String
                      
                  }

                  Comment


                  • #54
                    thanks for the Faces example above but I don't see reference to StringTrimmerEditor, I just want to reuse existing property editors and not reinvent them in the converters, which is what I'm getting from your example. BTW, I was looking for a non-Faces example, just pure Spring Web Flow using a FlowController as the flow dispatcher. Thanks for your time.

                    Comment


                    • #55
                      What view technology are you using for your view? JSP/JSF/Something-else?

                      I hear your thoughts on using existing functionality, but sometimes you're going to have to write your own code. You are not re-inventing the wheel if you can't get it to work with existing code. Part of being a programmer today is wiring together frameworks and understanding the cycles to inject yourself into so that you have control over when something happens.

                      More details on the tech stack gives us a better ability to help out.

                      Comment


                      • #56
                        so I guess your answer is yes, that I cannot reuse existing propertyeditors in my current code for the webflow implementation of converters? A yes or no answer would've sufficed. The view I'm using doesn't count as far as reusing property editors and doing validations on the middle tier.

                        Comment

                        Working...
                        X