如何编辑数据表的行(JAVA + Primefaces + JSF + MYSQL)

时间:2014-05-21 15:14:28

标签: java mysql jsf-2 primefaces dialog

我有一个数据表页面(Java + JSF + PRIMEFACES + Oracle GlassFish Server 3.1.2.2 + MYSQL)。当我在页面中指定行并单击显示按钮时,您可以在弹出窗口中看到:

enter image description here

当我尝试编辑行时,点击修改

enter image description here

问题从这里开始,当我点击复选标记时,行就会消失:

enter image description here

我在服务器日志中看到:

  

ThreadName =线程2; | javax.faces.model.NoRowAvailableException   javax.faces.model.NoRowAvailableException           在javax.faces.model.ListDataModel.getRowData(ListDataModel.java:150)           在javax.faces.component.UIData.getRowData(UIData.java:371)           at org.primefaces.component.datatable.DataTable.queueEvent(DataTable.java:620)           at org.primefaces.component.behavior.ajax.AjaxBehaviorRenderer.decode(AjaxBehaviorRenderer.java:44)           在javax.faces.component.behavior.ClientBehaviorBase.decode(ClientBehaviorBase.java:132)           在org.primefaces.renderkit.CoreRenderer.decodeBehaviors(CoreRenderer.java:395)           at org.primefaces.component.datatable.DataTableRenderer.decode(DataTableRenderer.java:64)           在javax.faces.component.UIComponentBase.decode(UIComponentBase.java:787)           在org.primefaces.component.api.UIData.processDecodes(UIData.java:232)           at com.sun.faces.context.PartialViewContextImpl $ PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:506)           at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)           在org.primefaces.component.api.UIData.visitTree(UIData.java:675)           在javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)           在javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)           在javax.faces.component.UIForm.visitTree(UIForm.java:344)           在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)           在org.primefaces.context.PrimePartialViewContext.processPartial(PrimePartialViewContext.java:57)           在javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:931)           at 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.StandardWrapper.service(StandardWrapper.java:1550)           在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)           在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)           在org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)           在org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)           在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)           在org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)           在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)           在com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)           at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)           在com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)           在com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)           at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)           在com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)           在com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)           在com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)           at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)           在com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)           在com.sun.grizzly.ContextTask.run(ContextTask.java:71)           at com.sun.grizzly.util.AbstractThreadPool $ Worker.doWork(AbstractThreadPool.java:532)           在com.sun.grizzly.util.AbstractThreadPool $ Worker.run(AbstractThreadPool.java:513)           在java.lang.Thread.run(Thread.java:724)|#]

