Attributes for Custom Converters in JSF

Discussions

Web tier: servlets, JSP, Web frameworks: Attributes for Custom Converters in JSF

  1. Attributes for Custom Converters in JSF (4 messages)

    Good day to all.. Any body knows how to implement a custom JSF converter with some attribute, as in the case of ?. [Note here we have an attribute, 'pattern'] For custom converter, it would be . No nested attributes supported by this tag []. How can I pass some additional parameters to this converter ? Thanks in advance.
  2. Use Spring[ Go to top ]

    In your jsf use for example : configure the converter in spring like : vat.BooleanConverter nl.wowww.web.pagecode.vat.BooleanConverter and make your own converter like : public class BooleanConverter extends nl.wowww.common.jsf.BooleanConverter { public String getAsString(FacesContext context, UIComponent component, Object object) { if (super.getAsString(context, component, object) == null) { if ("true".equals(object.toString()) || "false".equals(object.toString())) { return object.toString(); } else { return null; } } else { return super.getAsString(context, component, object); } } }
  3. The first thing I always encourage people to do is look at some of the component frameworks (e.g. Trinidad) to get converters. Most converters you will need are already out there for most data types. The other thing I encourage you to do is use the "validate" attribute associated with most input components. This lets you specific a method binding. While it does not help in support of dynamically changing a pattern, it is the simplest way to add "custom" converters. Often the pattern needed to be used by the validation method can be found elsewhere and does not have to be provided as part of the tag. If all else fails, I would get an example of a custom converter that has been written and canabilize that.
  4. See Exercise 3, "Build and run 'core-custom-converter' sample application" here: JSF Converter, Sang Shin, Sun Microsystems, www.javapassion.com/j2ee
  5. Do not know if you have a solution... But I did like this.... Create a tag that have a converter. Register a tag in faces-config and use the new tag in a component. faces-config.xml: numberConverter br.com.softcomex.components.gui.presentation.converters.NumberConverter maxFractionDigitsMask java.lang.Integer taglib.xml: http://www.site.com/jsf maskNumberConverter numberConverter xhtml: Converter Java: public class NumberConverter implements Converter { /** * Número máxmimo de dígitos depois da vírgula. Passado como parâmetro pela tela. */ private int maxFractionDigitsMask; /** * Passado como parametro pela tela. * @return Número Máximo de Digitos. */ public int getMaxFractionDigitsMask() { return maxFractionDigitsMask; } /** * Passado como parametro pela tela. * @param maxFractionDigitsMask Número máximo de digitos depois da vírgula. */ public void setMaxFractionDigitsMask(int maxFractionDigitsMask) { this.maxFractionDigitsMask = maxFractionDigitsMask; } /** * Construtor. */ public NumberConverter() { this.setMaxFractionDigitsMask(-1); } /** * Conversão de String para BigDecimal. * @param context FacesContext for the request being processed. * @param component UIComponent with which this model object value is associated. * @param value String value to be converted (may be null). * @return Resultado da conversão Number. * @see javax.faces.convert.Converter#getAsObject(javax.faces.context.FacesContext, * javax.faces.component.UIComponent, java.lang.String) */ public Object getAsObject(FacesContext context, UIComponent component, String value) { Locale locale = context.getViewRoot().getLocale(); DecimalFormat decimal = (DecimalFormat) NumberFormat.getInstance(locale); decimal.setParseBigDecimal(true); Object oReturn = null; try { oReturn = decimal.parseObject(value); } catch (ParseException e) { throw new SfwSystemException(e); } return oReturn; } /** * Conversão de BigDecimal para String. * @param context FacesContext for the request being processed * @param component UIComponent with which this model object value is associated * @param value Model object value to be converted (may be null) * @return Resultado da conversão * @see javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext, * javax.faces.component.UIComponent, java.lang.Object) */ public String getAsString(FacesContext context, UIComponent component, Object value) { String sReturn = null; if (value != null && value.toString().length() > 0) { double dNumber = Double.parseDouble(value.toString()); Locale locale = context.getViewRoot().getLocale(); DecimalFormat decimal = (DecimalFormat) NumberFormat.getInstance(locale); if (this.maxFractionDigitsMask != -1) { decimal.setMaximumFractionDigits(this.maxFractionDigitsMask); } sReturn = decimal.format(dNumber); } return sReturn; } }