0
當我點擊按鈕時,我在另一個名爲「page1.html」的頁面上獲得「記錄保存」在JSF中提到面臨配置文件,但該記錄對於字符串爲空,對於整數爲0。我哪裏錯了?點擊一個按鈕的動作當我嘗試通過java bean添加它時,空數據正在被存儲在數據庫中
我管理的bean代碼(Inventory.java): -
import java.awt.Frame;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import javax.inject.Named;
import javax.enterprise.context.Dependent;
import javax.swing.JOptionPane;
/**
*
* @author Pritam
*/
@Named(value = "inventory")
@Dependent
public class Inventory {
String item;
int price, qty;
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
public Inventory() {
}
public String addItem()
{
try{
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection con=DriverManager.getConnection("jdbc:derby://localhost:1527/practice");
Statement ps=con.createStatement();
ps.executeUpdate("insert into inv_table values('"+getItem()+"',"+getPrice()+","+getQty()+")");
return "Success";
}
catch(Exception e){
return "Failed";
}
}
}
這是我的JSF頁面(MyIndex.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://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<pre>
Item Name: <h:inputText id="t1" value="#{inventory.item}" size="10">
</h:inputText>
Item Price: <h:inputText id="t2" value="#{inventory.price}" size="10">
<f:convertNumber type="number"></f:convertNumber>
</h:inputText>
Item Quantity: <h:inputText id="t3" value="#{inventory.qty}" size="10">
<f:convertNumber type="number"></f:convertNumber>
</h:inputText>
<h:commandButton id="btn" value="add item" action="#{inventory.addItem()}">
</h:commandButton>
</pre>
</h:form>
</h:body>
</html>
JSF Faces配置文件: -
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
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-facesconfig_2_2.xsd">
<navigation-rule>
<from-view-id>MyIndex.xhtml</from-view-id>
<navigation-case>
<from-action>#{inventory.addItem()}</from-action>
<from-outcome>Success</from-outcome>
<to-view-id>page1.html</to-view-id>
</navigation-case>
<navigation-case>
<from-action>#{inventory.addItem()}</from-action>
<from-outcome>Failed</from-outcome>
<to-view-id>page2.html</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
請先嚐試自己,然後再作爲答案。 – BalusC
我做到了,它對我有用@BalusC – khashayar
感謝您的回覆。當我使用netbeans IDE的PageFlow功能修改faces-config.xml文件時,我的問題得到了解決。 –