Page 6 of 6 FirstFirst ... 456
Results 51 to 56 of 56

Thread: Custom Converter Example

  1. #51
    Join Date
    Jul 2006
    Posts
    8

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

  2. #52
    Join Date
    Jul 2006
    Posts
    8

    Default

    oh well...anyone please

  3. #53
    Join Date
    Aug 2008
    Posts
    15

    Post

    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
        
    }


  4. #54
    Join Date
    Jul 2006
    Posts
    8

    Default

    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.

  5. #55
    Join Date
    Aug 2008
    Posts
    15

    Default

    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.

  6. #56
    Join Date
    Jul 2006
    Posts
    8

    Default

    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.

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
  •