ViewScoped Bean在commandLink上重建

时间:2018-10-08 09:15:30

标签: jsf primefaces view

我正在使用仪表板构建CRUD应用程序,用户应该可以在其中部分重载窗口小部件。这就是我得到的:

   <f:metadata>
    <f:viewParam name="customerUuid" value="# 
    {dashboardManagedBean.customerUuid}" />
    <f:event type="preRenderView" listener="#{dashboardManagedBean.init}" />
   </f:metadata>

<f:view>
    <h:body>
        <h:form>
            <p:panelGrid>
                <p:row>
                    <p:column>
        <p:panelGrid columns="4" styleClass="ui-noborder" 
        style="float:right;">    

                            <p:outputLabel for="dateFrom" value="From:"> 
       </p:outputLabel>
                            <p:calendar id="dateFrom" value="# 
       {dashboardManagedBean.statisticsRequest.dateFrom}" 
       pattern="dd.MM.yyyy HH:mm:ss" locale="de" readonlyInput="false" 
        timeInput="true" mode="popup"/>

                            <p:outputLabel for="dateTo" value="Till:"> 
      </p:outputLabel>
                            <p:calendar id="dateTo" value="# 
     {dashboardManagedBean.statisticsRequest.dateTo}" pattern="dd.MM.yyyy 
     HH:mm:ss" locale="de" readonlyInput="false" timeInput="true" 
     mode="popup"/>
                        </p:panelGrid>

                    </p:column>
                </p:row>           
                <p:row>
                    <p:column colspan="3">
                        <p:panel id="telemetry" header="Telemetrie" 
    style="width: 100%" collapsed="true" toggleable="true">
                            <f:facet name="actions">
                                <p:commandLink
                                        actionListener="# 
   {dashboardManagedBean.loadData}"
                                        styleClass="ui-panel-titlebar-icon 
   ui-corner-all ui-state-default"><h:outputText
                                        styleClass="fa fa-refresh"/> 
   </p:commandLink>
                            </f:facet>
                        </p:panel>
                    </p:column>
                </p:row>

            </p:panelGrid>

        </h:form>
    </h:body>
</f:view>

bean看起来像这样:

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;

@ManagedBean(name = "dashboardManagedBean")
@ViewScoped @Getter @Setter
public class DashboardManagedBean implements Serializable {

    private Customer customer;
    private StatisticsRequest statisticsRequest;
    private String customerUuid;

    public DashboardManagedBean()  {
        this.customer = new Customer();
        this.statisticsRequest = new StatisticsRequest();
    }

    public void init() {
     loadCustomer();
     loadData();
    }

    public void loadData(){
        // load the customer data by using his the statisticsRequest

    }

    public void loadCustomer(){
        // load the customer
    }
}

StatisticsRequest类只是请求数据的包装器:

@Getter @Setter
public class StatisticsRequest {
    private Customer customer;
    private Date dateFrom;
    private Date dateTo;

    public StatisticsRequest(){
        java.util.Date da = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(da);
        cal.add(Calendar.MONTH, -4);
        this.dateFrom = cal.getTime();

        this.dateTo = new Date();
    }
}

我将其放入StatisticsRequest的构造函数中,以便在加载页面时获得合理的默认值。但是,每当我单击reload命令时,都会调用viewscoped bean的构造函数。链接删除我在日历中选择的日期,并且StatisticsRequest再次包含默认值。

为什么会这样?

0 个答案:

没有答案