我试图将Index.xhtml
页面上的输入文本中的输入添加到我的数据库中,一旦添加,我想将其显示在另一页上我已经制作的EG IndexOutput
我遇到的问题是它没有显示我输入的输入
只有已经预先填充在数据库中的输入显示在下面我拥有所有的(Java / XHTML /托管bean类)。
的index.xhtml
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://xmlns.jcp.org/jsf/passthrough"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Product Table</title>
<f:metadata>
<f:event listener="#{productController.loadProducts()}" type="preRenderView" />
</f:metadata>
</h:head>
<h:body>
<h:form>
ProdID: <h:inputText value="#{product.prodid}"
id="id"
p:placeholder="enter product id"
required="true"
requiredMessage="ID is mandatory" />
<br />
<br />
Prod Desc: <h:inputText value="#{product.proddesc}"
p:placeholder="enter product name"
required="true"
requiredMessage="Name is mandatory" />
<br />
<br />
<h:commandButton value="Display" action="IndexOutput" />
</h:form>
</h:body>
</html>
IndexOutput.xhtml
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://xmlns.jcp.org/jsf/passthrough"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Product Table Output</title>
<f:metadata>
<f:event listener="#{productController.loadProducts()}" type="preRenderView" />
</f:metadata>
</h:head>
<h:body>
<h:form>
<h:commandButton value="Add" action="Index" />
<br />
<br />
<h:dataTable
value="#{productController.products}"
var="p"
headerClass="table-header"
rowClasses="table-odd-row,table-even-row">
<h:column>
<f:facet name="header">Product</f:facet>
#{p.prodid}
#{p.proddesc}
</h:column>
</h:dataTable>
</h:form>
</h:body>
</html>
ProductController.java
package lab10;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
//import javax.faces.event.ComponentSystemEvent;
@ManagedBean
public class ProductController {
private ArrayList<Product> products;
private DAO dao;
public ProductController() {
try {
dao = new DAO();
}
catch (Exception e) {
e.printStackTrace();
}
}
public ArrayList<Product> getProducts() {
return products;
}
public void setProducts(ArrayList<Product> products) {
this.products = products;
}
public void loadProducts() throws Exception {
products = dao.getProductDetails();
}
}
Product.java
package lab10;
import javax.faces.bean.ManagedBean;
@ManagedBean
public class Product {
private int prodid;
private String proddesc;
public Product() {
}
public Product(int prodid, String proddesc) {
super();
this.prodid = prodid;
this.proddesc = proddesc;
}
public int getProdid() {
return prodid;
}
public void setProdid(int prodid) {
this.prodid = prodid;
}
public String getProddesc() {
return proddesc;
}
public void setProddesc(String proddesc) {
this.proddesc = proddesc;
}
}
DAO.java
package lab10;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import javax.naming.*;
import javax.sql.DataSource;
public class DAO {
private DataSource mysqlDS;
public DAO() throws Exception {
Context context = new InitialContext();
String jndiName = "java:comp/env/jdbc/employeesdb14";
mysqlDS = (DataSource) context.lookup(jndiName);
}
/*************************************************************************************/
public ArrayList<Product> getProductDetails() throws Exception {
ArrayList<Product> products = new ArrayList<Product>();
Connection conn = mysqlDS.getConnection();
PreparedStatement myStmt = conn.prepareStatement("select * from product;");
ResultSet rs = myStmt.executeQuery();
/*************************************************************************************/
while (rs.next()) {
int id = rs.getInt("PRODID");
String pname = rs.getString("DESCRIP");
products.add(new Product(id, pname));
}
return products;
}
}