0

我正在尝试在来自数据库的 jsf 可数据数据中显示数据,并且在同一行中显示编辑链接。现在,当用户单击编辑按钮时,它应该是来自 outputText 的 inputText。在这里我已经完成了编码,但我无法更改文本框,你能帮帮我吗?提前致谢。

显示数据.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>

客户数据.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;
    }


}

客户.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);
}

}

4

2 回答 2

2

老实说,我想知道您的代码是如何工作的。它有几个主要缺陷。

你应该从

  • 给你的托管 bean 一个范围,这@ViewScoped似乎是最合适的(见这里
  • 从 getter 方法中删除数据库访问。将它放在@PostConstructbean 中的带注释的方法中。在 JSF 生命周期中可以多次调用 Getter 方法。该@PostConstruct方法仅在 bean 构造之后。
  • 完成后关闭 sql 语句和连接(stmt.close()con.close()
  • 以下 bean 字段和方法命名约定:私有字段xxx有一个 gettergetXxx和一个 setter setXxx(大写很重要)
  • 删除数据表绑定。这里没有必要。

我建议通过 BalusC 完成这个简单的 CRUD 示例,并根据您的功能要求对其进行调整。

于 2012-04-10T07:34:17.683 回答
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. 添加您的编辑命令按钮更新标签:

但是为什么不使用primefaces 使用inplace 组件呢?http://www.primefaces.org/showcase-labs/ui/inplace.jsf(需要保存按钮)

于 2012-04-10T07:38:42.630 回答