Spiga

给Spring Web Flow自定义Converter

想用spring web flow和richfaces的table实现服务器端分页,可以生成一个假冒的List修改它的size等方法,可以参考《wicket开发指南》中的做法,但这种做法总觉得不太完美。按更好的做法是实现一个特定richfaces table的datamodel,而spring web flow中对table用了他自己的datamodel,这就需要自定义converter。

http://forum.springframework.org/showthread.php?t=59042

The best example is in the booking-mvc sample application.

Steps...

1. Implement your custom converter, modeling after existing converters in org.springframework.binding.convert.converters (done once per custom converter). For example:



  1. public class StringToMoney extends StringToObject {   

  2.   

  3. public StringToMoney() {   

  4. super(Money.class);   

  5. }   

  6.   

  7. @Override  

  8. protected Object toObject(String string, Class targetClass) throws Exception {   

  9. return Money.valueOf(string);   

  10. }   

  11.   

  12. @Override  

  13. protected String toString(Object object) throws Exception {   

  14. Money amount = (Money) object;   

  15. return amount.toString();   

  16. }   

  17.   

  18. }  



2. Create a custom conversion service implementation that installs your custom converters (done once):



  1. @Component("conversionService")   

  2. public class ApplicationConversionService extends DefaultConversionService {   

  3.   

  4.    @Override  

  5.    protected void addDefaultConverters() {   

  6.       super.addDefaultConverters();   

  7.   

  8.       // registers a default converter for Money type   

  9.       addConverter(new StringToMoney());   

  10.   

  11.       // registers a custom converter reference-able by id and applied when requested   

  12.       addConverter("shortDate"new StringToDate());   

  13.    }   

  14.   

  15. }  


3. Register the conversion service with Web Flow (done once):



  1. <webflow:flow-builder-services id="flowBuilderServices" service="conversionService">  



Now there is a bug in 2.0.3 where the default expression parser used during data binding does not apply your custom conversion service. This is fixed in 2.0.4 nightly, but to workaround it on 2.0.3 you must manually configure the expression parser implementation and configure its conversion service e.g.



  1. <webflow:flow-builder-services id="flowBuilderServices" service="conversionService" parser="expressionParser">  

  2.   

  3.  <bean id="expressionParser" class="org.springframework.webflow.expression.el.WebFlowELExpressionParser">  

  4.      <constructor-arg>  

  5.          <bean class="org.jboss.el.ExpressionFactoryImpl">     

  6.      </bean>  

  7.      <property name="conversionService" ref="conversionService">  

  8.  </property>  


4. Default converters for your custom types should be used automatically anytime you bind to a property of that type. For custom converters to be applied for a specific type you reference the converter when you setup a view-binding:



  1. <view-state id="enterBookingDetails" model="booking">  

  2.     <binder>  

  3.         <binding property="checkinDate" converter="shortDate" required="true">  

  4.         <binding property="checkoutDate" converter="shortDate" required="true">  

  5.         <binding property="beds" required="true">  

  6.         <binding property="smoking" required="true">  

  7.         <binding property="creditCard" required="true">  

  8.         <binding property="creditCardName" required="true">  

  9.         <binding property="creditCardExpiryMonth" required="true">  

  10.         <binding property="creditCardExpiryYear" required="true">  

  11.         <binding property="amenities" required="false">  

  12.     </binding>  

  13.     <transition on="proceed" to="reviewBooking">  

  14.     <transition on="cancel" to="cancel" bind="false">  

  15. </transition>  




多选:
http://jira.springframework.org/browse/SWF-720
使用jsf的验证器:
http://jira.springframework.org/browse/SWF-799

0 评论: