Changing ActiveIndex in TabMenu - primefaces

I'm using Primefaces 3.5 and have run into an issue with changing the ActiveIndex. ActiveIndex doesn't change when going between tabs. This is my TabMenu code:
<h:form id="formMenu">
<p:tabMenu id="tabMenu" activeIndex="#{toolbarBean.currentTab}">
<p:menuitem value="Main" action="#{toolbarBean.changeActiveIndex(0)}" />
<p:menuitem value="Page2" action="#{toolbarBean.changeActiveIndex(1)}" />
<p:menuitem value="Page3" actionListener="#{toolbarBean.changeActiveIndex(2)}" />
<p:menuitem value="Page4" action="#{toolbarBean.changeActiveIndex(3)}" />
</p:tabMenu>
</form>
toolbarBean.java
#Named
#SessionScoped
public class toolbarBean implements Serializable {
private int currentTab;
public int getCurrentTab() {
return currentTab;
}
public void setCurrentTab(int currentTab) {
this.currentTab = currentTab;
}
public String changeActiveIndex(int currentTab) {
this.currentTab = currentTab;
switch (currentTab) {
case 0:
return "/main";
case 1:
return "/page2";
case 2:
return "/page3";
case 3:
return "/page4";
default:
return "/page5";
}
}
}
The action part works properly where the variable currentTab gets assigned the right value. However, once it is running the page redirect part, toolbarBean is being reset and activeIndex starts off at 0 again. Does anyone have any ideas on what I'm doing wrong?

Have a look at my answer in this question:
How to change activeindex in TabMenu
I think it's what you are looking for.
Edit:
Here is the complete answer:
I have found a solution in the PrimeFaces showcase. You can add a request parameter to the menuitem and append this parameter to your url. So no backing bean is needed to keep the active index:
<p:tabMenu activeIndex="#{param.i}">
<p:menuitem value="Home" icon="ui-icon-home" url="page1.xhtml?i=0">
<f:param name="i" value="0" />
</p:menuitem>
<p:menuitem value="Search" icon="ui-icon-search" url="page2.xhtml?i=1">
<f:param name="i" value="1" />
</p:menuitem>
...
</p:tabMenu>

I've unsuccessfully tried to reproduce the error.
I believe that the code sample I used has the features you need. Check it out:
*The View
<p:layout fullPage="true">
<p:layoutUnit position="north" size="100" header="Top" resizable="true" closable="true" collapsible="true">
<h:form id="formMenu">
<p:tabMenu id="tabMenu" activeIndex="#{toolbarBean.currentTab}">
<p:menuitem value="Main" action="#{toolbarBean.changeActiveIndex(0)}" />
<p:menuitem value="Page2" action="#{toolbarBean.changeActiveIndex(1)}" />
<p:menuitem value="Page3" action="#{toolbarBean.changeActiveIndex(2)}" />
<p:menuitem value="Page4" action="#{toolbarBean.changeActiveIndex(3)}" />
</p:tabMenu>
</h:form>
</p:layoutUnit>
<p:layoutUnit position="south" size="100" header="Bottom" resizable="true" closable="true" collapsible="true">
<h:outputText value="South unit content." />
</p:layoutUnit>
<p:layoutUnit position="west" size="200" header="Left" resizable="true" closable="true" collapsible="true">
<h:outputText value="West unit content." />
</p:layoutUnit>
<p:layoutUnit position="east" size="200" header="Right" resizable="true" closable="true" collapsible="true" effect="drop">
<h:outputText value="Right unit content." />
</p:layoutUnit>
<p:layoutUnit position="center">
MAIN
</p:layoutUnit>
</p:layout>
The Managed Bean
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
#ManagedBean
#SessionScoped
public class ToolbarBean implements Serializable {
private int currentTab;
public int getCurrentTab() {
return currentTab;
}
public void setCurrentTab(int currentTab) {
this.currentTab = currentTab;
}
public String changeActiveIndex(int currentTab) {
this.currentTab = currentTab;
switch (currentTab) {
case 0:
return "main";
case 1:
return "page2";
case 2:
return "page3";
case 3:
return "page4";
default:
return "page5";
}
}
}
Note that the ToolbarBean is almost the same. As you are using #Named, see if the ManagedBean is indeed in session scope.
Another thing worth mentioning is that main.xhml, page1.xhtml, page2.xhtml and etc, has the same content except for the center layout unit content.

