JSF渲染属性问题

时间:2017-05-08 19:38:57

标签: jsf

所以,我有这段代码,基本上在数据库上执行CRUD并在<h:dataTable>中显示结果。问题是,当我提供固定的ArrayList时,我的代码能够生成编辑操作。但是,如果我从数据库填充ArrayList,则呈现的属性不会从表单中提供editable选项。我能够执行删除功能,因此这不是在动作方法参数中没有正确读取对象的问题。简而言之,当我的数据源是来自数据库的表时,呈现的属性不起作用。

这是页面:

index.xhtml
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <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"
  >
<h:head>
</h:head>
<h:body>

    <h1>JSF 2 dataTable example</h1>
    <h:form>
        <h:dataTable value="#{order.orderList}" var="o"
        styleClass="order-table"
        headerClass="order-table-header"
        rowClasses="order-table-odd-row,order-table-even-row"
       >

         <h:column>

            <f:facet name="header">Order No</f:facet>

            <h:inputText value="#{o.orderNo}" size="10" rendered="#
  {o.editable}" />

        <h:outputText value="#{o.orderNo}" rendered="#{not o.editable}" />

         </h:column>

         <h:column>

        <f:facet name="header">Product Name</f:facet>

        <h:inputText value="#{o.productName}" size="20" rendered="#
 {o.editable}" />

        <h:outputText value="#{o.productName}" rendered="#{not o.editable}" 
 />

         </h:column>

         <h:column>

        <f:facet name="header">Price</f:facet>

        <h:inputText value="#{o.price}" size="10" rendered="#{o.editable}" 
 />

        <h:outputText value="#{o.price}" rendered="#{not o.editable}" />

         </h:column>

         <h:column>

        <f:facet name="header">Quantity</f:facet>

            <h:inputText value="#{o.qty}" size="5" rendered="#{o.editable}" 
 />

            <h:outputText value="#{o.qty}" rendered="#{not o.editable}" />

         </h:column>

         <h:column>

        <f:facet name="header">Action</f:facet>

            <h:commandButton value="Edit" action ="#{order.editAction(o)}"> 
                <f:setPropertyActionListener 
                    target = "#{Orders}" value = "#{o}" />
            </h:commandButton>


         </h:column>
            <h:column>
                <f:facet name ="header">Action</f:facet>
                <h:commandButton value ="Delete" action="#
 {order.delete(o)}"/>
            </h:column>

      </h:dataTable>


  </h:form>
</h:body>
</html>

orderBean.java:

import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Arrays;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean(name="order")
@RequestScoped
public class orderBean implements Serializable{

private static final long serialVersionUID = 1L;

private static ArrayList<Orders> orderList;

public ArrayList<Orders> getOrderList() {
            try{
                    orderBean.orderList = new ArrayList();
                    Class.forName("com.mysql.jdbc.Driver");
                    Connection conn = 
DriverManager.getConnection("jdbc:mysql://localhost:3306/orders","root","");
                    PreparedStatement ps = conn.prepareStatement("Select * 
  from Orders");
                    ResultSet rs = ps.executeQuery();
                    while(rs.next())
                    {
                        Orders orders = new 
 Orders(rs.getString("orderNo"),rs.getString("productName"),
 rs.getBigDecimal("price"),rs.getInt("qty"));

                        orderList.add(orders);
                    }
                }
                catch(Exception e)
                {

                }
                return orderList;
}
    public void delete(Orders order)
    {
          try{
                    orderBean.orderList = new ArrayList();
                    Class.forName("com.mysql.jdbc.Driver");
                    Connection conn = 
DriverManager.getConnection("jdbc:mysql://localhost:3306/orders","root","");
                    PreparedStatement ps = conn.prepareStatement("Delete 
from orders where orderNo=?");
                    ps.setString(1,order.orderNo);
                    int rs = ps.executeUpdate();

                }
                catch(Exception e)
                {

                }
    }


public void editAction(Orders order) {
            order.setEditable(true);
            }


public static class Orders{

    String orderNo;
    String productName;
    BigDecimal price;
    int qty;
    boolean editable;

    public Orders(String orderNo, String productName, BigDecimal price, int 
  qty) {
        this.orderNo = orderNo;
        this.productName = productName;
        this.price = price;
        this.qty = qty;
    }
                 public void setOrderNo(String orderNo)
            {
                this.orderNo = orderNo;                          
            }
            public void setProductName(String productName)
            {
                this.productName = productName;
            }
            public void setPrice(BigDecimal price)
            {
                this.price = price;
            }
            public void setQty(int qty){
                this.qty = qty;
            }
            public String getOrderNo()
            {
                return this.orderNo;
            }
            public String getProductName()
            {
                return this.productName;
            }
            public BigDecimal getPrice()
            {
                return this.price;
            }
            public int getQty()
            {
                return this.qty;
            }
            public boolean isEditable() {
                return this.editable;
            }
            public void setEditable(Boolean editable) {
                this.editable = editable;
            }
    //getter and setter methods
 }
 }

0 个答案:

没有答案