>From: "Konstantin Shaposhnikov" <[EMAIL PROTECTED]>
>
> Hello,
>
> I am developing web application using facelets and have problem, that
> I do not the best way to solve.
>
> Consider following example:
>
> I have managed bean registered under name "locales" of following class:
> Locales {
> Locale[] getLocales() {
> // return array of some locales
> }
> }
>
> And have following code in template:
>
><h:dataTable value="#{locales.locales}" var="locale">
>  <h:column>
>    <h:outputText value="#{locale}">
>    <h:outputText value="#{currentDate}">
>      <f:convertDateTime locale="#{locale}" dateStyle="long"/>
>    </h:outputText>
>  </h:column>
></h:dataTable>
&g t;
>
>
> But it just doesn't work neither in JSP nor in facelets. In JSP date
> is always displayed formatted in current locale, in facelets exception
> is thrown that locale can not be null.
>
> If I understand JSF component model right there is only one
> h:outputText component is available in the tree and it can't have
> different converters every time it is rendered by t:dataTable.
> Facelets version that uses c:forEach instead of h:dataTable works
> perfectly, because it creates several components in JSF tree.
>
> Now I see only one solution for this problem: pass _expression_ itself
> into converter and reevaluate this _expression_ every time when
> conversion is performed. Fortunately I need this behaviour for my
> custom converter (not for convertDateTime that I have shown in example
> above).
>
> I know how to do it (I mean pass _expression_ instead of object) in JSP,
> but I am not sure how it can be done in facelets (I am using it only
> for 2 days :). Is it possible to write custom ConvertHandler to do it?
>

Using the converter in a dataTable will not work this way.  The DateTimeConverter's locale property doesn't support a value binding _expression_.  The components in the dataTable are created only once and used for all rows.  

You could create a custom converter to do this and bind your column outputText to a backing bean to avoid a custom JSP tag.  Consider:

JSP:

  <h:dataTable value="#{locale$select.supportedLocales}" var="locale">
  <h:column>
    <h:outputText value="#{e.displayName}"/>
    <h:outputText binding="#{locale$select.output}" value="#{locale$select.current}">
    </h:outputText>
  </h:column>
 </h:dataTable>


Backing Bean registered as "locale$select".

  private HtmlOutputText output = null;
    public HtmlOutputText getOutput() {
        if (output == null) {
            output = new HtmlOutputText();   
            FacesContext context = FacesContext.getCurrentInstance();
            context.getApplication().addConverter("org.apache.shale.usecases.locale.CustomDateTimeConverter", "org.apache.shale.usecases.locale.CustomDateTimeConverter");
            CustomDateTimeConverter converter = (CustomDateTimeConverter) context.getApplication().createConverter("org.apache.shale.usecases.locale.CustomDateTimeConverter");
            converter.setDateStyle("long");
            ValueBinding vb = context.getApplication().createValueBinding("#{locale}");
            converter.setLocaleVB(vb);
            output.setConverter(converter);            
        }
       
        return output;
    }
    public void setOutput(HtmlOutputText output) {
       this.output = output;   
    }

Subclass DateTimeConverter:

public class CustomDateTimeConverter extends DateTimeConverter {
   
   
    private ValueBinding localeVB = null;
    public void setLocaleVB(ValueBinding localeVB) {
        this.localeVB = localeVB;       
    }
   
    public Locale getLocale() {
        FacesContext context = FacesContext.getCurrentInstance();
        Locale locale = (Locale) localeVB.getValue(context);
        if (locale == null)
            locale = super.getLocale();
       
        return locale;
    }
   
}


> Best regards,
> Konstantin

Gary

 

>
> --
> http://step-inside.org

Reply via email to