Page 5 of 6 FirstFirst ... 3456 LastLast
Results 41 to 50 of 56

Thread: Custom Converter Example

  1. #41

    Default

    This is my full webflow-config.xml

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:webflow="http://www.springframework.org/schema/webflow-config"
           xsi:schemaLocation="
               http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/webflow-config
               http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd">
    
       <bean name="/register.htm" id="flowController" class="org.springframework.webflow.mvc.servlet.FlowController">
          <property name="flowExecutor" ref="flowExecutor" />
       </bean>
    
       <!-- For annotated methods and types -->
       <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
       <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    
       <!--enable jawr controller to be found without errors-->
       <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
    
    
       <webflow:flow-executor id="flowExecutor" flow-registry="flowRegistry" />
    
       <!--This creates an XmlFlowRegistryFactory bean -->
       <webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices">
          <webflow:flow-location path="/WEB-INF/flows/register.xml" />
       </webflow:flow-registry>
    
       <webflow:flow-builder-services id="flowBuilderServices"
                                      view-factory-creator="mvcViewFactoryCreator"
                                      conversion-service="conversionService"
                                      development="true" />
    
       <bean id="mvcViewFactoryCreator" class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
          <property name="viewResolvers" ref="viewResolver" />
          <property name="useSpringBeanBinding" value="true" />
       </bean>
    
       <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
          <property name="flowRegistry" ref="flowRegistry" />
          <property name="order" value="0" />
       </bean>
    
       <!-- Enables FlowHandler URL mapping -->
       <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
          <property name="flowExecutor" ref="flowExecutor" />
       </bean>
    
    
    </beans>
    the line webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices" is present so the problem should lie somewhere else. I noticed that the constructor of the ApplicationConversionService is called twice and therefore also the line addConverter("myDate", new MyDateConverter()); inside that class is called twice.

    I removed the @Service annotation for this class by using the standard <bean id="" class="" > definition but still the constructor is called twice. Before moving to webflow in my old controller with Spring MVC I was using a property editor for the MyDate class without any isse.

    Open for suggestions.
    Last edited by dawez; Sep 15th, 2009 at 05:38 AM. Reason: Adding better explanations
    --
    Davide

  2. #42
    Join Date
    Oct 2009
    Location
    Czech Republic
    Posts
    29

    Default

    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.

    Neither of this works (java.util.Date is not converted to a formatted String):

    Code:
    <input type="text" class="date_field" value="${ticketFilterCommand.dateFrom}" id="dateFrom" name="dateFrom" />
    Code:
    <form:input path="dateFrom" cssClass="date_field" />
    My config:

    Spring Framework 2.5.6
    Spring Web Flow 2.0.8
    com.springsource.org.jboss.el-2.0.0.GA.jar
    Tiles 2.0.7

    My definitions:

    Code:
    <flow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices">
    
        <flow:flow-location path="/WEB-INF/flows/tickets_flow.xml" id="auth/tickets" />
        <flow:flow-location path="/WEB-INF/flows/create_ticket_flow.xml" id="auth/create_ticket" />
        <flow:flow-location path="/WEB-INF/flows/ticket_inspector_flow.xml" id="auth/ticket_inspector" />
    
        <flow:flow-location path="/WEB-INF/flows/contact_persons_flow.xml" id="auth/contact_persons" />
        <flow:flow-location path="/WEB-INF/flows/create_contact_person_flow.xml" id="auth/create_contact_person" />
        <flow:flow-location path="/WEB-INF/flows/edit_contact_person_flow.xml" id="auth/edit_contact_person" />
    
        <flow:flow-location path="/WEB-INF/flows/contact_person_access_flow.xml" id="auth/contact_person_access" />
    
    </flow:flow-registry>
    
    <flow:flow-executor id="flowExecutor" />
    
    <flow:flow-builder-services id="flowBuilderServices"
        view-factory-creator="mvcViewFactoryCreator"
        conversion-service="myConversionService" />
    
    <bean id="mvcViewFactoryCreator" class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
        <property name="viewResolvers" ref="tilesViewResolver"/>
        <property name="useSpringBeanBinding" value="false" />
    </bean>
    
    <bean id="tilesViewResolver" class="org.springframework.js.ajax.AjaxUrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.webflow.mvc.view.FlowAjaxTilesView"/>
    </bean>
    The custom converter:

    Code:
    public class MyStringToDate extends StringToDate {
    
        public MyStringToDate() {
    
            setPattern("dd.MM.yyyy");
    
        }
    
    }
    The custom conversion service with two versions of the addDefaultConverters() method (none of them working):

    Code:
    @Component("myConversionService")
    public class MyConversionService extends DefaultConversionService {
    
        /*@Override
        protected void addDefaultConverters() {
    
            super.addDefaultConverters();
    
            addConverter(new MyStringToDate());
            
        }*/
    
        @Override
        protected void addDefaultConverters() {
    
            super.addDefaultConverters();
    
            StringToDate stringToDate = new StringToDate();
            stringToDate.setPattern("dd.MM.yyyy");
    
            addConverter(stringToDate);
    
        }
    
    }
    Observations:

    • I tried to turn the useSpringBeanBinding option on and off without any success or difference.


    Question

    What am I doing wrong?

    Can I use the custom conversion service to convert java.util.Date instances into a formatted string or am I getting something wrong here?

    Thank you very much for you assistance and help.

  3. #43

    Default

    HI MartinCZ,

    it seems to me that your converter is a bit too short. I am using the following one. Please try something like this.

    Code:
    public class StringToMyDate extends StringToObject {
       public StringToMyDate() {
          super(MyDate.class);
       }
    
       @Override
       public Object toObject(String string, Class aClass) throws Exception {
          MyDate myDate = new MyDate(string);
          return myDate;
       }
    
       @Override
       public String toString(Object object) throws Exception {
          MyDate myDate = (MyDate) object;
          return myDate.toString();
       }
    I am converting back<->forth Date class to Mydate class [which contains specific method for my app.] This way is working like a charm.

    Let me know if this is not working and I will try to help more.
    --
    Davide

  4. #44
    Join Date
    Oct 2009
    Location
    Czech Republic
    Posts
    29

    Default

    Hi, thanks for the tip. I didn't have to use it though, because my converter suddenly began to work I really have no idea what's going on here, I am pretty sure I didn't do any changes in the code. I was just debugging the framework's code and suddenly it worked.

  5. #45

    Default

    Hey Cool !

    I also had time ago an issue that was working only when debugging in intellij. At the end I managed to understand that the debugger is initializing the classes/object and sometimes you have different results in debug/run configuration.

    Cool... working code is always good !
    --
    Davide

  6. #46
    Join Date
    Feb 2010
    Posts
    3

    Default 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

  7. #47
    Join Date
    Feb 2010
    Posts
    3

    Default

    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..

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

    Default

    Let's care our nature!

  9. #49
    Join Date
    Nov 2010
    Posts
    16

    Default

    Quote 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

  10. #50
    Join Date
    Nov 2010
    Posts
    16

    Default

    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.

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
  •