【问题标题】:change the datatable outputText to inputText when clicked on Edit Button单击编辑按钮时将数据表 outputText 更改为 inputText
【发布时间】:2012-04-10 06:30:19
【问题描述】:

我正在尝试从数据库中显示 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);
}

}

【问题讨论】:

    标签: jsf jsf-2


    【解决方案1】:

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

    你应该从

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

    我建议通过 BalusC 浏览此 simple CRUD example 并根据您的功能要求对其进行调整。

    【讨论】:

      【解决方案2】:
      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(需要保存按钮)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-05
        • 2020-09-16
        • 2018-04-13
        相关资源
        最近更新 更多