Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 2 jsf2 ViewScope托管bean多次启动_Jsf 2_Cdi_View Scope - Fatal编程技术网

Jsf 2 jsf2 ViewScope托管bean多次启动

Jsf 2 jsf2 ViewScope托管bean多次启动,jsf-2,cdi,view-scope,Jsf 2,Cdi,View Scope,我对视图范围的bean有一个问题。我知道这是一个常见的问题,但我并没有得到正确的答案。我有一个datatable,负责在用户输入搜索条件时显示搜索结果。 这里是xhtml: <html xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns="http://www.w3.org/1999/xhtml" xmlns:p="http://primefac

我对视图范围的bean有一个问题。我知道这是一个常见的问题,但我并没有得到正确的答案。我有一个datatable,负责在用户输入搜索条件时显示搜索结果。 这里是xhtml:

<html   xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns="http://www.w3.org/1999/xhtml"

    xmlns:p="http://primefaces.org/ui">


<h:panelGroup id ="adminReportLogList">

<p:dataTable value="#{adminLogView.lazyModelReport}" var="model" id="reportLogList" lazy="true" paginator="true" rows="20" paginatorAlwaysVisible="false" paginatorPosition="bottom" emptyMessage="emppty list" styleClass="dtable" style="table-layout: fixed; width: 100%"
             paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}" rowsPerPageTemplate="5,10,15,20">

    <p:column headerText="Report number" sortBy="#{model.reportNumber}">
        <h:outputText value="#{model.reportLogId}"/>
    </p:column>
    <p:column headerText="Report date" sortBy="#{model.reportDate}">
            <h:outputText value="#{model.reportDate}">
                <f:convertDateTime pattern="yyyy/MM/dd hh:mm:ss"/>
            </h:outputText>
    </p:column>
    <p:column headerText="Search date" sortBy="#{model.searchLogId.searchDate}">
        <h:outputText value="#{model.searchLogId.searchdate}">
                <f:convertDateTime pattern="yyyy/MM/dd hh:mm:ss"/>
            </h:outputText>
    </p:column>
    <p:column headerText="User name" sortBy="#{model.searchLogId.loginLogId.username}">
        <h:outputText value="#{model.searchLogId.loginLogId.username}"/>
    </p:column>
    <p:column headerText="Customer number" sortBy="#{model.customerId}">
        <h:outputText value="#{model.customerId.registernumber}"/>
    </p:column>
    <p:column headerText="Customer name" sortBy="#{model.customerId}">
        <h:outputText value="#{model.customerId.name}"/>
    </p:column>
</p:dataTable>

</h:panelGroup>
<br/>

// SEARCH PANEL
<p:panel>
    <h:panelGrid columns="3" styleClass="insGrid" id="reportLogSearchPanel">
        <h:outputText value="User: "/>
        <p:inputText id="reportLogSearchByUserName" value="#{adminLogView.searchUserName}">
            <p:watermark for="reportLogSearchByUserName" value ="Customer name"/>
        </p:inputText>
        <h:message for="reportLogSearchByUserName" id="msgReportLogSearchByUserName" styleClass="errorMessage"/>

        <h:outputText value="Customer id number: "/>
        <p:inputText id="reportLogSearchByCustomerName" value="#{adminLogView.searchCustomerName}">
            <p:watermark for="reportLogSearchByCustomerName" value="Customer id number"/>
        </p:inputText>
        <h:message for="reportLogSearchByCustomerName" id="msgReportLogSearchBySearchDate" styleClass="errorMessage"/>

        <h:outputText value="Report date "/>
        <p:inputText id="reportLogSearchByReportDate" value="#{adminLogView.searchReportDate}">
            <p:watermark for="reportLogSearchByReportDate" value="Report date YYYY/MM/DD"/>
        </p:inputText>
        <h:message for="reportLogSearchByReportDate" id="msgReportLogSearchByReportDate" styleClass="errorMessage"/>

        <h:panelGroup/>
        <h:commandButton value="Search" styleClass="btn" action="#{adminLogView.searchReport()}">
            <f:ajax render =":form:adminReportLogList :form:reportLogList" execute=":form:reportLogSearchPanel"/>
        </h:commandButton>

    </h:panelGrid>

</p:panel>


//搜索面板

在另一个xhtml的标记中使用了:/我认为这不是本例的原因/

<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                template="./../templates/admin_main.xhtml"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:p="http://primefaces.org/ui">

    <ui:define name="content">
        <h:panelGroup id="include">
        <ui:include src="#{adminLogView.page}.xhtml"/>
        </h:panelGroup>
    </ui:define>

</ui:composition>

这是我的豆子

@Named(value = "adminLogView")
@ViewScoped
public class AdminLogView implements Serializable{
    @EJB
    private LogServiceLocal logService;
    private List<Reportlog> ReportLogList;

    String page = "reportLog";
    LazyDataModel<Reportlog> lazyModelReport;
    LazyDataModel<Loginlog>  lazyModelLogin;
    LazyDataModel<Searchlog> lazyModelSearch;

    String searchCustomerName;
    String searchUserName;
    String searchReportDate;


