当前位置: 首页 > 知识库问答 >
问题:

多次调用CDI bean构造函数和@PostConstruct

黄仲渊
2023-03-14

经过一些实验,这似乎是使用CDI bean时的一个问题,而与我最初假设的PrimeFaces无关。

===============================================================================

我对JSF和PrimeFaces还很陌生,因此任何指导都将非常感谢。我已经搜索了其他的帖子,并阅读了许多类似的,但没有找到解决方法。

我正试图让PrimeFaces展示站点上的懒惰加载数据表示例在我的项目中工作,但由于bean类的行为不符合我的预期,我遇到了问题。

我对来自PrimeFaces站点的示例代码做了一些更改:-将TableBean类重命名为TableBeanLazy;向类添加注释;分配的静态字符串数组;将初始化从构造函数移到@PostConstruct initialise()方法;分配了cars成员变量。

我还向构造函数和@PostConstruct方法添加了跟踪消息,这些消息表明,当表首次显示时,这两个函数都会被调用多次,每次单击搜索图标都会被调用多次,筛选字段中的每个字符项都会被调用多次。

这会导致每次执行操作时重新创建Car对象的随机列表,因此排序和筛选永远不会产生预期的结果--当直接在PrimeFaces Showcase站点上运行示例时就会产生预期的结果。

虽然我最初使用的是@ViewScoped,但我也尝试过@RequestScoped,最后尝试过@SessionScoped,但所有的行为都是一样的。

对构造函数和@PostConstruct的多次调用是bean的预期行为吗?

如果是这样,那么在PrimeFaces展示站点上给出的示例代码可能如何工作呢?

我该如何解决这个问题?

环境

PrimeFaces 3.5
JSF 2.1
JDK 1.7
GlassFish Server Open Source Edition 3.1.2.2 (build 5)
Mojarra 2.1.6 (SNAPSHOT 20111206)
Netbeans IDE 7.3
Windows 7 Pro x64
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

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

  <ui:define name="body">
    <h2 style="margin-top: 0px; padding-top: 0px;">Cars List</h2>

    <h:form id="form">
      <p:dataTable var="car" value="#{tableBeanLazy.lazyModel}" paginator="true" rows="10"
        paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
        rowsPerPageTemplate="5,10,15" selectionMode="single" selection="#{tableBeanLazy.selectedCar}" id="carTable" lazy="true">

      <p:column headerText="Model" sortBy="#{car.model}" filterBy="#{car.model}">
        <h:outputText value="#{car.model}" />
      </p:column>

      <p:column headerText="Year" sortBy="#{car.year}" filterBy="#{car.year}">
        <h:outputText value="#{car.year}" />
      </p:column>

      <p:column headerText="Manufacturer" sortBy="#{car.manufacturer}" filterBy="#{car.manufacturer}">
        <h:outputText value="#{car.manufacturer}" />
      </p:column>

      <p:column headerText="Color" sortBy="#{car.color}" filterBy="#{car.color}">
        <h:outputText value="#{car.color}" />
      </p:column>
      </p:dataTable>
    </h:form> 
  </ui:define>
</ui:composition>
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BackEnd;

import java.util.ArrayList;
import java.util.List;  
import javax.annotation.PostConstruct;
import javax.faces.bean.SessionScoped;
import javax.inject.Named;

import org.primefaces.model.LazyDataModel;  

@Named(value = "tableBeanLazy")
@SessionScoped
public class TableBeanLazy
{
  static String[] colors = new String[10];
  static String[] manufacturers = new String[10];
  static String[] models = new String[5];

  private LazyDataModel<Car> lazyModel;  

  private Car selectedCar;  

  private List<Car> cars;  

  static
  {
    colors[0] = "Black";  
    colors[1] = "White";  
    colors[2] = "Green";  
    colors[3] = "Red";  
    colors[4] = "Blue";  
    colors[5] = "Orange";  
    colors[6] = "Silver";  
    colors[7] = "Yellow";  
    colors[8] = "Brown";  
    colors[9] = "Maroon";  

    manufacturers[0] = "Mercedes";  
    manufacturers[1] = "BMW";  
    manufacturers[2] = "Volvo";  
    manufacturers[3] = "Audi";  
    manufacturers[4] = "Renault";  
    manufacturers[5] = "Opel";  
    manufacturers[6] = "Volkswagen";  
    manufacturers[7] = "Chrysler";  
    manufacturers[8] = "Ferrari";  
    manufacturers[9] = "Ford";

    models[0] = "Sports";
    models[1] = "Saloon";
    models[2] = "SUV";
    models[3] = "Hybrid";
    models[4] = "Estate";
  }  