Related

Simple p:inputText in p:dialog not setting value in #Named Bean

I am using primefaces 5.1 and still facing problem with p:dialog and p:inputText within that dialog.
Here is the .xhtml content
<ui:define name="content">
<f:metadata> <f:viewAction action="#{vLoginController.initSetup}" /> </f:metadata>
<h:form id="vhome" prependId="false">
<table>
<tr>
<td>
<p:commandButton value="Place Order" action="#{vLoginController.placeOrderHomePage}"
update="logindialog, signupdialog"/>
</td>
</tr>
</table>
<p:dialog id="logindialog" header="Login / Sign up" visible="#{vLoginController.loginDialog}"
draggable="false" resizable="false" modal="true">
<p:messages autoUpdate="true"/>
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel for="inuserid" value="EmailID:" />
<p:inputText id="inuserid" size="40" value="#{vLoginController.user.userId}" />
<h:outputLabel for="inpassword" value="Password:" />
<p:password id="inpassword" size="20" value="#{vLoginController.user.password}" />
<f:facet name="footer">
<p:commandButton value="Login" process="#form" action="#{vLoginController.validateLogin}" update="logindialog"/>
<p:commandButton value="Sign up" action="#{vLoginController.showSignup}" update="logindialog, signupdialog"/>
</f:facet>
</h:panelGrid>
</p:dialog>
<p:dialog id="signupdialog" header="Sign up / Login" visible="#{vLoginController.signupDialog}"
draggable="false" resizable="false" modal="true">
<p:outputLabel>Test</p:outputLabel>
</p:dialog>
</h:form>
</ui:define>
Here is the supporting Bean
#Named
#SessionScoped
public class VLoginController extends BaseController implements Serializable {
private VUser user;
public VLoginController() {
}
public void placeOrderHomePage() {
loginDialog = true;
signupDialog = false;
}
public String validateLogin() {
// Validate the screen fields
FacesMessage msg = new FacesMessage();
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
FacesContext context = FacesContext.getCurrentInstance();
if (user.getUserId() == null || user.getPassword() == null) {
msg.setSummary("Enter User ID and Password");
context.addMessage("screenvalidation", msg);
return null;
}
}
}
public class VUser implements Serializable {
public VUser() {
}
private int userSeq;
private String userId;
private String userType;
private String password;
private String salt;
private int active;
}
The userID and password in the user object are always null. Have gone through various forums content and tried like adding outputpanel around them etc., but none works. Have kept the code very similar to what the primefaces showcase says, still doesn't work.
Any solution please.. Please note that there is no problem in showing or hiding the dialog boxes, those logic works perfect. I just need to get the values entered by the user in the dialog fields to the bean..

Primefaces p:confirmDialog inside tabView