这是我的页面:

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

    <h:head>    
            <title>Teszt</title>        
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>                        
            <link rel="stylesheet" type="text/css" href="style.css"/>            
    </h:head>

    <ui:debug />
    <h:form id="form">
        <p:fieldset legend="Teszt:">
            <p:dataTable id="dataTableTeszt" value="#{TesztBean.values}" var="c" paginator="true" rows="25" editable="true" filteredValue="#{TesztBeanBean.filteredOsszesito}" scrollable="true" scrollWidth="1500" scrollHeight="550"
                        paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"  
                        paginatorPosition="bottom" rowsPerPageTemplate="5,10,15,20,30,40,50" 
                        selectionMode="multiple" selection="#{TesztBean.selectList}" rowKey="#{c.id}">

                        <p:column selectionMode="multiple" style="width:2%;text-align:center"/>

                        <p:column style="width: 130px; text-align: center" filterBy="#{c.id}" filterMatchMode="contains">
                            <f:facet name="header">ID:</f:facet>                                
                            <h:outputText value="#{c.id}" style="width: 130px; text-align: center"/>                                                
                        </p:column>


                        <p:column style="width: 130px; text-align: center" filterBy="#{c.name}" filterMatchMode="contains">
                            <f:facet name="header">Name:</f:facet>                                                                                                        
                            <h:outputText value="#{c.name}" style="width: 130px; text-align: center"/>                            
                        </p:column>

                        <p:column style="width: 130px; text-align: center" filterBy="#{c.age}" filterMatchMode="contains" >
                            <f:facet name="header">Age:</f:facet>                                                                                                        
                            <h:outputText value="#{c.age}" style="width: 130px; text-align: center"/>                            
                        </p:column>    


                        <p:column style="width: 230px; text-align: center" filterBy="#{c.kapcsolatfelvetel_megtortent}" filterMatchMode="contains" >
                            <f:facet name="header">Kapcsolatfelvétel megtörtént:</f:facet>                                                                                                        
                            <h:outputText value="#{c.kapcsolatfelvetel_megtortent}" style="width: 230px; text-align: center"/>                                
                        </p:column>               

                        <p:column style="width: 230px; text-align: center" filterBy="#{c.levelkuldesenek_datuma}" filterMatchMode="contains" >
                            <f:facet name="header">Levélküldés dátuma:</f:facet>                                                                                                        
                            <h:outputText value="#{c.levelkuldesenek_datuma}" style="width: 230px; text-align: center"/>                            
                        </p:column>

                        <p:column style="width: 230px; text-align: center" filterBy="#{c.kepzes_kezdete}" filterMatchMode="contains" >
                            <f:facet name="header">Képzés kezdete:</f:facet>
                            <h:outputText value="#{c.kepzes_kezdete}" style="width: 230px; text-align: center"/>                            
                        </p:column>

                        <p:column style="width: 230px; text-align: center" filterBy="#{c.kepzes_vege}" filterMatchMode="contains" >
                            <f:facet name="header">Képzés vege:</f:facet>
                            <h:outputText value="#{c.kepzes_vege}" style="width: 230px; text-align: center"/>                            
                        </p:column>

                        <p:column style="width: 1000px; text-align: center" filterBy="#{c.magyarazat}" filterMatchMode="contains">
                            <f:facet name="header">Megjegyzés:</f:facet>
                            <h:outputText value="#{c.magyarazat}" style="width: 1000px; text-align: center"/>                            
                        </p:column>   
                        <f:facet name="footer">                            
                            <p:commandButton id="addCategoryButton" value="Show" oncomplete="categoryDialog.show();" update=":form:dataTableTesztDialog :form:categoryDialog"/>
                        </f:facet>
        </p:dataTable>  
        </p:fieldset>

        <p:dialog id="categoryDialog" header="Category Detail" widgetVar="categoryDialog" closeOnEscape="true" resizable="false" style="width:1000px; height: 500px;" showEffect="explode" hideEffect="bounce">          
            <p:dataTable id="dataTableTesztDialog" value="#{TesztBean.selectList}" var="d" paginator="true" rows="25" editable="true">

                    <p:ajax event="rowEdit" listener="#{TesztBean.update(event)}"/>

                    <p:column style="width: 60px; text-align: center" headerText="Edit">
                        <p:rowEditor />
                    </p:column>

                    <p:column style="width: 130px; text-align: center">
                            <f:facet name="header">ID:</f:facet>                                                
                            <p:cellEditor>
                                <f:facet name="output">
                                    <h:outputText value="#{d.id}" style="width: 130px; text-align: center"/>
                                </f:facet>
                                <f:facet name="input">
                                    <p:inputText value="#{d.id}"/>
                                </f:facet>
                            </p:cellEditor>
                    </p:column>

                    <p:column style="width: 230px; text-align: center">
                        <f:facet name="header">Kapcsolatfelvétel megtörtént:</f:facet>                                                
                        <p:cellEditor>
                            <f:facet name="output">
                                <h:outputText value="#{d.kapcsolatfelvetel_megtortent}" style="width: 230px; text-align: center"/>
                            </f:facet>
                            <f:facet name="input">
                                <p:inputText value="#{d.kapcsolatfelvetel_megtortent}"/>
                            </f:facet>
                        </p:cellEditor>
                    </p:column>               

                        <p:column style="width: 230px; text-align: center">
                            <f:facet name="header">Levélküldés dátuma:</f:facet>                                                
                            <p:cellEditor>
                                <f:facet name="output">
                                    <h:outputText value="#{d.levelkuldesenek_datuma}" style="width: 230px; text-align: center"/>
                                </f:facet>
                                <f:facet name="input">
                                    <p:inputText value="#{d.levelkuldesenek_datuma}"/>
                                </f:facet>
                            </p:cellEditor>
                        </p:column>

                        <p:column style="width: 230px; text-align: center">
                            <f:facet name="header">Képzés kezdete:</f:facet>                                                
                            <p:cellEditor>
                                <f:facet name="output">
                                    <h:outputText value="#{d.kepzes_kezdete}" style="width: 230px; text-align: center"/>
                                </f:facet>
                                <f:facet name="input">
                                    <p:inputText value="#{d.kepzes_kezdete}"/>
                                </f:facet>
                            </p:cellEditor>
                        </p:column>

                        <p:column style="width: 230px; text-align: center">
                            <f:facet name="header">Képzés vege:</f:facet>                                                
                            <p:cellEditor>
                                <f:facet name="output">
                                    <h:outputText value="#{d.kepzes_vege}" style="width: 230px; text-align: center"/>
                                </f:facet>
                                <f:facet name="input">
                                    <p:inputText value="#{d.kepzes_vege}"/>
                                </f:facet>
                            </p:cellEditor>
                        </p:column>

                        <p:column style="width: 350px; text-align: center">
                            <f:facet name="header">Megjegyzés:</f:facet>                                                
                            <p:cellEditor>
                                <f:facet name="output">
                                    <h:outputText value="#{d.magyarazat}" style="width: 350px; text-align: center"/>
                                </f:facet>
                                <f:facet name="input">
                                    <p:inputText value="#{d.magyarazat}"/>
                                </f:facet>
                            </p:cellEditor>
                        </p:column>
                </p:dataTable>        
        </p:dialog>


    </h:form> 

