0

我正在尝试对 primefaces 数据表执行行编辑。我正在从数据库中获取数据表的所有值。在 onEdit 方法中,我只是试图获取正在编辑的行的主键值。当我尝试编辑或取消编辑时单击勾选或交叉标记时,我得到以下异常。我是 JSF 的新手,请帮助了解如何进行行编辑,以将值更新到数据库。

我得到的例外

2013 年 8 月 22 日上午 3:56:56 com.sun.faces.context.PartialViewContextImpl processPartial INFO:java.lang.NullPointerException java.lang.NullPointerException at org.primefaces.component.datatable.feature.SelectionFeature.decodeMultipleSelection(SelectionFeature.java :51) 在 org.primefaces.component.datatable.DataTableRenderer.decode(DataTableRenderer.java:57) 在 javax.faces.component 的 org.primefaces.component.datatable.feature.SelectionFeature.decode(SelectionFeature.java:40)。 UIComponentBase.decode(UIComponentBase.java:787) at org.primefaces.component.api.UIData.processDecodes(UIData.java:228) at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:507)在 com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183) 在 org.primefaces.component.api.UIData.visitTree(UIData.java:639) 在 javax.faces.component.UIComponent.visitTree(UIComponent.java:1623) 在 javax.faces.component.UIForm.visitTree(UIForm .java:371) 在 javax.faces.component.UIComponent.visitTree(UIComponent.java:1623) 在 javax.faces.component.UIComponent.visitTree(UIComponent.java:1623) 在 com.sun.faces.context.PartialViewContextImpl。 processComponents(PartialViewContextImpl.java:377) 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) 在 com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:78) 在 com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 在 com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) 在 javax.faces.webapp.FacesServlet.service (FacesServlet.java:593) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 在 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) 在 org.apache.catalina.core.StandardEngineValve 的 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)。在 org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) 在 org.apache 调用(StandardEngineValve.java:109) .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(Unknown资源)

我的 JSF 代码片段

<h:form id="lpcForm">

    <div id="content">      
        <p:dataTable var="lpcData" value="#{lpcBean.lpcIdList}" id="lpcList"
            editable="true" scrollable="true" scrollWidth="1110"
            tableStyle="table-layout:auto; width:1130px;" scrollHeight="330"
            styleClass="datatable">
            <p:ajax event="rowEdit" listener="#{lpcBean.onEdit}" />

            <p:ajax event="rowEditCancel" listener="#{lpcBean.onCancel}" />

            <p:column selectionMode="multiple" style="width:5%" />


            <p:column headerText="LPC ID" style="width:20%">
                <h:outputText value="#{lpcData.LPCID}" />
            </p:column>

            <p:column headerText="First Name" style="width:20%">
                <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{lpcData.name_First}" />
                    </f:facet>
                    <f:facet name="input">
                        <p:inputText value="#{lpcData.name_First}" style="width:100%"
                            label="firstName" />
                    </f:facet>
                </p:cellEditor>
            </p:column>

            <p:column headerText="Last Name" style="width:24%">
                <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{lpcData.name_Last}" />
                    </f:facet>
                    <f:facet name="input">
                        <p:inputText value="#{lpcData.name_Last}" style="width:100%"
                            label="lastName" />
                    </f:facet>
                </p:cellEditor>
            </p:column>

            <p:column headerText="Email" style="width:20%">
                <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{lpcData.email_Address}" />
                    </f:facet>
                    <f:facet name="input">
                        <p:inputText value="#{lpcData.email_Address}" style="width:100%"
                            label="emailAddress" />
                    </f:facet>
                </p:cellEditor>
            </p:column>

            <p:column headerText="Region" style="width:24%">
                <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{lpcData.region}" />
                    </f:facet>
                    <f:facet name="input">
                        <h:selectOneMenu value="#{lpcData.region}">
                            <f:selectItems value="#{lpcBean.regions}" />
                        </h:selectOneMenu>
                    </f:facet>
                </p:cellEditor>
            </p:column>


            <p:column headerText="State" style="width:24%">
                <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{lpcData.homeState}" />
                    </f:facet>
                    <f:facet name="input">
                        <h:selectOneMenu value="#{lpcData.homeState}">
                            <f:selectItems value="#{lpcBean.mainStates}" />
                        </h:selectOneMenu>
                    </f:facet>
                </p:cellEditor>
            </p:column>

            <p:column headerText="Profit Center" style="width:20%">
                <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{lpcData.profitCenter}" />
                    </f:facet>
                    <f:facet name="input">
                        <p:inputText value="#{lpcData.profitCenter}" style="width:100%"
                            label="profitCenter" />
                    </f:facet>
                </p:cellEditor>
            </p:column>



            <p:column headerText="Active" style="width:24%">
                <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{lpcData.active_LPC}" />
                    </f:facet>
                    <f:facet name="input">
                        <h:selectOneMenu value="#{lpcData.active_LPC}">
                            <f:selectItems value="#{lpcBean.activeLpcs}" />
                        </h:selectOneMenu>
                    </f:facet>
                </p:cellEditor>
            </p:column>

            <p:column style="width:6%">
                <p:rowEditor />
            </p:column>

        </p:dataTable>

