Screen that has parameters
<?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:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>Produtos</title>
</h:head>
<h:body>
<h:form>
<p:panel header="Carrinho">
<p:commandButton value="Voltar" action="index.xhtml" styleClass="ui-priority-primary" ajax="false"/>
<p:dataTable var="item" value="#{principalMB.itens}">
<p:column headerText="Código">
<h:outputText value="#{item.idproduto}" />
</p:column>
<p:column headerText="Nome">
<h:outputText value="#{item.quantidade}" />
</p:column>
<p:commandButton value="Finalizar" id="alterar" action="#" styleClass="ui-priority-primary" ajax="false"/>
</p:dataTable>
<br></br>
<h:outputLabel value="Cliente: "/>
<p:selectOneMenu value="#{principalMB.clienteSelecionado}">
<f:selectItems var="idselecionado" value="#{principalMB.clientesSelect}"/>
</p:selectOneMenu>
<p:commandButton value="Finalizar" id="finalizar" action="#{principalMB.cadastrarPedido(idselecionado)}"/>
</p:panel>
</h:form>
</h:body>
</html>
Managed Bean
@Named(value = "principalMB")
//@RequestScoped
@SessionScoped
public class PrincipalMB implements Serializable {
private Cliente clienteSelecionado;
private List<SelectItem> clientesSelect;
PedidoDAO eDAO = new PedidoDAO();
public String cadastrarPedido(int idcliente){
e.setIdcliente(idcliente);
e.setData(new Date());
eDAO.CadastrarPedido(e);
return "catalogo";
}
}
DAO
public void CadastrarPedido(Pedido p){
Connection con = Conexao2.getConnection();
PreparedStatement stmt = null;
try {
stmt = con.prepareStatement("INSERT INTO pedido (id_cliente,data) VALUES(?,?)");
stmt.setInt(1, p.getIdcliente());
stmt.setDate(2, (Date) p.getData());
stmt.executeUpdate();
} catch (SQLException ex) {
ex.printStackTrace();
}finally{
Conexao2.closeConnection(con,stmt);
}
}