When I click on the button I'm getting "record saved" message on another page named "page1.html" as mentioned in JSF faces configuration file, but the record is null for string and 0 for integers. Where am I getting wrong?
my managed bean code(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";
}
}
}
this is my jsf page(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 configuration file:-
<?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>