Question

I'm using convertDateTime tag in DataTable component. Connected managed bean property to timezone attribute. In this situation, every ajax requests re-creating Managed Bean.

The Managed Bean scope is View Scoped.

<h:column>
    <f:facet name="header">
       <h:outputLabel value="Date"/>
    </f:facet>
    <h:outputText value="#{item.date}">
         <f:convertDateTime timeZone="#{myBean.timezone}" locale="tr" pattern="dd.MM.yyyy"/>
    </h:outputText>
</h:column>




@ManagedBean(name="myBean")
@ViewScoped
public class MyBean {

   @PostConstruct
   public void initBeanMethod(){
      System.out.println("PostConstruct method is called...");
   }

   private TimeZone timezone = TimeZone.getDefault();

   public TimeZone getTimezone() {
      return timezone;
   }

   public void setTimezone(TimeZone timezone) {
      this.timezone = timezone;
   }

Shows the following output after each ajax request: "PostConstruct method is called..."

Do you have an idea about Re-creation of beans on each request?

Note: I apologize for my bad english :)

Was it helpful?

Solution

That will indeed happen when you bind attributes of tag handlers to a view scoped bean. This is related to JSF issue 1492 which is fixed for the upcoming JSF 2.2. In a nutshell, view scoped beans are stored in the view state. So when the view is to be restored, the view scoped beans are not available yet. But tag handlers (and id and binding attributes of JSF components) runs during building the view, so it will implicitly create a brand new view scoped bean instance. After restoring the view, those new view scoped bean instances will however be replaced by the ones in the view state. See also @ViewScoped fails in tag handlers.

There are several ways to fix this particular problem, all are outlined in this answer: How to set converter properties for each row of a datatable? But in your particular case there's probably a much easier solution: you seem to want to use the system's default timezone in all datetime converters. You can also achieve this by just setting the following context parameter in web.xml:

<context-param>
    <param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name>
    <param-value>true</param-value>
</context-param>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top