未加载到JSF表单中进行编辑的值

时间:2017-03-10 10:45:09

标签: jsf jsf-2

index.xhtml

此页面列出了用户可以编辑或删除产品的产品信息。当我单击编辑链接时,页面将重定向到editProduct.xhtml,但值不会加载到窗体上。加载一个空表单。所选productId的值在netbeans控制台中打印,但不以jsf格式打印。

        <?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://xmlns.jcp.org/jsf/html"
                      xmlns:fcore="http://java.sun.com/jsf/core">
                    <h:head>
                        <title>Product </title>
                    </h:head>
                    <h:body>

                        <br/>

                        <center>
                            <h2> Product Lists</h2>
                            <fcore:view>
                                <h:form>
                                    <h:dataTable var="lists" value="#{productController.getAllProduct()}" border="1" cellpadding="10">
                                        <h:column>
                                            <fcore:facet name="header">Product Id</fcore:facet>
                                            <h:outputText value="#{lists.productId}" />
                                        </h:column>

                                        <h:column>
                                            <fcore:facet name="header">Product Name</fcore:facet>
                                            <h:outputText value="#{lists.productName}" />
                                        </h:column>

                                        <h:column>
                                            <fcore:facet name="header">Product Price</fcore:facet>
                                            <h:outputText value="#{lists.productPrice}" />
                                        </h:column>

                                        <h:column>
                                            <fcore:facet name="header">Product Quantity</fcore:facet>
                                            <h:outputText value="#{lists.productQty}" />
                                        </h:column>

                                        <h:column>
                                            <fcore:facet name="header">Product Description</fcore:facet>
                                            <h:outputText value="#{lists.productDesc}" />
                                        </h:column>

                                        <h:column>
                                            <fcore:facet name="header">Purchase Date</fcore:facet>
                                            <h:outputText value="#{lists.purchaseDate}" />
                                        </h:column>

                                        <h:column>

                                            <fcore:facet name="header">Edit</fcore:facet>
                                            <h:commandLink action="editProduct" actionListener="#{productController.findProductById(lists.productId)}" value="Edit">
                                                <fcore:param name="id" value="#{lists.productId}"/>
                                            </h:commandLink>

                                        </h:column>

                                        <h:column>

                                            <fcore:facet name="header">Delete</fcore:facet>
                                            <h:commandLink action="index" actionListener="#{productController.deleteCategory(lists.productId)}" onclick="return confirm('Are You Sure ?')" value="Delete">
                                                <fcore:param name="id" value="#{lists.productId}"/>
                                            </h:commandLink>

                                        </h:column>

                                    </h:dataTable>

                                    <h:commandLink value="Add New Product" action="addProduct" />
                                </h:form>
                            </fcore:view>
                        </center>
                    </h:body>
                </html>

这里是java bean以及getter和setter。

ProductManagedBean.java

    @ManagedBean
    @RequestScoped
    public class ProductManagedBean {

        private int productId;
        private String productName;
        private double productPrice;
        private int productQty;
        private String productDesc;
        private Date purchaseDate;

        //getters and setter here ...
    }

这是用于获取值的控制器。所选productId的值将在控制台中打印,但不会以jsf格式打印。

@ManagedBean(name = "productController")
public class ProductController  {
    public ProductManagedBean findProductById(int productId) {
        ProductManagedBean productManagedBean = new ProductManagedBean();
        String query = "SELECT * FROM product WHERE proId = ?";

        try {
            DbConnection.DbDriver();
            pstat = DbConnection.con.prepareStatement(query);
            pstat.setInt(1, productId);

            resultSet = pstat.executeQuery();
            System.out.println("\n--- Product Id = " + productId);

            while (resultSet.next()) {
                productManagedBean.setProductId(resultSet.getInt("proId"));
                productManagedBean.setProductName(resultSet.getString("proName"));
                productManagedBean.setProductPrice(resultSet.getDouble("proPrice"));
                productManagedBean.setProductQty(resultSet.getInt("proQty"));
                productManagedBean.setProductDesc(resultSet.getString("proDesc"));
                productManagedBean.setPurchaseDate(resultSet.getDate("proDate"));

                System.out.println(productManagedBean.getProductId() + "\t" + productManagedBean.getProductName()
                        + "\t" + productManagedBean.getProductPrice() + "\t" + productManagedBean.getProductQty()
                        + "\t" + productManagedBean.getProductDesc() + "\t" + productManagedBean.getPurchaseDate());
            }

        } catch (SQLException ex) {
            Logger.getLogger(ProductController.class.getName()).log(Level.SEVERE, null, ex);
        }

  public void update(ProductManagedBean productManagedBean) {

  String query = "UPDATE product SET proName = ?, proPrice = ?, proQty = ?, proDesc = ?, proDate = ? "
            + "WHERE proId = ?";

    try {
        DbConnection.DbDriver();
        pstat = DbConnection.con.prepareStatement(query);

        pstat.setString(1, productManagedBean.getProductName());
        pstat.setDouble(2, productManagedBean.getProductPrice());
        pstat.setInt(3, productManagedBean.getProductQty());
        pstat.setString(4, productManagedBean.getProductDesc());
        pstat.setDate(5, new Date(productManagedBean.getPurchaseDate().getTime()));
        pstat.setInt(6, productManagedBean.getProductId());

        int save = pstat.executeUpdate();
        if (save > 0) {
            System.out.println("\n\n---- New Product Saved ---- " + productManagedBean.getProductName() + " --- \n");
        }
    } catch (SQLException ex) {
        Logger.getLogger(ProductController.class.getName()).log(Level.SEVERE, null, ex);
    }
   }
}

这是editProduct.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://xmlns.jcp.org/jsf/html"
  xmlns:f = "http://java.sun.com/jsf/core"
  xmlns:fcore="http://java.sun.com/jsf/core">
<h:head>
    <title>Update Product</title>
</h:head>
<h:body>

    <center>
        <fcore:view>
            <h:form>
                <h:panelGrid border="1" cellpadding="10" columns="2">

                    <h:outputLabel value="Product Id"/>
                    <h:inputText value="#{productManagedBean.productId}" >                            
                    </h:inputText>

                    <h:outputLabel value="Product Name"/>
                    <h:inputText value="#{productManagedBean.productName}" required="true">                            
                    </h:inputText>

                    <h:outputLabel value="Product Price"/>
                    <h:inputText value="#{productManagedBean.productPrice}" required="true">                            
                    </h:inputText>

                    <h:outputLabel value="Product Qty"/>
                    <h:inputText value="#{productManagedBean.productQty}" required="true">                            
                    </h:inputText>

                    <h:outputLabel value="Product Desc"/>
                    <h:inputText value="#{productManagedBean.productDesc}" required="true">                            
                    </h:inputText>

                    <h:outputLabel value="Product Date"/>
                    <h:inputText value="#{productManagedBean.purchaseDate}" required="true">  
                        <f:convertDateTime pattern="yyyy-MM-dd"/>  
                    </h:inputText>

                    <h:commandLink value="Update" action="index" actionListener="#{productController.update(productManagedBean)}" />

                </h:panelGrid>
            </h:form>
        </fcore:view>
    </center>
</h:body>

这是web.xml

  <?xml version="1.0" encoding="UTF-8"?>
  <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee    http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
 <context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>

1 个答案:

答案 0 :(得分:-2)

ProductManagedBean必须是SessionScop才能加载editProduct.xhtml上的值。