2012-04-10 52 views
0

我嘗試從數據庫中顯示jsf數據數據中的數據,並在同一行中顯示數據,並顯示編輯鏈接。現在當用戶點擊編輯按鈕時,它應該是outputText的inputText。在這裏我已經完成了編碼,但我不能改變文本框,你可以幫我嗎?提前致謝。當點擊Edit按鈕時,將數據表格outputText更改爲inputText

ShowData.xhtml

<h:dataTable value="#{customerdata.customerList}" var="c" 
      styleClass="order-table" binding="#{customerdata.dataTable}" 
      headerClass="order-table-header" border="1" width="100%" 
      rowClasses="order-table-odd-row,order-table-even-row" rows="3"> 

     <h:column> 
      <h:selectBooleanCheckbox></h:selectBooleanCheckbox> 
     </h:column> 
     <h:column> 
     <f:facet name="heder">User ID</f:facet> 
      <h:inputText value="#{c.cust_id}" size="10" rendered="#{c.editable}"/> 
      <h:outputLabel value="#{c.cust_id}" rendered="#{not c.editable}" /> 
     </h:column> 

     <h:column> 
     <f:facet name="heder">User Name</f:facet> 
      <h:inputText value="#{c.cust_name}" size="10" rendered="#{c.editable}"/> 
      <h:outputLabel value="#{c.cust_name}" rendered="#{not c.editable}" /> 
     </h:column> 

     <h:column>    
      <h:commandLink value="Update" rendered="#{c.editable}" action="#{customerdata.editAction(c)}" /> 
      <h:commandLink value="Edit" action="#{customerdata.editAction(c)}" rendered="#{not c.editable}"/> 
     </h:column> 

     <h:column>    
      <h:commandLink value="Delete" action="#{customerdata.deleteAction(c)}" /> 
     </h:column> 


     <!-- Footer Setting --> 
     <f:facet name="footer"> 
     <h:panelGroup> 
      <h:commandButton value="prev" action="#{customerdata.pagePrevious}" 
       disabled="#{customerdata.dataTable.first == 0}" /> 
      <h:commandButton value="next" action="#{customerdata.pageNext}" 
       disabled="#{customerdata.dataTable.first + customerdata.dataTable.rows 
        >= customerdata.dataTable.rowCount}" /> 


     </h:panelGroup> 
     </f:facet> 

    </h:dataTable> 

CustomerData.java

package model; 

@ManagedBean(name="customerdata") 
public class CustomerData implements Serializable { 

    private static final long serialVersionUID = 1L; 

    Connection con; 
    Statement smt; 
    HtmlDataTable dataTable; 
    //Customer cust=new Customer(); 

    public List<Customer> getcustomerList() throws SQLException{ 
     //System.out.println("in getcustomerlist"); 
     List<Customer> list= new ArrayList<Customer>(); 
     try{ 

      Class.forName("com.mysql.jdbc.Driver"); 
      con = DriverManager.getConnection("jdbc:mysql://localhost:3306/openid","root","root"); 
      smt = con.createStatement(); 

      String query="select * from jsftable"; 
      ResultSet rs= smt.executeQuery(query); 

      while(rs.next()){ 

       Customer cust = new Customer(); 

       cust.setCust_id(rs.getInt("cust_id")); 
       cust.setCust_name(rs.getString("cust_name")); 
       cust.setHas_attachment(rs.getBoolean("has_attachment")); 
       System.out.println("in cusotomer data"+cust.isEditable()); 
       //store all data into a List 
       list.add(cust); 
      } 

     } 
     catch(Exception e){ 
      e.printStackTrace(); 
     } 
    return list; 
    } 

    public void pageFirst() { 
     dataTable.setFirst(0); 
    } 

    public void pagePrevious() { 
     dataTable.setFirst(dataTable.getFirst() - dataTable.getRows()); 
     System.out.println("Prevoius"+dataTable.getFirst()); 
    } 