</html>

这是我的bean类:

import java.awt.event.ActionEvent;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import org.primefaces.event.RowEditEvent;
import org.primefaces.model.LazyDataModel;

@ManagedBean(name="tesztBean")
@SessionScoped
@ViewScoped

public class TesztBean{

    String URL = "jdbc:mysql://localhost:3306/osszesito?useUnicode=yes&characterEncoding=UTF-8";
    String USER = "User";        
    String PASSWORD = "Password";
    String DRIVER = "com.mysql.jdbc.Driver";

    private List<TesztSetGet> filteredOsszesito;
    private List<TesztSetGet> values;        
    private List<TesztSetGet> selectList;


    public List<TesztSetGet> getSelectList() {
        return selectList;
    }

    public void setSelectList(List<TesztSetGet> selectList) {
        this.selectList = selectList;
    }

    @PostConstruct
    public void init() {
        try {
            values = selectTesztTable();
        } catch (SQLException e) {

             e.printStackTrace();    
        }
    }


    public Connection getDBConnection() {

        Connection dbConnection = null;

        try {

            Class.forName(DRIVER);
            dbConnection= DriverManager.getConnection(URL, USER, PASSWORD);
            System.out.println("Connection completed.");

        } catch (SQLException e) { 

            System.out.println(e.getMessage()); 

        }catch(ClassNotFoundException cnfe){

           cnfe.printStackTrace();
           System.out.println(cnfe.getMessage());
           System.exit(-1);

       }

        return dbConnection; 
    }


    public List<TesztSetGet> selectTesztTable() throws SQLException{

        ResultSet rs = null;
        PreparedStatement pst = null;
        Connection con = getDBConnection();

        String stm = "select * from teszt order by age";

        List<TesztSetGet> records = new ArrayList<TesztSetGet>();


        try {

            pst = con.prepareStatement(stm);
            pst.execute();
            rs = pst.getResultSet();

         while(rs.next()){

            TesztSetGet objectMeghiv = new TesztSetGet();

            objectMeghiv.setId(rs.getInt(1));            
            objectMeghiv.setName(rs.getString(2)); 
            objectMeghiv.setAge(rs.getInt(3)); 
            objectMeghiv.setKapcsolatfelvetel_megtortent(rs.getString(4));            
            objectMeghiv.setLevelkuldesenek_datuma(rs.getString(5));
            objectMeghiv.setKepzes_kezdete(rs.getString(6));
            objectMeghiv.setKepzes_vege(rs.getString(7));
            objectMeghiv.setMagyarazat(rs.getString(8));                   
            records.add(objectMeghiv);

         }

         return records;


        }catch (SQLException e) {
            e.printStackTrace();         
        }catch (Exception e) {
            e.printStackTrace();         
        }finally{

            rs.close();
            pst.close();            
            con.close();

     }

      return records;

    }


    public List<TesztSetGet> getValues() { 

        return values; 

    }



    public void update(RowEditEvent event) {

        TesztSetGet edittedObject = (TesztSetGet) event.getObject();        

        Connection connection = null;
        PreparedStatement pst = null;
        ResultSet rs = null;

        String sql = "update teszt set kapcsolatfelvetel_megtortent=?, levelkuldes_datuma=?, kepzes_kezdete=?, kepzes_vege=?, megjegyzes=?  where id=?";

        try{

            connection = getDBConnection();                                                                       
            pst = connection.prepareStatement(sql);



            pst.setString(1, edittedObject.getKapcsolatfelvetel_megtortent());
            pst.setString(2, edittedObject.getLevelkuldesenek_datuma());
            pst.setString(3, edittedObject.getKepzes_kezdete());
            pst.setString(4, edittedObject.getKepzes_vege());
            pst.setString(5, edittedObject.getMagyarazat());
            pst.setInt(6, edittedObject.getId());

            pst.executeUpdate();
            pst.close();
            connection.close();

        }catch(SQLException se){
            se.printStackTrace();
            se.getMessage();
         }catch(Exception e){
             e.printStackTrace();
             e.getMessage();
         }    


    }