    @PostConstruct
    public void init(){

        this.lazyModelReport = new LazyReportLogDataModel(this.logService);
    }

    public void searchReport(){
        Map<String, String> filter = new HashMap<String, String>();
        if(this.searchCustomerName != null && !this.searchCustomerName.isEmpty())
            filter.put("customerName", this.searchCustomerName);
        if(this.searchReportDate != null && !this.searchReportDate.isEmpty())
            filter.put("reportDate", this.searchReportDate);
        if(this.searchUserName != null && !this.searchUserName.isEmpty())
            filter.put("userName", this.searchUserName);



        this.lazyModelReport.load(0, 30, null, SortOrder.UNSORTED, filter);

    }
}
@Named(value=“adminLogView”)
@视域
公共类AdminLogView实现可序列化{
@EJB
私人日志服务本地日志服务;
私有列表报告日志列表;
String page=“reportLog”;
LazyDataModel lazyModelReport;
LazyDataModel lazyModelLogin;
LazyDataModel lazyModelSearch;
字符串搜索客户名称;
字符串搜索用户名;
字符串搜索报告日期;
@施工后
公共void init(){
this.lazyModelReport=新的LazyReportLogDataModel(this.logService);
}
公共报告{
Map filter=newhashmap();
if(this.searchCustomerName!=null&!this.searchCustomerName.isEmpty())
filter.put(“customerName”,this.searchCustomerName);
if(this.searchReportDate!=null&!this.searchReportDate.isEmpty())
filter.put(“reportDate”,this.searchReportDate);
if(this.searchUserName!=null&&!this.searchUserName.isEmpty())
filter.put(“用户名”,this.searchUserName);
this.lazyModelReport.load(0,30,null,SortOrder.UNSORTED,filter);
}
}
当我们导航到上面的页面时,会多次调用@postConstruct方法,即使使用sessionScoped。即使在同一视图中单击搜索按钮,bean也会再次初始化。只有RequestScope工作正常。 是我误解了还是忘记了。附言
提前谢谢

你有正确的治疗方案吗?javax.enterprise.context.SessionScoped

在类路径上有Myfaces CODI或任何其他适配器吗?否则@ViewScoped具有未记录的行为,因为它是JSF-2作用域

如果您有正确的
@SessionScoped
,那么所记录的行为至少对我来说是完全未知的问题

此外:

这里给出的值是默认值。不妨跳过这样的设置值


祝你好运

你有正确的疗程吗?javax.enterprise.context.SessionScoped

在类路径上有Myfaces CODI或任何其他适配器吗?否则@ViewScoped具有未记录的行为,因为它是JSF-2作用域

如果您有正确的
@SessionScoped
,那么所记录的行为至少对我来说是完全未知的问题

此外:

这里给出的值是默认值。不妨跳过这样的设置值


祝你好运

@javax.faces.bean.ViewScoped无法与CDI一起工作。因此,您需要使用SeamFaces或MyFaces CODI从viewscope服务中获益

CDI提供了创建您自己的范围的扩展,因此您可以实现上下文并使用@NormalScope来实现这一点

  • CDI在每次bean调用后在bean发现后激发一个事件
  • 您可以使用CDI扩展来观察此事件,并添加上下文实现
  • 在范围实施中,您可以:
  • 使用contextFacesContext ViewRoot映射中获取bean的名称,并在每次ajax回调后返回
  • 如果未找到第一步中的bean名称,请使用CreationalContextFacesContext视图根映射中创建它

要获得更深入的解释,我建议使用此链接:

@javax.faces.bean.ViewScoped不能与CDI一起使用。因此,您需要使用SeamFaces或MyFaces CODI从viewscope服务中获益

CDI提供了创建您自己的范围的扩展,因此您可以实现上下文并使用@NormalScope来实现这一点

  • CDI在每次bean调用后在bean发现后激发一个事件
  • 您可以使用CDI扩展来观察此事件,并添加上下文实现
  • 在范围实施中,您可以:
  • 使用contextFacesContext ViewRoot映射中获取bean的名称,并在每次ajax回调后返回
  • 如果未找到第一步中的bean名称,请使用CreationalContextFacesContext视图根映射中创建它

要获得更深入的解释,我建议使用此链接:

这篇博文中的描述太简单了。作为一名开发人员,我更喜欢看可用的代码。就像Codi或DeltaSpike中的一样。我个人只是需要在每次ajax回调后检索bean,Steven Verborgh的实现对我很有用,并进行了一些调整,这就是我所说的。如果我能得到一些涵盖特殊情况的东西,我不会关注那些只显示一半真相的博客。谢谢@darWhi我想我应该发布一个使用cdi扩展的viewscope的完整实现。这篇博客文章中的描述太简单了。作为一名开发人员,我更喜欢看可用的代码。就像Codi或DeltaSpike中的一样。我个人只是需要在每次ajax回调后检索bean,Steven Verborgh的实现对我很有用,并进行了一些调整,这就是我所说的。我不关注那些只显示一半真相的博客,如果我能得到的话
@Named(value = "adminLogView")