gpt4 book ai didi

jsf-2 - org.primefaces.component.datatable.feature.CellEditFeature.encode() 处的 java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-12-01 12:40:09 25 4
gpt4 key购买 nike

我正在尝试创建一个包含动态生成列的可编辑数据表。

表格按应有的方式呈现,单击时单元格进入编辑模式,但是当按下回车键或焦点离开单元格时,我在服务器上收到空指针异常。 onCellEdit 方法被调用,但单元格值未在汽车对象中设置。

  • 我是否在 EL 表达式中为 p:inputTextp:outputTextvalue 属性做了一些奇怪的事情?我应该为 p:dataTablep:columns 上的 var 属性使用一些其他属性吗?什么样的 EL 表达式在这里有效?
  • 我还漏掉了什么吗?
  • 或者这可能是 PrimeFaces 中的错误?我可以看到错误发生在 CellEditFeature.encode 中,当它期望获得 CellEditor 但结果为空时。

我在 Primefaces 4.0 和 5.0 上尝试过,结果相同。我正在使用 JSF 2.2、Glassfish 4.0 和 Java 1.8.0_20。

如果有人能帮我找出问题的原因,我将不胜感激。在 Stack Overflow 上有一个动态可编辑数据表的工作示例也会很有用。

Bellow 是一个使用 Primefaces 动态生成列展示示例的最小示例,我已尝试向其添加编辑和异常堆栈跟踪。

网页:

<p:dataTable id="cars" var="car" value="#{dtColumnsView.cars}" widgetVar="carsTable"
editable="true" editMode="cell" >

<p:ajax event="cellEdit" listener="#{dtColumnsView.onCellEdit}" update=":form:msgs" />

<p:columns value="#{dtColumnsView.columns}" var="column" columnIndexVar="colIndex"
styleClass="ui-editable-column">

<f:facet name="header">
<h:outputText value="#{column.header}" />
</f:facet>

<p:cellEditor>
<f:facet name="output"><h:outputText value="#{car[column.property]}" /></f:facet>
<f:facet name="input"><p:inputText id="modelInput" value="#{car[column.property]}" /></f:facet>
</p:cellEditor>

</p:columns>
</p:dataTable>

支持 bean:

@ManagedBean(name = "dtColumnsView")
@ViewScoped
public class ColumnsView implements Serializable {

private static final long serialVersionUID = 1L;

private List<ColumnModel> columns;
private List<Car> cars;

@ManagedProperty("#{carService}")
private CarService service;

@PostConstruct
public void init() {
cars = service.createCars(10);
columns = Arrays.asList(
new ColumnModel("Id", "id"),
new ColumnModel("Brand", "brand"),
new ColumnModel("Year", "year"));
}

public List<Car> getCars() {
return cars;
}

public List<String> getBrands() {
return service.getBrands();
}

public List<String> getColors() {
return service.getColors();
}

public void setService(CarService service) {
this.service = service;
}

public List<ColumnModel> getColumns() {
return columns;
}

public void onCellEdit(CellEditEvent event) {
Object oldValue = event.getOldValue();
Object newValue = event.getNewValue();
System.out.println("Cell Changed, Old: " + oldValue + ", New:" + newValue);
if (newValue != null && !newValue.equals(oldValue)) {
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Cell Changed", "Old: " + oldValue + ", New:" + newValue);
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}

public void dumpCars() {
for (Car c : cars) System.out.println(c.brand);
}

static public class ColumnModel implements Serializable {

private static final long serialVersionUID = 1L;

private String header;
private String property;

public ColumnModel(String header, String property) {
this.header = header;
this.property = property;
}

public String getHeader() {
return header;
}

public String getProperty() {
return property;
}
}
}

异常堆栈跟踪:

java.lang.NullPointerException
at org.primefaces.component.datatable.feature.CellEditFeature.encode(CellEditFeature.java:54)
at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:76)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:924)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:559)
at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
at org.primefaces.component.api.UIData.visitTree(UIData.java:675)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
at javax.faces.component.UIForm.visitTree(UIForm.java:371)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:399)
at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:319)
at org.primefaces.context.PrimePartialViewContext.processPartial(PrimePartialViewContext.java:57)
at javax.faces.component.UIViewRoot.encodeChildren(UIViewRoot.java:1004)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:417)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:745)

最佳答案

org.primefaces.component.columns.Columns课后<p:columns>返回一个硬编码的 nullgetCellEditor()方法。 See source code :

258    public CellEditor getCellEditor() {
259 return null;
260 }

这至少进一步解释了链中的 NPE,但由于我不是 PrimeFaces 开发人员,我无法解释为什么它会故意返回硬编码的 null。 .最好的办法是向 PrimeFaces 报告新问题。 It doesn't seem to be already reported before .


与此同时,理论上您应该能够通过替换 <p:columns> 来解决这个棘手的问题。与 <c:forEach><p:column> .

<c:forEach items="#{dtColumnsView.columns}" var="column" varStatus="loop">
<p:column>

<f:facet name="header">
<h:outputText value="#{column.header}" />
</f:facet>

<p:cellEditor>
<f:facet name="output"><h:outputText value="#{car[column.property]}" /></f:facet>
<f:facet name="input"><p:inputText id="modelInput_#{loop.index}" value="#{car[column.property]}" /></f:facet>
</p:cellEditor>

</p:column>
</c:forEach>

注意:确保您手动将迭代索引分配给循环内的任何组件 ID。

另见:

关于jsf-2 - org.primefaces.component.datatable.feature.CellEditFeature.encode() 处的 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25650840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com