    public List<TesztSetGet> getFilteredOsszesito() {
        return filteredOsszesito;
    }

    public void setFilteredOsszesito(List<TesztSetGet> filteredOsszesito) {
        this.filteredOsszesito = filteredOsszesito;
    }

}

这是我的Set和Getter类:

public class TesztSetGet {

   private int id;    
   private String name;
   private int age;   
   private String kapcsolatfelvetel_megtortent;
   private String levelkuldesenek_datuma;
   private String kepzes_kezdete;
   private String kepzes_vege;
   private String magyarazat;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getKapcsolatfelvetel_megtortent() {
        return kapcsolatfelvetel_megtortent;
    }

    public void setKapcsolatfelvetel_megtortent(String kapcsolatfelvetel_megtortent) {
        this.kapcsolatfelvetel_megtortent = kapcsolatfelvetel_megtortent;
    }

    public String getLevelkuldesenek_datuma() {
        return levelkuldesenek_datuma;
    }

    public void setLevelkuldesenek_datuma(String levelkuldesenek_datuma) {
        this.levelkuldesenek_datuma = levelkuldesenek_datuma;
    }

    public String getKepzes_kezdete() {
        return kepzes_kezdete;
    }

    public void setKepzes_kezdete(String kepzes_kezdete) {
        this.kepzes_kezdete = kepzes_kezdete;
    }

    public String getKepzes_vege() {
        return kepzes_vege;
    }

    public void setKepzes_vege(String kepzes_vege) {
        this.kepzes_vege = kepzes_vege;
    }

    public String getMagyarazat() {
        return magyarazat;
    }

    public void setMagyarazat(String magyarazat) {
        this.magyarazat = magyarazat;
    }

}

如何正确编辑行?

非常感谢,

1 个答案:

答案 0 :(得分:4)

首先,你必须知道范围的种类:

  • 请求。
  • 视图。
  • 会话。
  • 应用。

范围是数据的生命周期,您必须只使用一个,您的代码中有两个范围:

@SessionScoped
@ViewScoped

我建议您使用ViewScoped,但是考虑一下您希望数据存活多长时间,如果选择ViewScoped,当您不更改视图或页面时,您的数据将会存在,如果您选择SessionScoped,如果你不注销,数据就会存在。

现在,您必须更新表格列表。你有:

<p:dataTable id="dataTableTeszt" value="#{TesztBean.values}" var="c" paginator="true" rows="25" editable="true" filteredValue="#{TesztBeanBean.filteredOsszesito}" scrollable="true" scrollWidth="1500" scrollHeight="550"
                        paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"  
                        paginatorPosition="bottom" rowsPerPageTemplate="5,10,15,20,30,40,50" 
                        selectionMode="multiple" selection="#{TesztBean.selectList}" rowKey="#{c.id}">

您的表格的主要列表是值value="#{TesztBean.values}",当您调用update方法时,您必须更新您的值列表:

public void update(RowEditEvent event) {

        TesztSetGet edittedObject = (TesztSetGet) event.getObject();        

        Connection connection = null;
        PreparedStatement pst = null;
        ResultSet rs = null;

        String sql = "update teszt set kapcsolatfelvetel_megtortent=?, levelkuldes_datuma=?, kepzes_kezdete=?, kepzes_vege=?, megjegyzes=?  where id=?";

        try{

            connection = getDBConnection();                                                                       
            pst = connection.prepareStatement(sql);



            pst.setString(1, edittedObject.getKapcsolatfelvetel_megtortent());
            pst.setString(2, edittedObject.getLevelkuldesenek_datuma());
            pst.setString(3, edittedObject.getKepzes_kezdete());
            pst.setString(4, edittedObject.getKepzes_vege());
            pst.setString(5, edittedObject.getMagyarazat());
            pst.setInt(6, edittedObject.getId());

            pst.executeUpdate();
            pst.close();
            connection.close();

            //UPDATE VALUES LIST WITH NEW DATA

        }catch(SQLException se){
            se.printStackTrace();
            se.getMessage();
         }catch(Exception e){
             e.printStackTrace();
             e.getMessage();
         }    


    }

我不知道你怎么做,但你可以看到primefaces网站,有一些关于这个的样本,想法是更新你的表的主列表,你的情况下的值列表。

我希望这些信息可以帮到你。

祝你好运。

PS。 如果我的英语不好,我愿意收到评论。 = P