单击“编辑”按钮时,将数据表outputText更改为inputText

时间:2012-04-10 06:30:20

标签: jsf jsf-2

我试图在数据库的jsf datable数据中显示数据,并且在同一行显示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 个答案:

答案 0 :(得分:2)

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

你应该从

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

我建议您通过BalusC审核此simple CRUD example并根据您的功能需求进行调整。

答案 1 :(得分:1)

  1. 为datatable添加id标记:
  2. <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. 添加您的编辑CommandButton更新代码:

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