0

Is there a way to bind a converter using spring bean?

asked 2009-06-02 15:37:37 +0800

fmcypriano gravatar image fmcypriano
612 1 7
http://felipecypriano.com...

Hello,

I'm trying to use spring instead of full class name, like this:
<radiogroup selectedItem="@{pedidoVenda.origemEstoque, converter=${origemEstoqueRadiogroupConverter}}">

But the ${} isn't evaluated and it's treated like the actual class name. Is there a way to do it?

Regards,
Felipe Cypriano

delete flag offensive retag edit

9 Replies

Sort by » oldest newest

answered 2009-06-02 15:43:14 +0800

robertpic71 gravatar image robertpic71
1275 1

>> I'm trying to use spring instead of full class name, like this:
This is not possible with the actual ZK version. There is also posted a featurerequest.

Meanwhile use fixed converters or try the SpringUtil from ZK inside the converter.

/Robert

link publish delete flag offensive edit

answered 2009-06-04 02:56:14 +0800

robbiecheng gravatar image robbiecheng
1144 2
http://robbiecheng.sys-co...

how do u define your bean in spring configuration file?

/robbie

link publish delete flag offensive edit

answered 2010-04-15 07:02:25 +0800

fmcypriano gravatar image fmcypriano
612 1 7
http://felipecypriano.com...

updated 2010-04-15 07:04:54 +0800

So, it's time to resurrect this thread. Now I need this functionality to avoid lots of implementation that changes almost nothing, based on the unique-possible-solution given by henrichen in Input masks and data binding not working, I'll need one type for each mask I use in my app.

If it was possible to use spring bean in converters I only need to create a single class and one spring bean for each mask, something like (to simplify this is groovy/grails code):

package com.felipecypriano;
class MaskFilterTypeConverter implements TypeConverter {
  String maskToAvoid
  public Object coerceToBean(java.lang.Object val, Component comp) {
    if (maskToAvoid.equals(val)) {
       TypeConverter.IGNORE
    }
    return val;
  }
}

// And the beans definition - using Grails Spring DSL, the result is the same as xml
phoneMaskTypeConverter(com.felipecypriano. MaskFilterTypeConverter){ bean ->
    bean.maskToAvoid = "(__) ____-____"
}

zipCodeMaskTypeConverter(com.felipecypriano. MaskFilterTypeConverter){ bean ->
    bean.maskToAvoid = "_____-___"
}

And in the zul file:

<textbox value="@{phoneNumber, converter=phoneMaskTypeConverter}"/>
<textbox value="@{zipCode, converter=zipCodeMaskTypeConverter}"/>

Is it possible? If it's not possible what need to be done to make it possible?

link publish delete flag offensive edit

answered 2010-04-15 20:54:58 +0800

henrichen gravatar image henrichen
3869 2
ZK Team

Currently, the converter accepts "converter class name only"(i.e. Sorry, no spring bean).

However, you can pass extra information into the converter via data binder arguments.
In zul file:

<textbox value="@{phoneNumber, converter=com.mypackage.PhoneNumberConverter, maskToAvoid='(__) ____-____'}"/>

In your converter,

public class PhoneNumberConverter implements TypeConverter {
  public Object coerceToBean(java.lang.Object val, Component comp) {
    Map args = (Map) comp.getAttribute(DataBinder.ARGS); //get the databinder argument map
    if (args != null) {
      String maskToAvoid = (String) args.get("maskToAvoid"); //get the argument named "maskToAvoid"
      if (maskToAvoid != null && maskToAvoid.equals(val)) {
        return TypeConverter.IGNORE
      }
      return val;
    }
    ...
  }
  ...
}

link publish delete flag offensive edit

answered 2010-04-16 06:21:29 +0800

fmcypriano gravatar image fmcypriano
612 1 7
http://felipecypriano.com...

updated 2010-04-16 06:21:56 +0800

The only problem using arguments is that value in 'maskToAvoid' needs to be hard coded everywhere, this can easily become a maintenance nightmare.

Don't you have any tips on where to look (which classes) so I could implement this functionality?

link publish delete flag offensive edit

answered 2010-04-26 19:46:33 +0800

henrichen gravatar image henrichen
3869 2
ZK Team

Maybe you want to consider "Common super class and subclass for each mask" strategy.

link publish delete flag offensive edit

answered 2010-04-27 06:12:28 +0800

fmcypriano gravatar image fmcypriano
612 1 7
http://felipecypriano.com...

Hi Henri,

Yes, this is what I did. I've a super class named StringFiterConverter and subclasses like PhoneMaskFilterConverter:

simple groovy code:

class StringFilterConverter implements TypeConverter {
    final String stringToAvoid
    // coerceToBean implementation
}

class PhoneMaskFilterConverter extends StringFilterConverter {
    public PhoneMasFilterConverter() {
        super("(__) ____-____");
    }
}

I don't his option is better than using spring beans, it's more expensive. But, it's all we have for now.

link publish delete flag offensive edit

answered 2010-04-30 08:44:49 +0800

liglesia gravatar image liglesia
6

Hi,
did you try something like this with zk+grails?
I want to show a formatted date from a grails domain class in a zk page but I don't know how.
Thank you,
Lucía

link publish delete flag offensive edit

answered 2010-04-30 13:13:01 +0800

fmcypriano gravatar image fmcypriano
612 1 7
http://felipecypriano.com...

If your grails domain class has a property (getter and/or setter) which returns a java.util.Date the code will be the same as using a Java class.
Look at the ZK components, like Datebox.

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2009-06-02 15:37:37 +0800

Seen: 950 times

Last updated: Apr 30 '10

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More