I now have problem with using confirmDialog inside tabView.
Here is my confirmDialog
<p:confirmDialog global="true" showEffect="fade" hideEffect="explode">
<h:form>
<p:commandButton value="Yes" type="button"
styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
<p:commandButton value="No" type="button"
styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
</h:form>
</p:confirmDialog>
In the 1st tabView, I have a button with confirmation
<p:commandButton value="Import" icon="ui-icon-arrowrefresh-1-w"
update=":form:growl">
<p:confirm header="Confirmation" message="Are you sure?" />
</p:commandButton>
In the 2nd tabView, I have exactly that button.
And now my problem is: In the 1st tab, my confirmDialog have full text as I want: header and message, however in the 2nd tab, the header and message all become "null". Only button yes and no of confirmDialog still work. I don't know what is happening, please help me, how to make confirmDialog display full content in all tabView
p:confirm does not implement state saving, thus it loses its attributes' values after the first JSF lifecycle. It also evaluates EL only once at view build time.
How to fix state saving.
You can extends PrimeFaces' ConfirmBehavior, implement saveState, restoreState, and override the original behavior in faces-config.xml by behavior-id org.primefaces.behavior.ConfirmBehavior. Then you'll be able to render and re-render p:confirm on subsequent requests.
How to fix state saving and reevaluate EL bound attribute values.
You should create your own my:confirm, because you need a custom taghandler, and you can't substitute a taghandler of another taglibrary's tag in a non-ugly way.
Create ConfirmBehavior.
package my;
import javax.faces.FacesException;
import javax.faces.component.UIComponent;
import javax.faces.component.behavior.ClientBehaviorContext;
import javax.faces.context.FacesContext;
import org.primefaces.behavior.base.AbstractBehavior;
import org.primefaces.component.api.Confirmable;
import org.primefaces.json.JSONObject;
public class ConfirmBehavior extends AbstractBehavior {
public final static String BEHAVIOR_ID = "my.ConfirmBehavior";
#Override
public String getScript(ClientBehaviorContext behaviorContext) {
FacesContext context = behaviorContext.getFacesContext();
UIComponent component = behaviorContext.getComponent();
String source = component.getClientId(context);
String headerText = JSONObject.quote(this.getHeader());
String messageText = JSONObject.quote(this.getMessage());
if (component instanceof Confirmable) {
String script = "PrimeFaces.confirm({source:\"" + source + "\",header:" + headerText + ",message:"
+ messageText + ",icon:\"" + getIcon() + "\"});return false;";
((Confirmable) component).setConfirmationScript(script);
return null;
} else {
throw new FacesException("Component " + source + " is not a Confirmable. ConfirmBehavior can only be "
+ "attached to components that implement org.primefaces.component.api.Confirmable interface");
}
}
public String getHeader() {
return eval(PropertyKeys.header, null);
}
public void setHeader(String header) {
setLiteral(PropertyKeys.header, header);
}
public String getMessage() {
return eval(PropertyKeys.message, null);
}
public void setMessage(String message) {
setLiteral(PropertyKeys.message, message);
}
public String getIcon() {
return eval(PropertyKeys.icon, null);
}
public void setIcon(String icon) {
setLiteral(PropertyKeys.icon, icon);
}
public enum PropertyKeys {
header(String.class), message(String.class), icon(String.class);
final Class<?> expectedType;
PropertyKeys(Class<?> expectedType) {
this.expectedType = expectedType;
}
}
#Override
protected Enum<?>[] getAllProperties() {
return PropertyKeys.values();
}
}
Create ConfirmBehaviorHandler.
package my;
import javax.faces.application.Application;
import javax.faces.view.facelets.BehaviorConfig;
import javax.faces.view.facelets.FaceletContext;
import javax.faces.view.facelets.TagAttribute;
import org.primefaces.behavior.base.AbstractBehaviorHandler;
public class ConfirmBehaviorHandler extends AbstractBehaviorHandler<ConfirmBehavior> {
private final TagAttribute header;
private final TagAttribute message;
private final TagAttribute icon;
public ConfirmBehaviorHandler(BehaviorConfig config) {
super(config);
this.header = this.getAttribute(ConfirmBehavior.PropertyKeys.header.name());
this.message = this.getAttribute(ConfirmBehavior.PropertyKeys.message.name());
this.icon = this.getAttribute(ConfirmBehavior.PropertyKeys.icon.name());
}
#Override
protected ConfirmBehavior createBehavior(FaceletContext ctx, String eventName) {
Application application = ctx.getFacesContext().getApplication();
ConfirmBehavior behavior = (ConfirmBehavior) application.createBehavior(ConfirmBehavior.BEHAVIOR_ID);
setBehaviorAttribute(ctx, behavior, this.header, ConfirmBehavior.PropertyKeys.header.expectedType);
setBehaviorAttribute(ctx, behavior, this.message, ConfirmBehavior.PropertyKeys.message.expectedType);
setBehaviorAttribute(ctx, behavior, this.icon, ConfirmBehavior.PropertyKeys.icon.expectedType);
return behavior;
}
}
Register ConfirmBehavior in faces-config.xml.
<?xml version="1.0" encoding="utf-8"?>
<faces-config 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"
version="2.2">
<behavior>
<behavior-id>my.ConfirmBehavior</behavior-id>
<behavior-class>my.ConfirmBehavior</behavior-class>
</behavior>
</faces-config>
Register ConfirmBehaviorHandler in your taglibrary my.taglib.xml.
<?xml version="1.0" encoding="UTF-8"?>
<facelet-taglib 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-facelettaglibrary_2_2.xsd"
version="2.2">
<namespace>http://mycompany.ru/my</namespace>
<tag>
<tag-name>confirm</tag-name>
<behavior>
<behavior-id>my.ConfirmBehavior</behavior-id>
<handler-class>my.ConfirmBehaviorHandler</handler-class>
</behavior>
</tag>
</facelet-taglib>
Now you can use my:confirm, just as you would use p:confirm, but with state saving and dynamic EL evaluation.
It seems to work ok for me. See example below
<?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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>JSF User Dialog</title>
</h:head>
<h:body>
<h3>This is a JSF view.</h3>
<h:form id="form">
<p:confirmDialog global="true" showEffect="fade" hideEffect="explode">
<h:form>
<p:commandButton value="Yes" type="button"
styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
<p:commandButton value="No" type="button"
styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
</h:form>
</p:confirmDialog>
<p:tabView id="tabView">
<p:tab id="tab1" title="Tab one ">
<p:commandButton value="Import" icon="ui-icon-arrowrefresh-1-w"
update=":form">
<p:confirm header="Confirmation" message="Are you sure?" />
</p:commandButton>
</p:tab>
<p:tab id="tab2" title="Tab two">
<p:commandButton value="Import" icon="ui-icon-arrowrefresh-1-w"
update=":form">
<p:confirm header="Confirmation" message="Are you sure?" />
</p:commandButton>
</p:tab>
</p:tabView>
</h:form>
</h:body>
</html>
Output:
It would be good if you post your full xhtml code so we can see what could be the problem there.
I had the same problem with the component. The proposed solution of removing the dynamic true works, but when we have to work in a dialog does not meet because the data are no longer updated automatically resulting in that the fields are blank.
If this happens you must perform the following action.
Ex:.
<p:commandButton style="font-size: 12px;width:30px;height:20px;" icon="ui-icon-trash">
<p:confirm header="Confirmação" message="Você realmente quer excluir o item de despacho?" icon="ui-icon-alert" />
</p:commandButton>
<p:growl id="growl" showDetail="true" />
<p:confirmDialog global="true" showEffect="fade" hideEffect="explode" style="font-size: 12px;" closeOnEscape="true" widgetVar="confir">
<p:commandButton id="confirm" value="Sim" ajax="true" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" style="font-size: 12px;">
<p:ajax event="click" listener="#{tableExpedicao.excluiItem}" update="confirm" oncomplete="confir.hide()"> </p:ajax>
</p:commandButton>
<p:commandButton value="Não" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close" style="font-size: 12px;" oncomplete="confir.hide()"/>
</p:confirmDialog>