</h:form>

我的托管 bean 片段

@RequestScoped
@ManagedBean
public class LpcBean implements Serializable {

    private List<LPC>lpcItems=new ArrayList<LPC>();
    public static LPC[] selectedRows; 
    public static LPC itemSelect;
    private LpcDataModel lpcIdList;
    Connection connection;
    Statement selectStmt1,selectStmt2,selectStmt3,selectStmt4;
    private List<String> nameList;
    private String selectedLpcName;
    private String selectedItem; 
    private List<String> regions=new ArrayList<String>();
    private List<String>mainStates=new ArrayList<String>();
    private List<String>activeLpcs=new ArrayList<String>();
    public LpcBean() {
        System.out.println("inside the constructor");
        lpcIdList = new LpcDataModel(lpcItems); 
        getLpcFields();
    }


    public List<LPC> getLpcItems() {
        return lpcItems;
    }


    public void setLpcItems(List<LPC> lpcItems) {
        this.lpcItems = lpcItems;
    }


    public LPC[] getSelectedRows() {
        return selectedRows;
    }

    public void setSelectedRows(LPC[] selectedRows) {
        LpcBean.selectedRows = selectedRows;
    }


    public LpcDataModel getLpcIdList() {
        return lpcIdList;
    }


    public void setLpcIdList(LpcDataModel lpcIdList) {
        this.lpcIdList = lpcIdList;
    }


    public String getSelectedItem() {
        return selectedItem;
    }

    public void setSelectedItem(String selectedItem) {
        this.selectedItem = selectedItem;
    }

    public List<String> getNameList() {
        return nameList;
    }


    public void setNameList(List<String> nameList) {
        this.nameList = nameList;
    }



    public String getSelectedLpcName() {
        return selectedLpcName;
    }


    public void setSelectedLpcName(String selectedLpcName) {
        this.selectedLpcName = selectedLpcName;
    }



    public void getLpcFields() {

        try
        {

            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:jtds:sqlserver://cvgapp106I/dev2_LPSR");

            System.out.println("connected to the database");

            selectStmt1 = connection.createStatement();
            ResultSet rs1 = selectStmt1.executeQuery("select * from LPC order by LPCID");


            while(rs1.next()){


                lpcItems.add(new LPC(rs1.getString("LPCID"),rs1.getString("Name_First"),
                rs1.getString("Name_Last"),rs1.getString("Email_Address"),rs1.getString("Region"),
                rs1.getString("HomeState"),rs1.getString("ProfitCenter"),rs1.getString("Active_LPC")));


            }
            selectStmt2=connection.createStatement();
            ResultSet rs2=selectStmt2.executeQuery("Select * from Tbl_state");


            while(rs2.next())
            {
                mainStates.add(rs2.getString("State"));

            }
            selectStmt3=connection.createStatement();
            ResultSet rs3=selectStmt3.executeQuery("Select * from Tbl_Regional_Managers");

            while(rs3.next())
            {
                regions.add(rs3.getString("Location"));
            }
            selectStmt4=connection.createStatement();
            ResultSet rs4=selectStmt4.executeQuery("Select * from YesNo");



            while(rs4.next())
            {
                activeLpcs.add(rs4.getString("YesNo_Key"));
            }

        }
        catch (Exception e)
        {
            //System.out.println("Error Data : " + e.getMessage());
            e.printStackTrace();
        }
        finally
        {
            try {

                connection.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }


    }
    public void setRegions(List<String> regions) {
        this.regions = regions;
    }


    public void setMainStates(List<String> mainStates) {
        this.mainStates = mainStates;
    }


    public void setActiveLpcs(List<String> activeLpcs) {
        this.activeLpcs = activeLpcs;
    }


    public List<String> getRegions() {
        return regions;
    }


    public List<String> getMainStates() {
        return mainStates;
    }


    public List<String> getActiveLpcs() {
        return activeLpcs;
    }

    public void onEdit(RowEditEvent event) {


        String lpcId=(String) ((LPC) event.getObject()).getLPCID();
        System.out.println("EMAIL ID:"+lpcId);

    }

    public void onCancel(RowEditEvent event) {

        System.out.println("inside on cancel");
    }
}
4

1 回答 1

1

糟糕!!!我错过了数据表中的选择属性。现在它工作正常。

 <p:dataTable var="lpcData" value="#{lpcBean.lpcIdList}" selection="#{lpcBean.selectedRows}" 
              id="lpcList" editable="true" scrollable="true" scrollWidth="1110"
              tableStyle="table-layout:auto; width:1130px;" scrollHeight="330" 
              styleClass="datatable">
于 2013-08-23T06:37:52.643 回答