【问题标题】:Using convertDateTime tag in DataTable component and every ajax requests re-creating Managed Bean在 DataTable 组件中使用 convertDateTime 标记,并且每个 ajax 请求重新创建托管 Bean
【发布时间】:2012-05-14 10:49:18
【问题描述】:

我在 DataTable 组件中使用了 convertDateTime 标记。将托管 bean 属性连接到时区属性。在这种情况下,每个 ajax 都会请求重新创建托管 Bean。

Managed Bean 范围是 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;
   }

在每个 ajax 请求之后显示以下输出:“PostConstruct 方法被调用...”

您对在每个请求上重新创建 bean 有什么想法吗?

注意:我为我糟糕的英语道歉:)

【问题讨论】:

    标签: jsf-2 managed-bean date-conversion ajax-request


    【解决方案1】:

    当您将标记处理程序的属性绑定到视图范围的 bean 时,确实会发生这种情况。这与 JSF issue 1492 相关,这是为即将到来的 JSF 2.2 修复的。简而言之,视图范围的 bean 存储在视图状态中。因此,当要恢复视图时,视图范围的 bean 尚不可用。但是标记处理程序(以及 JSF 组件的 idbinding 属性)在构建视图期间运行,因此它将隐式创建一个全新的视图范围 bean 实例。然而,在恢复视图之后,那些新的视图范围 bean 实例将被视图状态中的那些替换。另见@ViewScoped fails in tag handlers

    有几种方法可以解决这个特定问题,所有方法都在这个答案中列出:How to set converter properties for each row of a datatable? 但在你的特殊情况可能有一个更简单的解决方案:你似乎想使用系统的所有日期时间转换器中的默认时区。您也可以通过在web.xml 中设置以下上下文参数来实现此目的:

    <context-param>
        <param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name>
        <param-value>true</param-value>
    </context-param>
    

    【讨论】:

    • 嗨 BalusC。感谢您的启发性回应。我已经使用了 context 参数,但这不足以获得准确的结果。您的共享链接将仔细阅读。再次感谢您...
    猜你喜欢
    • 2015-01-14
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2011-04-26
    • 2012-09-11
    • 2013-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多