  public TableBeanLazy()
  {
    System.out.println("-->  In constructor for TableBeanLazy");
  }

  @PostConstruct
  public void initialise()
  {
    System.out.println("-->  In initialise for TableBeanLazy");

    cars = new ArrayList<Car>();

    populateRandomCars(cars, 5);

    lazyModel = new LazyCarDataModel(cars);  
  }

  public Car getSelectedCar()
  {
    return selectedCar;
  }  

  public void setSelectedCar(Car selectedCar)
  {
    this.selectedCar = selectedCar;
  }

  public LazyDataModel<Car> getLazyModel()
  {
    return lazyModel;
  }

  private void populateRandomCars(List<Car> list, int size)
  {
    System.out.println("-->  In populateRandomCars for TableBeanLazy");

    for(int i = 0 ; i < size ; i++)
    {
      list.add(new Car(getRandomModel(), getRandomYear(), getRandomManufacturer(), getRandomColor()));  
    }  
  }  

  private String getRandomColor()
  {
    return colors[(int)(Math.random() * 10)];
  }

  private String getRandomManufacturer()
  {
    return manufacturers[(int) (Math.random() * 10)];
  }

  private String getRandomModel()
  {
    return models[(int)(Math.random() * 5)];
  }

  private int getRandomYear()
  {
    return (int)(Math.random() * 50 + 1960);
  }
}
INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: java.lang.ArithmeticException: / by zero
java.lang.ArithmeticException: / by zero
  at org.primefaces.model.LazyDataModel.setRowIndex(LazyDataModel.java:62)
  at org.primefaces.component.api.UIData.setRowModel(UIData.java:409)
  at org.primefaces.component.api.UIData.setRowIndex(UIData.java:401)
  at org.primefaces.component.api.UIData.processChildren(UIData.java:289)
  at org.primefaces.component.api.UIData.processPhase(UIData.java:261)
  at org.primefaces.component.api.UIData.processDecodes(UIData.java:227)
  at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:506)
  at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
  at org.primefaces.component.api.UIData.visitTree(UIData.java:639)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIForm.visitTree(UIForm.java:344)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:376)
  at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:252)
  at javax.faces.context.PartialViewContextWrapper.processPartial(PartialViewContextWrapper.java:183)
  at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:931)
  at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:78)
  at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
  at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
  at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
  at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
  at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
  at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
  at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
  at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
  at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
  at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
  at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
  at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
  at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
  at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
  at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
  at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
  at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
  at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
  at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
  at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
  at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
  at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
  at java.lang.Thread.run(Thread.java:722)

INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: java.lang.ArithmeticException: / by zero
java.lang.ArithmeticException: / by zero
  at org.primefaces.model.LazyDataModel.setRowIndex(LazyDataModel.java:62)
  at org.primefaces.component.api.UIData.setRowModel(UIData.java:409)
  at org.primefaces.component.api.UIData.setRowIndex(UIData.java:401)
  at org.primefaces.component.api.UIData.processChildren(UIData.java:289)
  at org.primefaces.component.api.UIData.processPhase(UIData.java:261)
  at org.primefaces.component.api.UIData.processValidators(UIData.java:241)
  at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:508)
  at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
  at org.primefaces.component.api.UIData.visitTree(UIData.java:639)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIForm.visitTree(UIForm.java:344)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:376)
  at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:252)
  at javax.faces.context.PartialViewContextWrapper.processPartial(PartialViewContextWrapper.java:183)
  at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1170)
  at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
  at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
  at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
  at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
  at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
  at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
  at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
  at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
  at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
  at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
  at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
  at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
  at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
  at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
  at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
  at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
  at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
  at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
  at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
  at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
  at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
  at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
  at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
  at java.lang.Thread.run(Thread.java:722)

INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: java.lang.ArithmeticException: / by zero
java.lang.ArithmeticException: / by zero
  at org.primefaces.model.LazyDataModel.setRowIndex(LazyDataModel.java:62)
  at org.primefaces.component.api.UIData.setRowModel(UIData.java:409)
  at org.primefaces.component.api.UIData.setRowIndex(UIData.java:401)
  at org.primefaces.component.api.UIData.processChildren(UIData.java:289)
  at org.primefaces.component.api.UIData.processPhase(UIData.java:261)
  at org.primefaces.component.api.UIData.processUpdates(UIData.java:253)
  at org.primefaces.component.datatable.DataTable.processUpdates(DataTable.java:550)
  at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:510)
  at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
  at org.primefaces.component.api.UIData.visitTree(UIData.java:639)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIForm.visitTree(UIForm.java:344)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
  at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:376)
  at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:252)
  at javax.faces.context.PartialViewContextWrapper.processPartial(PartialViewContextWrapper.java:183)
  at javax.faces.component.UIViewRoot.processUpdates(UIViewRoot.java:1229)
  at com.sun.faces.lifecycle.UpdateModelValuesPhase.execute(UpdateModelValuesPhase.java:78)
  at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
  at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
  at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
  at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
  at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
  at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
  at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
  at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
  at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
  at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
  at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
  at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
  at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
  at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
  at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
  at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
  at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
  at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
  at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
  at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
  at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
  at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
  at java.lang.Thread.run(Thread.java:722)

INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy
INFO: -->  In constructor for TableBeanLazy
INFO: -->  In initialise for TableBeanLazy
INFO: -->  In populateRandomCars for TableBeanLazy

共有1个答案

洪德寿
2023-03-14

这里是关于CDI和JSF的第一条建议--永远不要将CDI和JSF注释混合在一起。

您的问题是使用@javax.inject.named一个CDI注释和javax.faces.bean.sessionscoped一个JSF注释。

在CDI中,您可以执行以下操作:

import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

@Named(value = "tableBeanLazy")
@SessionScoped
public class TableBeanLazy {...}

以纯JSF的方式:

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

@ManagedBean(value = "tableBeanLazy")
@SessionScoped
public class TableBeanLazy {...}

我建议一直坚持使用CDI。但是,CDI目前并没有为JSF@javax.faces.bean.viewscoped提供现成的替代方法。要在CDI中得到它,请查看Apache Deltaspike。

 类似资料:
  • 我是新的反应,我试图用它开发一个简单的网络应用程序,但我得到了一个错误。当我加载一个类组件时,我的构造函数被调用两次,你能帮忙吗? 原木地被击中两次。在一些组件中,我希望进行一个命中DB的API调用,但我只想命中它一次。在许多情况下,使用并不方便,因为道具只出现在之后,因此我想在中调用(我不会使用setState,这会导致重新加载render)。 提前感谢

  • 问题内容: 我是AP Java学生,正在为考试做练习。我遇到了这个问题,但我不明白答案: 考虑以下类别: 执行以下代码后输出是什么: 正确答案是B *。有人可以向我解释方法调用的顺序吗? 问题答案: B构造函数被调用。B构造函数的第一条隐式指令是(调用超类的默认构造函数)。因此,调用了A的构造函数。A的构造函数调用,它调用java.lang.Object构造函数,该构造函数不输出任何内容。然后被称

  • 我有一个帖子列表,我试图在构造函数或componentDidMount中为每个帖子调用一个操作。但不知何故,当我发送一个新消息时,构造函数和componentDidMount函数会被调用两次。 当从列表中读取帖子时,这些函数仅被调用一次。但是当我发送一条新消息时,他们会被呼叫两次。 我怎样才能避免这种情况。我试着像这样使用ComponendDupDate函数:

  • 我的react组件的构造函数被调用了两次,但我不知道为什么。我正在使用react redux存储我的应用程序的语言。我有一个基于浏览器语言设置默认语言的函数。LoginPage是第一个获得render的组件,因此我在其构造函数中调用了我的函数。基本上,它所做的就是比较并分派一个动作。当我用redux开发工具检查我的状态时,我看到它被调度了两次。我在构造函数中打印了虚拟数据,它也打印了两次。 登录页

  • 第二个构造函数应该调用第一个构造函数,但却给了我“递归构造函数调用”错误。 我明白这个错误的意思,只是不明白递归在哪里。第一个contructor将作为参数,而应该是该类型的数组。我错过了什么? 多谢了。

  • 为什么下面没有编译: 如有需要,请提供更多详细信息: 我想将tmp传递给父构造函数