Primefaces datatable row selection does not work in viewscope

In my application I am using ViewScoped Bean and it does not show selected row when a row is selected in primefaces datatable. But if I changed the Bean to a SessionScoped Bean then it shows the selected row perfectly.
my code is like below.
<h:form id="form">
<p:growl id="msgs" showDetail="true" />
<p:dataTable var="pMData" rowIndexVar="rowIndex" value="# {managedBean.dataModel}" selectionMode="single" paginator="true" rows="100"
widgetVar="pMTable" emptyMessage="No Records Found." filteredValue="#{managedBean.filteredRecords}" selection="#{managedBean.selectedRecord}" rowKey="#{pMData.cellid}">
<p:ajax event="rowSelect" listener="#{managedBean.onRowSelect}"
update=":form:display :form:msgs" oncomplete="moreviewDialog.show()" />
<p:ajax event="rowUnselect" listener="#{managedBean.onRowUnselect}" update=":form:msgs"/>
<p:column headerText="Cell Name" filterBy="#{pMData.cellid}" filterStyle="display:none" >
<h:outputText value="#{pMData.modifiedCellID}" />
</p:column>
</p:dataTable>
<p:dialog header="History Data" widgetVar="moreviewDialog" resizable="false" id="moreviewDlg"
showEffect="fade" hideEffect="explode" modal="true">
<h:panelGrid id="display" columns="2" cellpadding="4" style="margin:0 auto;">
<p:lineChart id="category" value="# {managedBean.createCategoryModel(managedBean.selectedRecord.cellid)}" legendPosition="e"
title="NodeB Throughput(kbit/s)" minY="0" maxY="5000" style="height:300px;margin-top:20px"/>
</h:panelGrid>
</p:dialog>
</h:form>
and my managedBean CDI is like this.
#Named(value = "managedBean")
#ViewScoped
public class ManagedBean implements Serializable {
public void setSelectedRecord(PMData selectedRecord1){
this.selectedRecord=selectedRecord1;
}
public PMData getSelectedRecord(){
return selectedRecord;
}
public void onRowSelect(SelectEvent event) {
FacesMessage msg = new FacesMessage("NodeB Selected", String.valueOf(((PMData) event.getObject()).getCellid()));
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void onRowUnselect(UnselectEvent event) {
FacesMessage msg = new FacesMessage("Row Unselected",((PMData) event.getObject()).getCellid());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public PMDataModel getDataModel() {
return dataModel;
}
public CartesianChartModel createCategoryModel(String key) { .....}
public List<PMData> getFilteredRecords() {
return filteredRecords;
}
public void setFilteredRecords(List<PMData> filteredRecords) {
// System.out.println("came ");
this.filteredRecords = filteredRecords;
}
}
and PMData and PMDataModel classes are working normally. Can someone help me to select the row while still using viewscoped bean.
I think I found the answer. the error is with the dialogbox. I put dynamic=true in dialogbox. Now working perfectly.

Primefaces Delete & Confirm Dialog inside table column - Update or Freeze Issue

I have a smiliar problem like here: Primefaces: commandButton in confirmDialog cannot update datatable in the same form
I have a table with games. In the last column there are 2 buttons: delete and details.
The delete button should delete the game
The button does delete the game but the update doesn't work.
update=":form1:overviewTableGame" --> no reaction (no refresh)
update="#form" --> update performes (table refreshes), but the entire scren is locked. i think due to the fact, that the form which contains the dialog is updated...
The table code:
<h:form id="form1">
<p:messages id="messages" showDetail="true" autoUpdate="true"
closable="true" />
<p:dataTable id="overviewTableGame" var="game" value="#{gameMB.list}">
<p:column headerText="#{msg.ID}" sortBy="#{game.id}">
<h:outputText value="#{game.id}" />
</p:column>
<p:column headerText="#{msg.NAME}" sortBy="#{game.name}">
<h:outputText value="#{game.name}" />
</p:column>
<p:column headerText="#{msg.DESCRIPTION}"
sortBy="#{game.description}">
<h:outputText value="#{game.description}" />
</p:column>
<p:column headerText="#{msg.ADMIN}" sortBy="#{game.admin.firstname}">
<h:outputText value="#{game.admin.firstname}" />
</p:column>
<p:column headerText="#{msg.ACTION}">
<p:commandButton id="commandButtonDELETE" value="löschen"
onclick="confirmation.show()" type="button"
update=":form1:display">
<f:setPropertyActionListener value="#{game}"
target="#{gameMB.selectedGame}" />
</p:commandButton>
<p:commandButton id="commandButtonDETAIL" value="detail"
action="#{gameMB.details()}">
<f:param name="id" value="#{game.id}" />
</p:commandButton>
</p:column>
</p:dataTable>
<p:confirmDialog id="confirmDialog"
message="Are you sure about destroying the world?"
header="Initiating destroy process" severity="alert"
widgetVar="confirmation">
<h:panelGrid id="display" columns="2" cellpadding="4"
style="margin:0 auto;">
<h:outputText value="Name:" />
<h:outputText value="#{gameMB.selectedGame.name}"
style="font-weight:bold" />
<p:commandButton id="confirm" value="Yes Sure"
oncomplete="confirmation.hide()"
actionListener="#{gameMB.delete(gameMB.selectedGame)}"
update=":form1:overviewTableGame">
</p:commandButton>
</h:panelGrid>
<p:commandButton id="decline" value="Not Yet"
onclick="confirmation.hide()" type="button" />
</p:confirmDialog>
</h:form>
The Delete Method:
public void delete(Game game) {
//FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"DELETE", "Game Deleted!"));
System.out.println("==================");
System.out.println(game);
getGameService().removeGame(game);
//this.gameList = gameService.listAllGames();
}
The selectedGame
private Game selectedGame;
public Game getSelectedGame() {
return selectedGame;
}
public void setSelectedGame(Game selectedGame) {
this.selectedGame = selectedGame;
}
Any ideas?
Thanks
Separate your dialog from p:dataTable. Following code is working:
The xhtml:
<ui:composition 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" template="/WEB-INF/templates/globalTemplate.xhtml">
<ui:define name="title">15344819</ui:define>
<ui:define name="content">
<p:growl id="growl" showDetail="true" />
<h:form id="form">
<p:dataTable id="students" value="#{so15344819.students}" var="student">
<p:column>
<p:commandButton id="selectButton" update=":form:display" oncomplete="studentDialog.show()" icon="ui-icon-search" title="View">
<f:setPropertyActionListener value="#{student}" target="#{so15344819.selectedStudent}" />
</p:commandButton>
</p:column>
</p:dataTable>
<p:dialog header="Student Detail" widgetVar="studentDialog" resizable="false" id="studentDlg"
showEffect="fade" hideEffect="explode" modal="true">
<h:panelGrid id="display" columns="2" cellpadding="4" style="margin:0 auto;">
<h:outputText value="Name:" />
<h:outputText value="#{so15344819.selectedStudent.name}" style="font-weight:bold"/>
<p:commandButton id="deleteButton" actionListener="#{so15344819.delete(so15344819.selectedStudent)}" oncomplete="studentDialog.hide()"
update=":form:students" value="Delete"/>
<p:commandButton id="cancelButton" onclick="studentDialog.hide()" value="Cancel"/>
</h:panelGrid>
</p:dialog>
</h:form>
</ui:define>
</ui:composition>
The managed bean:
package app.so.dev.web.controller;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import app.so.dev.web.model.Student;
#ManagedBean(name = "so15344819")
#ViewScoped
public class SO15344819 implements Serializable {
private static final long serialVersionUID = 6686378446131077581L;
private List<Student> students;
private Student selectedStudent;
#PostConstruct
public void init() {
students = new ArrayList<Student>();
students.add(new Student("Student 1"));
students.add(new Student("Student 2"));
}
public void delete(Student student) {
System.out.println("==================");
System.out.println(student);
students.remove(student);
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
public Student getSelectedStudent() {
return selectedStudent;
}
public void setSelectedStudent(Student selectedStudent) {
this.selectedStudent = selectedStudent;
}
}
Feel free to revert.

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