0

我有一个带有<h:column>as的 DataTable <h:selectBooleanCheckbox>。刻面标题和 DataTable 行的内容<h:column>都是<h:selectBooleanCheckbox>. 下面是我使用的代码:

<h:form>
    <h:dataTable
        value="#{employeeService.employeeList }"
        var="empl"
        binding="#{employeeService.dataTablebinding }">
        ......
        ......
        ......
        <h:column>
            <f:facet name="header">             
                <h:selectBooleanCheckbox id="chkBoxAll" value="#{empl.checked }" valueChangeListener="#{employeeService.checkAll }" onclick="submit()"></h:selectBooleanCheckbox>
            </f:facet>
            <h:selectBooleanCheckbox id="tableChkBox" value="#{empl.checked }" valueChangeListener="#{employeeService.getCheckChanged }" onclick="submit()"></h:selectBooleanCheckbox>
        </h:column>
    </h:dataTable>
</h:form>

当我单击构面标题复选框时,我希望选中所有行复选框,并且对于未选中的复选框也相同。这是我为ValueChangeListener那个 facet header 复选框提供的代码:

public void checkAll(ValueChangeEvent event){       
    for(Employee empl : employeeList){
        employeeList.remove(empl);
        empl.setChecked(true);
        employeeList.add(empl);
    }               
}

我收到以下异常:

SEVERE: AbortProcessingException thrown when processing event of type 'javax.faces.component.WrapperEvent' during phase 'PROCESS_VALIDATIONS 3' for component with ID or Client ID of 'j_id7'
        27 Apr, 2013 8:20:38 PM javax.faces.component.UIViewRoot broadcastEvents
        SEVERE: javax.faces.event.AbortProcessingException: java.util.ConcurrentModificationException
        javax.faces.event.AbortProcessingException: java.util.ConcurrentModificationException
            at javax.faces.event.MethodExpressionValueChangeListener.processValueChange(MethodExpressionValueChangeListener.java:93)
            at javax.faces.event.ValueChangeEvent.processListener(ValueChangeEvent.java:134)
            at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:771)
            at javax.faces.component.UIData.broadcast(UIData.java:943)
            at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:444)
            at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:701)
            at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
            at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
            at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
            at com.icesoft.faces.webapp.http.core.JsfLifecycleExecutor.apply(JsfLifecycleExecutor.java:18)
            at com.icesoft.faces.webapp.http.core.ReceiveSendUpdates.renderCycle(ReceiveSendUpdates.java:132)
            at com.icesoft.faces.webapp.http.core.ReceiveSendUpdates.service(ReceiveSendUpdates.java:74)
            at com.icesoft.faces.webapp.http.core.RequestVerifier.service(RequestVerifier.java:31)
            at com.icesoft.faces.webapp.http.common.standard.PathDispatcherServer.service(PathDispatcherServer.java:24)
            at com.icesoft.faces.webapp.http.servlet.BasicAdaptingServlet.service(BasicAdaptingServlet.java:16)
            at com.icesoft.faces.webapp.http.servlet.PathDispatcher.service(PathDispatcher.java:23)
            at com.icesoft.faces.webapp.http.servlet.SessionDispatcher.service(SessionDispatcher.java:53)
            at com.icesoft.faces.webapp.http.servlet.SessionVerifier.service(SessionVerifier.java:26)
            at com.icesoft.faces.webapp.http.servlet.PathDispatcher.service(PathDispatcher.java:23)
            at com.icesoft.faces.webapp.http.servlet.MainServlet.service(MainServlet.java:131)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
            at com.icesoft.faces.webapp.xmlhttp.BlockingServlet.service(BlockingServlet.java:56)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
            at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
            at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
            at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291)
            at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
            at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
            at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
            at java.lang.Thread.run(Thread.java:662)
        Caused by: java.util.ConcurrentModificationException
            at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
            at java.util.AbstractList$Itr.next(AbstractList.java:343)
            at com.infy.service.DataTableService.checkAll(DataTableService.java:66)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
            at java.lang.reflect.Method.invoke(Method.java:597)
            at org.apache.el.parser.AstValue.invoke(AstValue.java:191)
            at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
            at javax.faces.event.MethodExpressionValueChangeListener.processValueChange(MethodExpressionValueChangeListener.java:91)
            ... 33 more

请让我知道原因ConcurrentModificationException

4

3 回答 3

3

这是因为在循环列表时您正在修改它:

for(Employee empl : employeeList){
    employeeList.remove(empl);
    empl.setChecked(true);
    employeeList.add(empl);
}               

您需要使用迭代器

for(Employee empl : employeeList){
   // employeeList.remove(empl); //Not required
    empl.setChecked(true);
    //employeeList.add(empl); //Not required
}  

更新正如 Luiggi Mendoza 所提到的,您无需删除元素并将其添加回列表即可对其进行更改。只需参考并修改它。

于 2013-04-27T15:06:34.650 回答
3
for(Employee empl : employeeList){
    empl.setChecked(true);
    }

正如 Luigi 所说,您不需要从列表中删除员工。

于 2013-04-27T15:08:28.447 回答
1

jsf 1.2 不是问题。这里的问题是你不能在迭代集合时修改它。

我认为您会得到相同的行为:

public void checkAll(ValueChangeEvent event){       
for(Employee empl : employeeList){
    empl.setChecked(true);
}               

}

于 2013-04-27T15:15:29.603 回答