Database Update with Prepared Statement in JSF - mysql

I have mainpage.xhtml which displays entire records in my database and allows me to edit them. In case of clicling Edit button it calls mainPageController.edit() function which fills _bilgi variable in MainPageConroller class then it redirects to edit.xhtml page.
In edit.xhtml page it basically displays the values of _bilgi that are filled before in MainPageController class. There's no problem till here(probably there is but I am not aware of it.)
When I want to update one of those variables such as baslik I created a method in MainPageController which calls updateBilgi(_bilgi) in bilgiDAO class.
In UpdateBilgi it is pretty clear what it does yet it is not updating the table. It is interesting for me that when I debug and follow the variables, everything goes well but in the end it just does not update.
Another thing is when I put just a string in BilgilerDAO such is ps.setString(1, "aqa1") it works but ps.setString(2, bilgi.getBaslik()) does not eventho bilgi.getBaslik() returns the changed value.
What could be wrong?
Thank you for the answers in advance.
I hope I am clear enough.
Edit: I found the glitch. in BilgilerDAO the function updateBilgi(Bilgi bilgi)
the variable bilgi doesn't get the id from _bilgi but gets other variables. Why it wouldn't get the id?
mainpage.xhtml
<h:form id="bilgiForm">
<h:dataTable id="bilgiTable" value="#{mainPageController.bilgiListesi}"
var="bilgiler" border="1">
<h:column>
<f:facet name="header">
<h:outputText value="Başlık" />
</f:facet>
<h:outputText value="#{bilgiler.baslik}" />
<f:facet name="footer">
<h:outputText value="Başlık" />
</f:facet>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Bilgi Metni" />
</f:facet>
<h:outputText value="#{bilgiler.bilgi}" />
<f:facet name="footer">
<h:outputText value="Bilgi Metni" />
</f:facet>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Bilgi Metni" />
</f:facet>
<h:outputText value="#{bilgiler.bilgi}" />
<f:facet name="footer">
<h:outputText value="Bilgi Metni" />
</f:facet>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Bilgi Metni" />
</f:facet>
<h:graphicImage
value="https://canakkale.000webhostapp.com/showImage.php/?name=#{bilgiler.img.name}" />
<f:facet name="footer">
<h:outputText value="Bilgi Metni" />
</f:facet>
</h:column>
<h:column>
<fcore:facet name="header">Edit</fcore:facet>
<h:commandButton action="edit" value="Edit"
actionListener="#{mainPageController.edit()}">
<fcore:param name="id" value="#{bilgiler.id}" />
</h:commandButton>
</h:column>
</h:dataTable>
</h:form>
<fcore:view>
<h:form>
<fcore:view>
<h:form>
<h1>Update</h1>
<h:panelGrid border="" cellpadding="20" cellspacing="20"
columns="3">
<h:outputLabel value="Bilgi ID" />
<h:inputText value="#{mainPageController._bilgi.id}"
readonly="true" required="true" requiredMessage="Id is Required"
id="bilgiID">
</h:inputText>
<h:message for="bilgiID" />
<h:outputLabel value="Tag" />
<h:inputText value="#{mainPageController._bilgi.tag}"
readonly="true" required="true"
requiredMessage="Tag Girmeniz Gereklidir." id="bilgiTag">
</h:inputText>
<h:message for="bilgiTag" />
<h:outputLabel value="Başlık" />
<h:inputText value="#{mainPageController._bilgi.baslik}"
required="true" requiredMessage="Başlık Girmeniz Gereklidir."
id="bilgiBaslik">
</h:inputText>
<h:message for="bilgiBaslik" />
<h:outputLabel value="Bilgi Metni" />
<h:inputTextarea value="#{mainPageController._bilgi.bilgi}"
required="true" requiredMessage="Başlık Girmeniz Gereklidir."
id="bilgiBilgi">
</h:inputTextarea>
<h:message for="bilgiBilgi" />
<h:outputLabel value="Image URL" />
<h:inputText value="#{mainPageController._bilgi.img.name}"
required="true" requiredMessage="Başlık Girmeniz Gereklidir."
readonly="true" id="bilgiImgName">
</h:inputText>
<h:message for="bilgiImgName" />
<h:outputLabel value="Başlık" />
<input type="file" value="#{mainPageController._bilgi.img.image}" />
<h:commandButton action="#{mainPageController.updateRecord()}"
value="Update">
</h:commandButton>
</h:panelGrid>
</h:form>
</fcore:view>
</h:form>
</fcore:view>
</h:body>
</html>
MainPageController Class
#ManagedBean
public class MainPageController implements Serializable {
List<Bilgi> bilgiListesi = new ArrayList<Bilgi>();
private Bilgi _bilgi = new Bilgi();
private static final long serialVersionUID = 1L;
BilgilerDAO bilgiDao = new BilgilerDAO();
ImageDAO imgDao = new ImageDAO();
public MainPageController() {// Default Constructor
try {
bilgiListesi = showAllRecords();
} catch (SQLException e) {
e.printStackTrace();
}
}
public List<Bilgi> showAllRecords() throws SQLException {
return bilgiDao.getAllBilgiler();
}
public List<Image> showAllImages() throws Exception {
return imgDao.getAllImages();
}
public void updateRecord() {
try {
bilgiDao.updateBilgi(_bilgi);
//imgDao.updateImage(_bilgi.getImg());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void edit() {
FacesContext fc = FacesContext.getCurrentInstance();
int id;
HttpServletRequest request = (HttpServletRequest) fc.getExternalContext().getRequest();
id = Integer.parseInt(request.getParameter("id"));
try {
bilgiListesi = bilgiDao.getAllBilgiler();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (Bilgi bilgi : bilgiListesi) {
if (bilgi.getId() == id) {
_bilgi.setBaslik(bilgi.getBaslik());
_bilgi.setBilgi(bilgi.getBilgi());
_bilgi.setImg(bilgi.getImg());
_bilgi.setTag(bilgi.getTag());
_bilgi.setId(bilgi.getId());
}
}
}
public void edit(Bilgi bilgi) {
for (Bilgi existing : getBilgiListesi()) {
existing.setEditable(false);
}
bilgi.setEditable(true);
}
}
BilgilerDAO
#Override
public String updateBilgi(Bilgi bilgi) throws Exception {
String sql = "UPDATE bilgiler SET bilgi=?, baslik =? WHERE id=? ";
ps = DatabaseConnection.getConnection().prepareStatement(sql);
ps.setString(1, "aqa1");
ps.setString(2, bilgi.getBaslik());
ps.setInt(3, bilgi.getId());
int ex = ps.executeUpdate();
if (ex > 0) {
System.err.println("coodld");
}
ps.close();
return "mainpage";
}

I kind of solved the issue.
<h:inputText value="#{mainPageController._bilgi.id}" readonly="true" required="true" requiredMessage="Id is Required" id="bilgiID">
in this xhtml line readonly="true" caused it return null. I removed it now it works how I wanted. Though, I still want them to be displayed but not editable.

Related

p:wizard with a progress bar

I would like to create a registration form with two PrimeFaces components: Wizard and Progress Bar. For the backing bean, I am using the following one:
Backing bean: UserWizard.xhtml
import java.io.Serializable;
import java.util.Map;
import java.util.TreeMap;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.primefaces.context.RequestContext;
import org.primefaces.event.FlowEvent;
#ViewScoped
#Named
public class UserWizard implements Serializable {
private User user = new User();
private boolean skip;
private Integer progress = 0;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public void save() {
FacesMessage msg = new FacesMessage("Successful", "Welcome :" + user.getFirstname());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public boolean isSkip() {
return skip;
}
public void setSkip(boolean skip) {
this.skip = skip;
}
public String onFlowProcess(FlowEvent event) {
String oldStep = event.getOldStep();
Integer oldValue = getStepNumber(oldStep);
String newStep = event.getNewStep();
Integer newValue = getStepNumber(newStep);
if(oldValue < newValue)
progress += 25;
else
progress += 25;
return event.getNewStep();
}
public Integer getStepNumber(String Step) {
Integer StepNumber;
switch(Step) {
case "personal":
StepNumber = 1;
break;
case "address":
StepNumber = 2;
break;
case "contact":
StepNumber = 3;
break;
default:
StepNumber = 4;
break;
}
return StepNumber;
}
public Integer getProgress() {
return progress;
}
public void setProgress(Integer progress) {
this.progress = progress;
}
public void onComplete() {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Progress Completed"));
}
public void cancel() {
progress = null;
}
}
Registration Form: registration.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<body>
<f:view contracts="#{view.locale.language}">
<ui:composition template="/template.xhtml">
<ui:define name="centralBody">
<h:form>
<p:growl id="growl" sticky="true" showDetail="true"/>
<p:wizard flowListener="#{userWizard.onFlowProcess}">
<p:tab id="personal" title="Personal">
<p:panel header="Personal Details">
<p:messages />
<h:panelGrid columns="2" columnClasses="label, value">
<h:outputText value="Firstname: *" />
<p:inputText value="#{userWizard.user.firstname}" required="true" label="Firstname"/>
<h:outputText value="Lastname: *" />
<p:inputText value="#{userWizard.user.lastname}" required="true" label="Lastname"/>
<h:outputText value="Age: " />
<p:inputText value="#{userWizard.user.age}" />
<h:outputText value="Skip to last: " />
<h:selectBooleanCheckbox value="#{userWizard.skip}" />
</h:panelGrid>
</p:panel>
</p:tab>
<p:tab id="address" title="Address">
<p:panel header="Address Details">
<p:messages />
<h:panelGrid columns="2" columnClasses="label, value">
<h:outputText value="Street: " />
<p:inputText value="#{userWizard.user.street}" />
<h:outputText value="Postal Code: " />
<p:inputText value="#{userWizard.user.postalCode}" />
<h:outputText value="City: " />
<p:inputText value="#{userWizard.user.city}" />
<h:outputText value="Skip to last: " />
<h:selectBooleanCheckbox value="#{userWizard.skip}" />
</h:panelGrid>
</p:panel>
</p:tab>
<p:tab id="contact" title="Contact">
<p:panel header="Contact Information">
<p:messages />
<h:panelGrid columns="2" columnClasses="label, value">
<h:outputText value="Email: *" />
<p:inputText value="#{userWizard.user.email}" required="true" label="Email"/>
<h:outputText value="Phone: " />
<p:inputText value="#{userWizard.user.phone}"/>
<h:outputText value="Additional Info: " />
<p:inputText value="#{userWizard.user.info}"/>
</h:panelGrid>
</p:panel>
</p:tab>
<p:tab id="confirm" title="Confirmation">
<p:panel header="Confirmation">
<h:panelGrid id="confirmation" columns="3" columnClasses="grid,grid,grid">
<h:panelGrid columns="2" columnClasses="label, value">
<h:outputText value="Firstname: " />
<h:outputText value="#{userWizard.user.firstname}" styleClass="outputLabel"/>
<h:outputText value="Lastname: " />
<h:outputText value="#{userWizard.user.lastname}" styleClass="outputLabel"/>
<h:outputText value="Age: " />
<h:outputText value="#{userWizard.user.age}" styleClass="outputLabel"/>
</h:panelGrid>
<h:panelGrid columns="2" columnClasses="label, value">
<h:outputText value="Street: " />
<h:outputText value="#{userWizard.user.street}" styleClass="outputLabel"/>
<h:outputText value="Postal: " />
<h:outputText value="#{userWizard.user.postalCode}" styleClass="outputLabel"/>
<h:outputText value="City: " />
<h:outputText value="#{userWizard.user.city}" styleClass="outputLabel"/>
</h:panelGrid>
<h:panelGrid columns="2" columnClasses="label, value">
<h:outputText value="Email: " />
<h:outputText value="#{userWizard.user.email}" styleClass="outputLabel"/>
<h:outputText value="Phone " />
<h:outputText value="#{userWizard.user.phone}" styleClass="outputLabel"/>
<h:outputText value="Info: " />
<h:outputText value="#{userWizard.user.info}" styleClass="outputLabel"/>
<h:outputText />
<h:outputText />
</h:panelGrid>
</h:panelGrid>
<p:commandButton value="Submit" actionListener="#{userWizard.save}" update="growl" process="#this"/>
</p:panel>
</p:tab>
</p:wizard>
<p:progressBar id="progressBar" widgetVar="pbAjax" ajax="true" value="#{UserWizard.progress}" labelTemplate="{value}%" styleClass="animated" global="false">
<p:ajax event="complete" listener="#{UserWizard.onComplete}" update="growl" oncomplete="PF('startButton2').enable()"/>
</p:progressBar>
</h:form>
</ui:define>
</ui:composition>
</f:view>
</body>
</html>
My purpose is that, when the registration flow goes through the different registration tabs (Personal, Address, Contact and Confirmation), the progress bar in the lower part of the screen to be updated according to the next or back buttons. In order to achieve this, I want to use the method
public String onFlowProcess(FlowEvent event) {
String oldStep = event.getOldStep();
Integer oldValue = getStepNumber(oldStep);
String newStep = event.getNewStep();
Integer newValue = getStepNumber(newStep);
if(oldValue < newValue)
progress += 25;
else
progress -= 25;
return event.getNewStep();
}
public Integer getStepNumber(String Step) {
Integer StepNumber;
switch(Step) {
case "personal":
StepNumber = 1;
break;
case "address":
StepNumber = 2;
break;
case "contact":
StepNumber = 3;
break;
default:
StepNumber = 4;
break;
}
return StepNumber;
}
But I do not know how to update the progress bar. I have tried:
changing the value of progress bar value (variable progress) in the
function onFlowProcess. It does not work.
<p:wizard ... update="progress_bar">. I have realized that the attribute update is not allowed for the Wizard component.
<p:ajax listener="#{UserWizard.onFlowProcess}" update="#this"> within the <p:progressBar> element.
Since the wizard (at least in/upto PrimeFaces 6.0) does not seem to support the update attribute when using a flowListener and no explicit ajax events are supported, the only option I see is to use the PrimeFaces RequestContext to update the other component. Keep in mind that you need to use the 'full absolute path' to the element, so including all the id's of the namingcontainers it is in. It is best then to explicitly assign id's to all namingcontainers (including a form!)
So using
RequestContext.getCurrentInstance().update("formId:progress_bar");
like e.g.
public String onFlowProcess(FlowEvent event) {
String oldStep = event.getOldStep();
Integer oldValue = getStepNumber(oldStep);
String newStep = event.getNewStep();
Integer newValue = getStepNumber(newStep);
if(oldValue < newValue)
progress += 25;
else
progress -= 25;
RequestContext.getCurrentInstance().update("formId:progress_bar");
return event.getNewStep();
}
in the flowListener should work. Keep in mind that in the updates from beans, the ':' as a prefix for absolute paths is not needed, they are assumed to be always absolute

Primefaces datatable not editing

I'm trying to make editable primefaces datatable. I'm using http://www.primefaces.org/showcase/ui/datatableRowEditing.jsf. All database table data showing up, but then I trying to edit data in a table I'm getting following error:
/index.xhtml #23,90 listener="#{tableBean.onEdit}": Target Unreachable, identifier 'tableBean' resolved to null: javax.el.PropertyNotFoundException: /index.xhtml #23,90 listener="#{tableBean.onEdit}": Target Unreachable, identifier 'tableBean' resolved to null
index.xhtml
<h:head>
<title>index.xhtml</title>
</h:head>
<h:body>
<h:form id="form">
<p:growl id="messages" showDetail="true"/>
<p:dataTable var="u" value="#{logonTest.userList}" id="carList" editable="true">
<f:facet name="header">
In-Cell Editing
</f:facet>
<p:ajax event="rowEdit" listener="#{tableBean.onEdit}" update=":form:messages" />
<p:ajax event="rowEditCancel" listener="#{tableBean.onCancel}" update=":form:messages" />
<p:column headerText="Name" style="width:30%">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{u.name}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{u.name}" style="width:100%"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Surname" style="width:20%">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{u.surname}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{u.surname}" style="width:100%" />
</f:facet>
</p:cellEditor>
</p:column>
TableBean.java
public void onEdit(RowEditEvent event) {
FacesMessage msg = new FacesMessage(" Edited", ((User) event.getObject()).getName());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void onCancel(RowEditEvent event) {
FacesMessage msg = new FacesMessage(" Cancelled", ((User) event.getObject()).getName());
FacesContext.getCurrentInstance().addMessage(null, msg);
}

Can't leave editing mode on prime faces roweditor

I have this code that shows a table with a prime faces datatable and a roweditor column. When I click on pencil icon, editable mode is enabled but after that, when I click on check or cancel icon nothing happens, editable mode remains active. I've search a lot about it but I couldn't find response. Listener on backing bean is not called.
This is my code of the view:
<p:dataTable var="vac" value="#{vacDocBean.obl}" id="documentacion" editable="true">
<f:facet name="header">
Table
</f:facet>
<p:ajax event="rowEdit" update="#this" listener="#{vacDocBean.onEditRow(_record)}" />
<p:column headerText="Edad">#{vacuna.inm.e}</p:column>
<p:column headerText="Inm">#{vac.inm.n}</p:column>
<p:column headerText="Fecha aplicacion">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{vac.fechaAplicacion}">
<f:convertDateTime pattern="dd/MM/yyyy"/>
</h:outputText>
</f:facet>
<f:facet name="input">
<p:calendar value="#{vac.fechaAplicacion}" style="width:100%"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Marca">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{vac.marca}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{vac.marca}" style="width:100%"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Lote">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{vac.lote}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{vac.lote}" style="width:100%"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Editar" styleClass="ui-editable-column-options">
<!-- <p:commandLink ajax="true">-->
<p:rowEditor />
<!-- </p:commandLink > -->
</p:column>
<f:facet name="footer">
Vacunación documentada
</f:facet>
</p:dataTable>
And the backing bean:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.mycompany.calendario;
#Named(value="vacDocBean")
#SessionScoped
public class VacDocBean implements Serializable {
private List<Vac> obl;
public VacDocBean() {
obl = new ArrayList<Vac>();
setVac();
Collections.sort(obl);
}
private void setVac()
{
this.obl = new ArrayList(Helper.getObl());
}
public List<Vac> getObl() {
return obl;
}
public void setObligatorias(List<Vac> obl) {
this.obl = obl;
}
public void onEditRow(RowEditEvent e){
System.out.println("Hello");
}
public void onCancel(RowEditEvent e){
System.out.println("Hello 2");
}
}
Thanks!!
Just surround your h:datatable by a h:form tag, since the editable datatable needs it.
To make your listener method work, try that, whithout changing method's signature:
<p:ajax event="rowEdit" listener="#{vacDocBean.onEditRow}" />

how to get the database values by using hibernate and how to display that values in a primefaces dataTable?

Here i have some doubts.If you know the answer please post here.
How to get the list of values into database table by using hibernate?
How to display that values into primefaces dataTable?
Here i am posting what i am trying .see below
<p:dataTable id="users" value="#{user.listUsers}" var="user"
sortBy="user.id" rows="10" style="width: 30%">
<p:column>
<f:facet name="header">
<h:outputText value="ID" />
</f:facet>
<h:outputText value="#{user.id}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Name" />
</f:facet>
<h:outputText value="#{user.name}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Surname" />
</f:facet>
<h:outputText value="#{user.surName}" />
</p:column>
</p:dataTable>
Here i am finding the answer how to display a table values into primefaces dataTable.
Here i am placing what i am trying code.
<p:dataTable value="#{actions.messages}" var="act">
<p:column style="width:10%;text-align:left" filterBy="#{act.id}" >
<f:facet name="header">
<h:outputText value="ID" id="id" />
</f:facet>
<h:outputText value="#{act.id}" />
</p:column>
<p:column style="width:20%;text-align:left"
sortBy="#{act.action}">
<f:facet name="header">
<h:outputText value="Customer" id="customer" />
</f:facet>
<h:outputText value="#{act.action}" />
</p:column>
<p:column style="width:40%;text-align:left"
sortBy="#{act.scheduled}">
<f:facet name="header">
<h:outputText value="Date" />
</f:facet>
<h:outputText value="#{act.scheduled}" />
</p:column>
<p:column style="width:30%;text-align:left"
sortBy="#{act.status}">
<f:facet name="header">
<h:outputText value="Action" />
</f:facet>
<h:outputText value="#{act.status}" />
</p:column>
</p:dataTable>
Here is the hibernate dao class name called ActionsDao
public class ActionsDao {
public Session session;
public ArrayList<Actions> getActions() {
session = Util.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
try {
#SuppressWarnings("unchecked")
ArrayList<Actions> actionsList = (ArrayList<Actions>) session
.createQuery("from Actions").list();
tx.commit();
boolean found = false;
for (Actions actions : actionsList) {
Actions action = new Actions();
action.setId(actions.getId());
action.setAction(actions.getAction());
action.setActual(actions.getActual());
action.setAssignedBy(actions.getAssignedBy());
action.setAssignedTo(actions.getAssignedTo());
action.setScheduled(actions.getScheduled());
action.setOutcome(actions.getOutcome());
action.setStatus(actions.getStatus());
actionsList.contains(action);
found = true;
}
if (found) {
return actionsList;
} else {
return null;
}
} catch (Exception e) {
System.out.println("Error In getActions() -->" + e.getMessage());
return (null);
} finally {
session.close();
}
}
}
Here is the bean class i am developing name called Actions.
#ManagedBean(name = "actions", eager = true)
#SessionScoped
public class Actions implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String action;
private String assignedTo;
private String assignedBy;
private Date scheduled;
private Date actual;
private String outcome;
private String status;
//write here setter & getter methods of the above properties
public ArrayList<Actions> getMessages() {
ActionsDao dao = new ActionsDao();
return dao.getActions();
}
}
you can use the annotations or hbm file that yours choice to map the bean class.
Here i am using only mapping file why because, first i am trying to create annotations but it raised some problems that jar files are some problem.your using the correct jar files that not a problem you can continue the annotations otherwise hbm file is best.

primefaces input components shows the old data upon validation

I am using primefaces3.3 version. I have a datatable to show the details and the CRUD buttons as part of the footer of the datatable. A Dialog would be shown on click of the CRUD buttons after selecting a particular row in the datatable.
This dialog contains the input fields with requiredMessage and validationMessage properties and a Save button. When i do not enter any input and click on the save button in the dialog the validation message is showing up. However, upon closing the dialog , only the old data is showing up even if a different row is selected in the datatable. I have attached both the BackingBean and xhtml code.
account.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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
<ui:composition template="/template.xhtml">
<ui:define name="body">
<h:form id="form" prependId="false">
<p:contextMenu for="dataTable">
<p:menuitem value="View" update=":form2:accountDisplay"
icon="ui-icon-search" oncomplete="accountDialog.show()"
process="#this">
<f:setPropertyActionListener value="#{account}"
target="#{accountBean.selectedAccount}" />
</p:menuitem>
<p:menuitem value="Edit" update=":form2:accountDisplay"
icon="ui-icon-edit" oncomplete="accountDialog.show()"
process="#this" immedeate="true">
<f:setPropertyActionListener value="#{account}"
target="#{accountBean.currentAccount}" />
</p:menuitem>
<p:menuitem value="Delete" update=":dialogForm:confirmDialog"
icon="ui-icon-close" oncomplete="confirmation.show()" />
</p:contextMenu>
<h:panelGroup id="dataPanel">
<p:dataTable var="account" value="#{accountBean.accounts}"
paginator="true" rows="20" rowKey="#{account.accountCode}"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
selection="#{accountBean.selectedAccount}" selectionMode="single"
id="dataTable">
<p:column id="accountCode" headerText=" Account Code"
sortBy="#{account.accountCode}" filterBy="#{account.accountCode}"
filterMatchMode="startsWith" footerText="startsWith">
<h:outputText value="#{account.accountCode}" />
</p:column>
<p:column id="accountCode" headerText=" Account Code"
sortBy="#{account.accountDescription}" filterBy="#{account.accountDescription}"
filterMatchMode="startsWith" footerText="startsWith">
<h:outputText value="#{account.accountDescription}" />
</p:column>
<f:facet name="footer">
<p:toolbar>
<p:toolbarGroup align="left">
<p:commandButton id="btnAdd" value="Add" title="Add New Account"
icon="ui-icon-add"
oncomplete="accountDialog.show()" />
<p:commandButton id="editButton" value="Edit" icon="ui-icon-edit"
update=":form2:accountDisplay" oncomplete="accountDialog.show()">
</p:commandButton>
<p:commandButton id="showDialogButton" icon="ui-icon-delete"
value="Delete"
title="Delete Account Details" oncomplete="confirmation.show()">
</p:commandButton>
<p:commandButton id="viewButton" value="View"
icon="ui-icon-search" update=":form2:display"
oncomplete="accountDialog.show()" />
<p:commandButton id="btnRefresh" styleClass="refreshButton"
title="Refresh Account Information" value="Refresh"
icon="ui-icon-refresh"
actionListener="#{accountBean.populateAccounts}"
update=":form:dataTable"/>
</p:toolbarGroup>
</p:toolbar>
</f:facet>
</p:dataTable>
</h:panelGroup>
</h:form>
<h:form id="form2">
<!-- <p:growl id="growl" showDetail="true" sticky="true" /> -->
<p:dialog id="basicDialog" header="Account Details"
widgetVar="detailDialog" resizable="false">
<h:panelGrid id="display" columns="2">
<h:outputText value="#{bundle.AccountLabel_accountCode}" />
<h:outputText value="#{accountBean.selectedAccount.accountCode}"
title="#{bundle.AccountTitle_accountCode}" />
<h:outputText value="#{bundle.AccountLabel_accountDescription}" />
<h:outputText value="#{accountBean.selectedAccount.accountDescription}"
title="#{bundle.AccountTitle_alertCode}" />
</h:panelGrid>
</p:dialog>
<p:dialog header="Account Detail" widgetVar="accountDialog"
resizable="false" height="600" width="600" showEffect="clip"
hideEffect="fold" id="dialog" modal="true">
<h:panelGrid id="accountDisplay" columns="2" cellpadding="4">
<f:facet name="header">
<p:messages id="messages" autoUpdate="true" />
</f:facet>
<h:outputLabel value="Account Code:" for="accountCode" />
<p:inputText label="Account Code" id="accountCode"
value="#{accountBean.selectedAccount.accountCode}"
title="Account Code" required="true"
validatorMessage="#{bundle.AccountValidationAccountCode}" requiredMessage="#{bundle.AccountRequiredMessage_accountCode}">
</p:inputText>
<h:outputLabel value="Account Description:" for="accountDescripition" />
<p:inputText label="Account Description" id="accountDescription"
value="#{accountBean.selectedAccount.accountDescription}"
title="Account Description" required="true"
validatorMessage="#{bundle.AccountValidationAccountDescription}" requiredMessage="#{bundle.AccountRequiredMessage_accountDescription}">
</p:inputText>
<f:facet name="footer">
<!-- <p:commandButton value="New" icon="ui-icon-check"/> -->
<h:outputLabel />
<p:commandButton styleClass="saveButton" value="Save"
icon="ui-icon-check"
actionListener="#{accountBean.createOrUpdate}"
update=":form:dataTable,:form2:accountDisplay" oncomplete="if (!args.validationFailed) {accountDialog.hide();}"/>
<p:commandButton styleClass="cancelButton" value="Cancel"
icon="ui-icon-cancel" oncomplete="accountDialog.hide()" process="#this"/>
</f:facet>
</h:panelGrid>
</p:dialog>
</h:form>
<h:form id="dialogForm">
<p:confirmDialog id="confirmDialog" header="Confirm Delete Account"
severity="alert" widgetVar="confirmation">
<f:facet name="message">
<h:outputText
value="Delete #{accountBean.selectedAccount.accountCode}?" />
</f:facet>
<p:commandButton id="confirm" value="Yes"
oncomplete="confirmation.hide()" update=":form:dataTable"
actionListener="#{accountBean.remove}" ajax="false"/>
<p:commandButton id="decline" value="No"
onclick="confirmation.hide()" type="button" ajax="false"/>
</p:confirmDialog>
</h:form>
</ui:define>
</ui:composition>
AccountBean.java
package com.cba.web.beans;
import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.context.RequestContext;
import com.cba.services.AccountService;
import com.cba.utils.ServiceLocator;
import com.cba.web.model.Account;
#ManagedBean(name = "accountBean")
#ViewScoped
public class AccountBean implements Serializable {
private transient AccountService accountService;
private List<Account> accounts;
private Account selectedAccount = new Account();
private Account currentAccount = new Account();
public Account getSelectedAccount() {
return selectedAccount;
}
public void setSelectedAccount(Account selectedAccount) {
this.selectedAccount = selectedAccount;
}
public Account getCurrentAccount() {
return currentAccount;
}
public void setCurrentAccount(Account currentAccount) {
this.currentAccount = currentAccount;
}
public AccountBean() {
populateAccounts();
}
public AccountService getAccountService() {
return accountService;
}
public void setAccountService(AccountService accountService) {
this.accountService = accountService;
}
public List<Account> getAccounts() {
return accounts;
}
public void populateAccounts() {
accountService = (AccountService) ServiceLocator.getInstance().getBean(
"accountService");
accounts = accountService.getAccountDetails();
}
public void createOrUpdate(ActionEvent e) {
try {
System.out.println("entering createOrUpdate in AccountBean");
accountService = (AccountService) ServiceLocator.getInstance()
.getBean("accountService");
accountService.updateAccountDetails(selectedAccount);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void remove(ActionEvent e) {
try {
accountService = (AccountService) ServiceLocator.getInstance()
.getBean("accountService");
accountService.deleteAccountDetails(selectedAccount);
selectedAccount = new Account();
populateAccounts();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
I am using Google Chrome 19 and IE 8. Any input on this would be appreciated. Thanks in advance