    public void pageNext() { 
     dataTable.setFirst(dataTable.getFirst() + dataTable.getRows()); 
     System.out.println("Next"+dataTable.getFirst()); 
    } 

    public void pageLast() { 
     int count = dataTable.getRowCount(); 
     int rows = dataTable.getRows(); 
     dataTable.setFirst(count - ((count % rows != 0) ? count % rows : rows)); 
    } 

    public HtmlDataTable getdataTable() { 
     return dataTable; 
    } 

    public void setdataTable(HtmlDataTable dataTable) { 
     this.dataTable = dataTable; 
    } 

    public void showRow(){ 
     System.out.println(dataTable.getRows()); 
    } 
    public String deleteAction(Customer customer) throws SQLException{ 

     //list.remove(customer); 
     //System.out.println(customer.cust_id); 

     try{ 
      Class.forName("com.mysql.jdbc.Driver"); 
      con = DriverManager.getConnection("jdbc:mysql://localhost:3306/openid","root","root"); 
      smt = con.createStatement(); 

      String query="delete from jsftable where cust_id="+customer.cust_id+""; 
      //ResultSet rs= smt.executeQuery(query); 
      smt.executeUpdate(query); 

     } 
     catch(Exception e){ 

      e.printStackTrace(); 
     } 

     return null; 
    } 

    public String editAction(Customer customer) { 

     //list.remove(customer); 
     System.out.println(customer.cust_id); 

     customer.setEditable(true); 
     return null; 
    } 


} 

Customer.java

public class Customer { 

int cust_id; 
String cust_name; 
boolean has_attachment; 
boolean editable; 
String edit_value; 


public String getEdit_value() { 
    System.out.println("in getter"); 
    return edit_value; 
} 
public void setEdit_value(String editvalue) { 
    this.edit_value = editvalue; 
} 
public boolean isHas_attachment() { 
    System.out.println("in getter of has_attach"); 
    return has_attachment; 
} 
public void setHas_attachment(boolean hasAttachment) { 
    has_attachment = hasAttachment; 
} 
public int getCust_id() { 
    System.out.println("in getter of cust_id"); 
    return cust_id; 
} 
public void setCust_id(int custId) { 
    cust_id = custId; 
} 
public String getCust_name() { 
    return cust_name; 
} 
public void setCust_name(String custName) { 
    cust_name = custName; 
} 
public boolean isEditable() { 
    //System.out.println("in isEditable"+editable); 
    return editable; 
} 
public void setEditable(boolean editable) { 

    this.editable = editable; 
    //System.out.println("in set editable"+editable); 
} 

}

回答

2

說實話,我不知道你的代碼是如何工作的所有。它有幾個主要缺陷。

你應該

  • 給你的託管bean的作用域開始,該@ViewScoped似乎是最合適的(見here
  • 去除getter方法的數據庫訪問。將它放入bean中的@PostConstruct註釋方法中。在JSF生命週期中可以多次調用Getter方法。 @PostConstruct方法只有在bean構建之後。
  • 關閉SQL語句和連接,當你完成(stmt.close()con.close()
  • 以下豆領域和方法命名約定:私人領域xxx有一個getter getXxx和二傳手setXxx(大小寫很重要)
  • 移除數據表綁定。這裏沒有必要。

我建議您通過BalusC的simple CRUD example並根據您的功能要求對其進行調整。

1
  1. 的數據表中添加ID標籤:

<h:dataTable value="#{customerdata.customerList}" var="c" id="customerDT" 
      styleClass="order-table" binding="#{customerdata.dataTable}" 
      headerClass="order-table-header" border="1" width="100%" 
      rowClasses="order-table-odd-row,order-table-even-row" rows="3"> 

  1. 添加在你的編輯命令按鈕更新標籤:

但是爲什麼不使用inplace組件使用primefaces? http://www.primefaces.org/showcase-labs/ui/inplace.jsf(需要保存按鈕)