I face with situation, when item selection from does not change bean property. Method setCurrentOrg() does not invoked.
Managed bean code is:
#ManagedBean(name = "requestAccessBean")
#RequestScoped
public class RequestAccessSection {
private List<AccessRight> accessList;
private List<OrgUnit> orgList;
private String currentOrg;
public String getCurrentOrg() {
return this.currentOrg;
}
public void setCurrentOrg(String currentOrg) {
this.currentOrg = currentOrg;
}
public List<AccessRight> getAccessList() {
if (this.accessList == null) {
this.accessList = returnAccessList();
}
return this.accessList;
}
public void setAccessList(List<AccessRight> accessList) {
this.accessList = accessList;
}
public List<OrgUnit> getOrgList() {
if (this.orgList == null) {
this.orgList = returnOrgList();
}
return this.orgList;
}
public void setOrgList(List<OrgUnit> orgList) {
this.orgList = orgList;
}
public List<OrgUnit> returnOrgList() {
List<OrgUnit> orgList = new ArrayList<OrgUnit>();
orgList = getOfficeBranches();
return orgList;
}
public List<AccessRight> returnAccessList() {
List<AccessRight> accessList = new ArrayList<AccessRight>();
accessList = getAccessList();
return accessList;
}
}
Page is:
<h:form>
<h:selectOneMenu id="orgList" value="# {requestAccessBean.currentOrg}">
<f:selectItem itemLabel="--select--" itemValue="null"></f:selectItem>
<f:selectItems value="#{requestAccessBean.orgList}"
var="org" itemLabel="#{org.ou}" itemValue="#{org.globalid}"/>
<f:ajax event="change" execute="#this" render="accessTable"/>
</h:selectOneMenu>
<h:dataTable var="access" value="#{requestAccessBean.accessList}"
binding="#{requestAccessBean.htmlDataTable}" id="accessTable">
<h:column>
<h:selectOneRadio onclick="radioButton(this);" id="selectAccess"
valueChangeListener="#{requestAccessBean.setSelected}">
<f:selectItem itemValue="null" />
</h:selectOneRadio>
</h:column>
<h:column>
<h:outputText value="#{access.title}" />
</h:column>
</h:dataTable>
<h:form>
Could you please help me?
<h:selectOneMenu id="orgList" value="# {requestAccessBean.currentOrg}">
This is not valid EL. You have a space between # and {. Remove it.
<h:selectOneMenu id="orgList" value="#{requestAccessBean.currentOrg}">
Related
I am working on richfaces4,
when i am trying to edit details from a popup panel,i am not able to save/update the edited details into my database.can anybody help me to solve this.
My XHTML Page is as follows:
<!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://java.sun.com/jsf/html"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:t="http://myfaces.apache.org/tomahawk">
<h:head>
</h:head>
<h:body>
<h:form id="dboperatorform">
<t:saveState value="#{dBOperator.empList}"></t:saveState>
<t:saveState value="#{dBOperator.edit}"></t:saveState>
<t:saveState value="#{dBOperator.rowNum}"></t:saveState>
<t:saveState value="#{dBOperator.employee}"></t:saveState>
<rich:panel style="width:1345px">
<f:facet name="header">
<h:outputText value="Search Panel" />
</f:facet>
<h:panelGrid id="searchPanel" columns="6">
<h:column>
<h:outputLabel value="First Name:" />
</h:column>
<h:column>
<h:inputText value="#{dBOperator.first_Name}" />
</h:column>
<h:column>
<h:outputLabel value="Last Name:" />
</h:column>
<h:column>
<h:inputText value="#{dBOperator.last_name}" />
</h:column>
<h:column>
<a4j:commandButton value="Search" action="#{dBOperator.search}"
render="richtable">
</a4j:commandButton>
</h:column>
<h:column>
</h:column>
</h:panelGrid>
</rich:panel>
<br />
<a4j:commandButton value="Add" action="#{dBOperator.editdetails}"
execute="#this" style="width:30px;height:30px;"
oncomplete="#{rich:component('popup1')}.show();"
render="richtable popup1">
<f:setPropertyActionListener target="#{dBOperator.edit}"
value="false"></f:setPropertyActionListener>
</a4j:commandButton>
<p></p>
<rich:dataTable value="#{dBOperator.employees}" var="emp"
id="richtable" cellpadding="0" cellspacing="0" style="width:1349px"
reRender="richtable" iterationStatusVar="it"
noDataLabel="No Records Found!" rows="5">
<rich:column>
<f:facet name="header">#</f:facet>
<h:outputText value="#{it.index}" />
</rich:column>
<rich:column>
<f:facet name="header">
<a4j:commandLink value="First Name"
action="#{dBOperator.sortByFirstName}" render="richtable"></a4j:commandLink>
</f:facet>
<h:outputText value="#{emp.first_Name}" />
</rich:column>
<rich:column>
<f:facet name="header">Last Name</f:facet>
<h:outputText value="#{emp.last_name}" />
</rich:column>
<rich:column>
<f:facet name="header">Edit</f:facet>
<a4j:commandButton image="images/edit.png"
action="#{dBOperator.editdetails}" execute="#this"
style="width:30px;height:30px;"
oncomplete="#{rich:component('popup1')}.show();"
render="richtable popup1">
<f:setPropertyActionListener target="#{dBOperator.rowNum}"
value="#{it.index}"></f:setPropertyActionListener>
<f:setPropertyActionListener target="#{dBOperator.edit}"
value="true"></f:setPropertyActionListener>
</a4j:commandButton>
</rich:column>
<f:facet name="footer">
<rich:dataScroller for="richtable" align="right"
status="ajaxProcessIcon" renderIfSinglePage="false" fastStep="5"
fastControls="auto" />
</f:facet>
</rich:dataTable>
<rich:popupPanel id="popup1" minHeight="300"
minWidth="300">
<t:saveState value="#{dBOperator.edit}"></t:saveState>
<t:saveState value="#{dBOperator.rowNum}"></t:saveState>
<f:facet name="header">
<h:outputText value="Simple popup panel" />
</f:facet>
<f:facet name="controls">
<h:outputLink
onclick="#{rich:component('popup1')}.hide(); return false;"
reRender="richtable">X</h:outputLink>
</f:facet>
<rich:toolbar itemSeparator="line" width="100%" id="qawq">
<rich:toolbarGroup location="left">
<h:outputText
value="#{dBOperator.edit?'Employee Edit':'Employee Add'}" />
</rich:toolbarGroup>
</rich:toolbar>
<h:panelGrid id="CancelGrid" columns="2" width="100%">
<rich:column width="30%" style="border:none;">
<h:outputText value="First Name" style="font-weight: bold;" />
<font class="star" color="red">*</font>
</rich:column>
<rich:column width="70%" style="border:none;">
<h:inputText value="#{dBOperator.employee.first_Name}"
style="width:100%" id="abbr" />
</rich:column>
<rich:column width="30%" style="border:none;">
<h:outputText value="Last Name" style="font-weight: bold;" />
<font class="star" color="red">*</font>
</rich:column>
<rich:column width="70%" style="border:none;">
<h:inputText value="#{dBOperator.employee.last_name}"
style="width:100%" id="abbr1" />
</rich:column>
</h:panelGrid>
<a4j:commandButton value="#{dBOperator.edit?'Edit':'Save'}"
action="#{dBOperator.savedetails}"
oncomplete="#{rich:component('popup1')}.hide();"
onclick="validateFields();"></a4j:commandButton>
</rich:popupPanel>
<script type="text/javascript">
function validateFields() {
alert('came inside validateFields');
var fn=document.getElementById('dboperatorform:abbr');
alert('fn '+fn.value);
var ln=document.getElementById('dboperatorform:abbr1');
alert('ln '+ln.value);
}
</script>
</h:form>
</h:body>
</html>
My Bean Class is as follows
package com.richfaces.db;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.management.MBeanServer;
import javax.swing.SortOrder;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.comparator.NameComparator;
import com.comparator.NameCompartor1;
import com.richfaces.Employee;
public class DBOperator {
String id;
String first_Name;
String last_name;
String fName;
String lName;
Employee employee;
int rowNum;
boolean edit;
Boolean search=Boolean.FALSE;
Boolean status = Boolean.FALSE;
List<Employee> list = new ArrayList<Employee>();
List<Employee> empList = new ArrayList<Employee>();
public boolean isEdit() {
return edit;
}
public void setEdit(boolean edit) {
this.edit = edit;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
public int getRowNum() {
return rowNum;
}
public void setRowNum(int rowNum) {
this.rowNum = rowNum;
}
public String getFirst_Name() {
return first_Name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public void setFirst_Name(String first_Name) {
this.first_Name = first_Name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public List<Employee> getEmployees() {
System.out.println("hghhghhhhh");
String sql2="";
Query query = null;
String sql1 = "from Employee as emp where id is not null ";
Configuration cfg = new Configuration();
cfg.configure();
SessionFactory sf = cfg.buildSessionFactory();
Session sess = sf.openSession();
sess.beginTransaction();
if(status==false && search==false) {
System.out.println("Normal Query +++++++++++++++++++++++ " + sql1);
String appendedQuery = createCriteriaQuery(sql1);
System.out.println("Appended Query ++++++++++++++++++++++++ "
+ appendedQuery);
query = sess.createQuery(appendedQuery);
}else if(status==true && search==true) {
System.out.println("came inside when search and status is true");
String appendedQuery = createCriteriaQuery(sql1);
System.out.println("Appended Query ++++++++++++++++++++++++ "
+ appendedQuery);
query = sess.createQuery(appendedQuery);
}else if(status==false && search==true) {
System.out.println("came inside when search is true and status is false");
String appendedQuery = createCriteriaQuery(sql1);
System.out.println("Appended Query ++++++++++++++++++++++++ "
+ appendedQuery);
query = sess.createQuery(appendedQuery);
}else if(status==true && search==false) {
System.out.println("came inside when search is false and status is true");
String appendedQuery = createCriteriaQuery(sql1);
System.out.println("Appended Query ++++++++++++++++++++++++ "
+ appendedQuery);
query = sess.createQuery(appendedQuery);
}
list = query.list();
/*if (status) {
Collections.sort(list, new NameComparator());
} else {
Collections.sort(list, new NameCompartor1());
}*/
System.out.println(list.size());
this.empList = list;
return list;
}
public String createCriteriaQuery(String query) {
StringBuffer sb = new StringBuffer(query);
if (getFirst_Name() != null && !(getFirst_Name().equals(""))) {
sb.append(" and emp.first_Name='" + first_Name + "'");
}
if (getLast_name() != null && !(getLast_name().equals(""))) {
sb.append(" and emp.last_name='" + last_name + "'");
}
if(this.status) {
sb.append(" order by emp.first_Name desc");
}else {
sb.append(" order by emp.first_Name");
}
System.out.println(sb.toString());
return sb.toString();
}
public void editdetails() {
if (edit) {
System.out.println("rownum is " + rowNum);
System.out.println(empList.size());
employee = (Employee) empList.get((rowNum));
System.out.println(employee.getFirst_Name() + "\t"
+ employee.getLast_name() + "\t" + employee.getId());
System.out.println("end of edit details method");
} else {
employee = new Employee();
}
}
public List<Employee> getEmpList() {
return empList;
}
public void setEmpList(List<Employee> empList) {
this.empList = empList;
}
public void savedetails() {
System.out.println("came into savedetails");
Configuration cfg = new Configuration();
cfg.configure();
SessionFactory sf = cfg.buildSessionFactory();
Session sess = sf.openSession();
if(this.edit) {
System.out.println("EMPLOYEE DETAILS in IF "+getEmployee().getFirst_Name()+"\t"+getEmployee().getLast_name());
System.out.println("came inside if when edit is true");
sess.update(employee);
}else {
System.out.println("came inside else when edit is false");
System.out.println("EMPLOYEE DETAILS in ELSE "+getEmployee().getFirst_Name()+"\t"+getEmployee().getLast_name());
sess.save(employee);
}
}
public void search() {
System.out.println("came into search method");
search = true;
}
public void refreshData(ActionEvent ae) {
System.out.println("came into refreshdata");
getEmployees();
}
public void sortByFirstName() {
System.out.println("came inside sortByFirstName");
if (!status) {
status = Boolean.TRUE;
} else {
status = Boolean.FALSE;
}
//status=Boolean.TRUE;
getEmployees();
}
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
}
My Entity class is as follows:
package com.richfaces;
import java.io.Serializable;
public class Employee implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
String id;
String first_Name;
String last_name;
public String getFirst_Name() {
return first_Name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public void setFirst_Name(String first_Name) {
System.out.println("came inside setter method of firstname");
this.first_Name = first_Name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
System.out.println("came inside setter method of lastname");
this.last_name = last_name;
}
}
if your exact problem is not seeing #{dBOperator.savedetails} action triggered:
i couldn't figure out the root cause yet, but this problem should be fixed by adding modal="true" to the rich:popupPanel attributes and moving validateFields() function into the popup panel. i faced this problem many times and saw it working properly on different popup components, including this one.
I am not able to get the required data in rich popup panel when I click on edit link. Can anybody help me in resolving it.
When I am trying to edit row details, I am not able to get the required data in their respective fields for the first time, but surprisingly when i refresh my page and do the same procedure, I got the data in their respective fields.
I want to get the required data on click of edit link for the first time.
So, can anybody help me to resolve it.
My xhtml page
<!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://java.sun.com/jsf/html"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:t="http://myfaces.apache.org/tomahawk">
<h:head>
</h:head>
<h:body>
<h:form id="dboperatorform">
<t:saveState value="#{dBOperator.empList}"></t:saveState>
<t:saveState value="#{dBOperator.edit}"></t:saveState>
<t:saveState value="#{dBOperator.rowNum}"></t:saveState>
<t:saveState value="#{dBOperator.employee}"></t:saveState>
<rich:panel style="width:1345px">
<f:facet name="header">
<h:outputText value="Search Panel" />
</f:facet>
<h:panelGrid id="searchPanel" columns="6">
<h:column>
<h:outputLabel value="First Name:" />
</h:column>
<h:column>
<h:inputText value="#{dBOperator.first_Name}" />
</h:column>
<h:column>
<h:outputLabel value="Last Name:" />
</h:column>
<h:column>
<h:inputText value="#{dBOperator.last_name}" />
</h:column>
<h:column>
<a4j:commandButton value="Search" action="#{dBOperator.search}"
render="richtable">
</a4j:commandButton>
</h:column>
<h:column>
</h:column>
</h:panelGrid>
</rich:panel>
<br />
<a4j:commandButton value="Add" action="#{dBOperator.editdetails}"
execute="#this" style="width:30px;height:30px;"
oncomplete="#{rich:component('popup1')}.show();"
render="richtable popup1">
<f:setPropertyActionListener target="#{dBOperator.edit}"
value="false"></f:setPropertyActionListener>
</a4j:commandButton>
<p></p>
<rich:dataTable value="#{dBOperator.employees}" var="emp"
id="richtable" cellpadding="0" cellspacing="0" style="width:1349px"
reRender="richtable" iterationStatusVar="it"
noDataLabel="No Records Found!" rows="5">
<rich:column>
<f:facet name="header">#</f:facet>
<h:outputText value="#{it.index}" />
</rich:column>
<rich:column>
<f:facet name="header">
<a4j:commandLink value="First Name"
action="#{dBOperator.sortByFirstName}" render="richtable"></a4j:commandLink>
</f:facet>
<h:outputText value="#{emp.first_Name}" />
</rich:column>
<rich:column>
<f:facet name="header">Last Name</f:facet>
<h:outputText value="#{emp.last_name}" />
</rich:column>
<rich:column>
<f:facet name="header">Edit</f:facet>
<a4j:commandButton image="images/edit.png"
action="#{dBOperator.editdetails}" execute="#this"
style="width:30px;height:30px;"
oncomplete="#{rich:component('popup1')}.show();"
render="richtable popup1">
<f:setPropertyActionListener target="#{dBOperator.rowNum}"
value="#{it.index}"></f:setPropertyActionListener>
<f:setPropertyActionListener target="#{dBOperator.edit}"
value="true"></f:setPropertyActionListener>
</a4j:commandButton>
</rich:column>
<f:facet name="footer">
<rich:dataScroller for="richtable" align="right"
status="ajaxProcessIcon" renderIfSinglePage="false" fastStep="5"
fastControls="auto" />
</f:facet>
</rich:dataTable>
<rich:popupPanel id="popup1" minHeight="300"
minWidth="300">
<t:saveState value="#{dBOperator.edit}"></t:saveState>
<t:saveState value="#{dBOperator.rowNum}"></t:saveState>
<f:facet name="header">
<h:outputText value="Simple popup panel" />
</f:facet>
<f:facet name="controls">
<h:outputLink
onclick="#{rich:component('popup1')}.hide(); return false;"
reRender="richtable">X</h:outputLink>
</f:facet>
<rich:toolbar itemSeparator="line" width="100%" id="qawq">
<rich:toolbarGroup location="left">
<h:outputText
value="#{dBOperator.edit?'Employee Edit':'Employee Add'}" />
</rich:toolbarGroup>
</rich:toolbar>
<h:panelGrid id="CancelGrid" columns="2" width="100%">
<rich:column width="30%" style="border:none;">
<h:outputText value="First Name" style="font-weight: bold;" />
<font class="star" color="red">*</font>
</rich:column>
<rich:column width="70%" style="border:none;">
<h:inputText value="#{dBOperator.employee.first_Name}"
style="width:100%" id="abbr" />
</rich:column>
<rich:column width="30%" style="border:none;">
<h:outputText value="Last Name" style="font-weight: bold;" />
<font class="star" color="red">*</font>
</rich:column>
<rich:column width="70%" style="border:none;">
<h:inputText value="#{dBOperator.employee.last_name}"
style="width:100%" id="abbr1" />
</rich:column>
</h:panelGrid>
<a4j:commandButton value="#{dBOperator.edit?'Edit':'Save'}"
action="#{dBOperator.savedetails}"
oncomplete="#{rich:component('popup1')}.hide();"
onclick="validateFields();"></a4j:commandButton>
</rich:popupPanel>
<script type="text/javascript">
function validateFields() {
alert('came inside validateFields');
var fn=document.getElementById('dboperatorform:abbr');
alert('fn '+fn.value);
var ln=document.getElementById('dboperatorform:abbr1');
alert('ln '+ln.value);
}
</script>
</h:form>
</h:body>
</html>
My Bean Class code
package com.richfaces.db;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.management.MBeanServer;
import javax.swing.SortOrder;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.comparator.NameComparator;
import com.comparator.NameCompartor1;
import com.richfaces.Employee;
public class DBOperator {
String id;
String first_Name;
String last_name;
String fName;
String lName;
Employee employee;
int rowNum;
boolean edit;
Boolean search=Boolean.FALSE;
Boolean status = Boolean.FALSE;
List<Employee> list = new ArrayList<Employee>();
List<Employee> empList = new ArrayList<Employee>();
public boolean isEdit() {
return edit;
}
public void setEdit(boolean edit) {
this.edit = edit;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
public int getRowNum() {
return rowNum;
}
public void setRowNum(int rowNum) {
this.rowNum = rowNum;
}
public String getFirst_Name() {
return first_Name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public void setFirst_Name(String first_Name) {
this.first_Name = first_Name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public List<Employee> getEmployees() {
System.out.println("hghhghhhhh");
String sql2="";
Query query = null;
String sql1 = "from Employee as emp where id is not null ";
Configuration cfg = new Configuration();
cfg.configure();
SessionFactory sf = cfg.buildSessionFactory();
Session sess = sf.openSession();
sess.beginTransaction();
if(status==false && search==false) {
System.out.println("Normal Query +++++++++++++++++++++++ " + sql1);
String appendedQuery = createCriteriaQuery(sql1);
System.out.println("Appended Query ++++++++++++++++++++++++ "
+ appendedQuery);
query = sess.createQuery(appendedQuery);
}else if(status==true && search==true) {
System.out.println("came inside when search and status is true");
String appendedQuery = createCriteriaQuery(sql1);
System.out.println("Appended Query ++++++++++++++++++++++++ "
+ appendedQuery);
query = sess.createQuery(appendedQuery);
}else if(status==false && search==true) {
System.out.println("came inside when search is true and status is false");
String appendedQuery = createCriteriaQuery(sql1);
System.out.println("Appended Query ++++++++++++++++++++++++ "
+ appendedQuery);
query = sess.createQuery(appendedQuery);
}else if(status==true && search==false) {
System.out.println("came inside when search is false and status is true");
String appendedQuery = createCriteriaQuery(sql1);
System.out.println("Appended Query ++++++++++++++++++++++++ "
+ appendedQuery);
query = sess.createQuery(appendedQuery);
}
list = query.list();
/*if (status) {
Collections.sort(list, new NameComparator());
} else {
Collections.sort(list, new NameCompartor1());
}*/
System.out.println(list.size());
this.empList = list;
return list;
}
public String createCriteriaQuery(String query) {
StringBuffer sb = new StringBuffer(query);
if (getFirst_Name() != null && !(getFirst_Name().equals(""))) {
sb.append(" and emp.first_Name='" + first_Name + "'");
}
if (getLast_name() != null && !(getLast_name().equals(""))) {
sb.append(" and emp.last_name='" + last_name + "'");
}
if(this.status) {
sb.append(" order by emp.first_Name desc");
}else {
sb.append(" order by emp.first_Name");
}
System.out.println(sb.toString());
return sb.toString();
}
public void editdetails() {
if (edit) {
System.out.println("rownum is " + rowNum);
System.out.println(empList.size());
employee = (Employee) empList.get((rowNum));
System.out.println(employee.getFirst_Name() + "\t"
+ employee.getLast_name() + "\t" + employee.getId());
System.out.println("end of edit details method");
} else {
employee = new Employee();
}
}
public List<Employee> getEmpList() {
return empList;
}
public void setEmpList(List<Employee> empList) {
this.empList = empList;
}
public void savedetails() {
System.out.println("came into savedetails");
Configuration cfg = new Configuration();
cfg.configure();
SessionFactory sf = cfg.buildSessionFactory();
Session sess = sf.openSession();
if(this.edit) {
System.out.println("EMPLOYEE DETAILS in IF "+getEmployee().getFirst_Name()+"\t"+getEmployee().getLast_name());
System.out.println("came inside if when edit is true");
sess.update(employee);
}else {
System.out.println("came inside else when edit is false");
System.out.println("EMPLOYEE DETAILS in ELSE "+getEmployee().getFirst_Name()+"\t"+getEmployee().getLast_name());
sess.save(employee);
}
}
public void search() {
System.out.println("came into search method");
search = true;
}
public void refreshData(ActionEvent ae) {
System.out.println("came into refreshdata");
getEmployees();
}
public void sortByFirstName() {
System.out.println("came inside sortByFirstName");
if (!status) {
status = Boolean.TRUE;
} else {
status = Boolean.FALSE;
}
//status=Boolean.TRUE;
getEmployees();
}
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
}
My Entity class code as follows:
package com.richfaces;
import java.io.Serializable;
public class Employee implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
String id;
String first_Name;
String last_name;
public String getFirst_Name() {
return first_Name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public void setFirst_Name(String first_Name) {
this.first_Name = first_Name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
}
Change a4j:commandButton attributes
<a4j:commandButton value="Edit" action="#{dBOperator.editdetails}" execute="#this"
oncomplete="#{rich:component('popup1')}.show();"
render="richtable, popup1">
<f:setPropertyActionListener target="#{dBOperator.rowNum}" value="#{it.index}" />
</a4j:commandButton>
I'm using primefaces 3.5 and watching a strange datatable behavior when editing data.
If I enter wrong data in 'year' field and click on the tick I get a message about mistake.
But if I don't click on the tick and click on the 'Add row' button I don't get a message. Only new row is added.
I expect to stay in edit mode. How to solve this problem?
<h:panelGrid columns="2" cellpadding="5">
<h:outputText value="In cell editing" />
<p:dataTable id="inCellEditing" var="car" value="#{dataTableController.cars}" rowKey="#{car.name}" editable="true">
<p:ajax event="rowEdit" listener="#{dataTableController.onEdit}" update=":mainForm:growl" />
<p:ajax event="rowEditCancel" listener="#{dataTableController.onCancel}" update=":mainForm:growl" />
<p:column headerText="Year">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{car.year}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{car.year}">
<p:ajax event="blur" update="#this"/>
</p:inputText>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Name">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{car.name}" />
</f:facet>
<f:facet name="input">
<h:selectOneMenu value="#{car.name}" >
<f:selectItems value="#{dataTableController.carNames}"
var="name"
itemLabel="#{name}"
itemValue="#{name}" />
</h:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Actions">
<p:rowEditor />
</p:column>
</p:dataTable>
<p:commandButton action="#{dataTableController.addRow()}" value="Add row" ajax="false"/>
</h:panelGrid>
public class DataTableController implements Serializable {
private List<Car> cars;
private Car selectedCar;
private Car[] selectedCars;
private List<Car> selectedCarsList;
private SelectItem[] carNamesOptions;
public DataTableController() {
cars = new ArrayList<Car>(CarConverter.cars.values());
}
public String[] getCarNames() {
return CarConverter.cars.keySet().toArray(new String[0]);
}
public SelectItem[] getCarNamesAsOptions() {
carNamesOptions = createFilterOptions(CarConverter.cars.keySet().toArray(new String[0]));
return carNamesOptions;
}
private SelectItem[] createFilterOptions(String[] data) {
SelectItem[] options = new SelectItem[data.length + 1];
options[0] = new SelectItem("", "Select");
for(int i = 0; i < data.length; i++) {
options[i + 1] = new SelectItem(data[i], data[i]);
}
return options;
}
public void onEdit(RowEditEvent event) {
MessageUtil.addInfoMessage("car.edit", ((Car) event.getObject()).getName());
}
public void onCancel(RowEditEvent event) {
MessageUtil.addInfoMessage("car.edit.cancelled", ((Car) event.getObject()).getName());
}
public Car getSelectedCar() {
return selectedCar;
}
public void setSelectedCar(Car selectedCar) {
this.selectedCar = selectedCar;
}
public Car[] getSelectedCars() {
return selectedCars;
}
public void setSelectedCars(Car[] selectedCars) {
this.selectedCars = selectedCars;
}
public List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
public List<Car> getSelectedCarsList() {
return selectedCarsList;
}
public void setSelectedCarsList(List<Car> selectedCarsList) {
this.selectedCarsList = selectedCarsList;
}
public void addRow(){
cars.add(new Car("",0));
}
}
I implemented code that allows me to display a dropdown which depends on another. Everything works fine except when I try to retrieve and display the value of the two fields, it raises the following error:
form:parcours : erreur : de validation. la valeur est incrorrecte
in english :
form:parcours : validation error. value is not valid
I am using JSF 2.0, EJB 3.0, JPA 2.0 and PrimeFaces 3.2.
View:
<h:form id="form" >
<!-- <p:growl id="msgs" showDetail="true"/> -->
<h:messages globalOnly="true"/>
<p:growl id="msgs" showDetail="true" />
<p:panel header="Double Combo" style="margin-bottom:10px;">
<h:panelGrid columns="2" cellpadding="5">
<p:selectOneMenu id="countries" value="#{plansEtude.selectedDep}">
<f:selectItem itemLabel="Select Country" itemValue="" />
<f:selectItems value="#{plansEtude.depList}" var="c" itemLabel="#{c.nomDepFr}" itemValue="#{c.id}"/>
<p:ajax update="parcours,parcoursTab"
listener="#{plansEtude.handleDepChange}" />
</p:selectOneMenu>
<p:selectOneMenu id="parcours" value="#{plansEtude.selectedParcours}" >
<f:convertNumber maxFractionDigits="0"/>
<f:selectItem itemLabel="Select City" itemValue="" />
<f:selectItems value="#{plansEtude.parcoursList}" var="ct" itemLabel="#{ct.designParcours}" itemValue="#{ct.id}" />
</p:selectOneMenu>
</h:panelGrid>
<p:separator />
<p:commandButton value="Submit" update="msgs" actionListener="#{plansEtude.displayLocation}" id="btnSubmit"/>
</p:panel>
Controller :
#EJB
private DepartementFacade departementFacade;
#EJB
private ParcoursFacade parcoursFacade;
private List<Departement> depList;
private List<Parcours> parcoursList;
private Integer selectedDep;
private Integer selectedParcours;
public PlansEtude() {
}
public DepartementFacade getDepartementFacade() {
return departementFacade;
}
public void setDepartementFacade(DepartementFacade departementFacade) {
this.departementFacade = departementFacade;
}
public ParcoursFacade getParcoursFacade() {
return parcoursFacade;
}
public void setParcoursFacade(ParcoursFacade parcoursFacade) {
this.parcoursFacade = parcoursFacade;
}
public List<Departement> getDepList() {
depList = getDepartementFacade().findAll();
return depList;
}
public void setDepList(List<Departement> depList) {
this.depList = depList;
}
public List<Parcours> getParcoursList() {
return parcoursList;
}
public void setParcoursList(List<Parcours> parcoursList) {
this.parcoursList = parcoursList;
}
public Integer getSelectedDep() {
return selectedDep;
}
public void setSelectedDep(Integer selectedDep) {
this.selectedDep = selectedDep;
}
public Integer getSelectedParcours() {
return selectedParcours;
}
public void setSelectedParcours(Integer selectedParcours) {
this.selectedParcours = selectedParcours;
}
public void handleDepChange(){
if(selectedDep !=null && !selectedDep.equals(""))
parcoursList = parcoursFacade.findParcoursInDep(selectedDep);
else
parcoursList = new ArrayList<Parcours>();
}
public void handleParcoursChange(){
}
public void displayLocation() {
String monMessage="Departement :" + selectedDep + ", Parcours : " + selectedParcours;
FacesMessage msg = new FacesMessage("Selected", monMessage);
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
parcoursFacade :
public List<Parcours> findParcoursInDep(Integer dep){
Query query = em.createNamedQuery("Parcours.findParcoursInDep");
query.setParameter("dep", dep);
return (List<Parcours>)query.getResultList();
}
Named query :
#NamedQuery(name = "Parcours.findParcoursInDep", query = "SELECT p FROM Parcours p WHERE p.departementid.id = :dep"),
Remove the <f:convertNumber maxFractionDigits="0"/> from your parcours dropdown. It makes no sense. It would only convert the number to BigDecimal while you need an Integer.
Another possible cause is that the #{plansEtude.parcoursList} has incompatibly changed during the form submit because the managed bean is request scoped. You need to make sure that the managed bean is placed in at least the view scope, so that the parcoursList is preserved for the submit.
I am facing an issue with h:selectOneRadio's valueChangeListener="#{user.loadYesNo}"
(I use Mojarra 2-0-8 on Tomcat-7) .
If I remove both the panelGrid enclosing the 'h:selectOneRadio', then the value change litener is getting fired.
View:
<!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">
<h:head><title> Starting JSF</title></h:head>
<h:body>
<h:form>
<h:panelGrid column="2">
<h:outputLabel>User Name</h:outputLabel>
<h:inputText id="loginName" value="#{user.userName}"></h:inputText>
<h:outputLabel>Password</h:outputLabel>
<h:inputSecret id="loginPassword" value="#{user.password}"></h:inputSecret>
</h:panelGrid>
<h:commandButton value="Submit" action ="#{user.validateLogin}">
<f:ajax execute="#form" render="yesNoRadioGrid message"></f:ajax>
</h:commandButton>
<h:panelGrid>
<h:outputText id ="message" value="#{user.message}"></h:outputText>
</h:panelGrid>
<h:panelGrid id="yesNoRadioGrid">
<h:panelGrid columns="2" rendered="#{user.yesNoRadioGridFlag}">
<h:outputText id ="otherLbl" value="Select Yes or No"></h:outputText>
<h:selectOneRadio id="yesNoRadio" value ="#{user.yesNoRadio}" valueChangeListener="#{user.loadYesNo}">
<f:selectItem itemValue="1" itemLabel="YES"></f:selectItem>
<f:selectItem itemValue="0" itemLabel="NO"></f:selectItem>
<f:ajax event="change" execute="#form" render="userDetailsGrid "></f:ajax>
</h:selectOneRadio>
</h:panelGrid>
</h:panelGrid>
<h:message for ="yesNoRadio"> </h:message>
<h:panelGrid id="userDetailsGrid">
<h:panelGrid columns="2" rendered="#{user.userDetailsGridFlag}">
<h:outputLabel>Name :</h:outputLabel>
<h:inputText id="customerName" value="#{user.customerName}"></h:inputText>
<h:outputLabel>Salary: </h:outputLabel>
<h:inputText id="customerSalary" value="#{user.customerSalary}"></h:inputText>
</h:panelGrid>
</h:panelGrid>
</h:form>
</h:body>
</html>
Model+Controller mingled:
package com.jsf.test;
import javax.faces.bean.*;
import javax.faces.event.ValueChangeEvent;
#ManagedBean
public class User {
private String userName;
private String password;
private String message;
private String customerName;
private String customerSalary;
private Integer yesNoRadio;
private Boolean yesNoRadioGridFlag;
private Boolean userDetailsGridFlag;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getCustomerSalary() {
return customerSalary;
}
public void setCustomerSalary(String customerSalary) {
this.customerSalary = customerSalary;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getYesNoRadio() {
return yesNoRadio;
}
public void setYesNoRadio(Integer yesNoRadio) {
this.yesNoRadio = yesNoRadio;
}
public Boolean getUserDetailsGridFlag() {
return userDetailsGridFlag;
}
public void setUserDetailsGridFlag(Boolean userDetailsGridFlag) {
this.userDetailsGridFlag = userDetailsGridFlag;
}
public Boolean getYesNoRadioGridFlag() {
return yesNoRadioGridFlag;
}
public void setYesNoRadioGridFlag(Boolean yesNoRadioGridFlag) {
this.yesNoRadioGridFlag = yesNoRadioGridFlag;
}
public String validateLogin() {
if (userName.equals("xyz") && password.equals("xyz")) {
message = "Login Success";
yesNoRadioGridFlag = true;
} else {
yesNoRadioGridFlag = false;
message = "Login Failure";
}
return message;
}
public void loadYesNo(ValueChangeEvent evt){
Integer yesNoValue = (Integer)evt.getNewValue();
setYesNoRadio(yesNoValue);
userDetailsGridFlag = true;
}
}
You need to put the bean in the view scope in order to retain the underlying condition of the rendered attribute for the subsequent requests.
#ManagedBean
#ViewScoped
public class User {
// ...
}
Unrelated to the concrete problem, the valueChangeListener is intended to be used whenever you want to have a hook on the server side value change event which allows you to have both the old value and the new value at your hands. For example, to log an event. It's not intended to perform business actions based on the change. For that you should be using the listener attribute of <f:ajax> instead.
So, replace
<h:selectOneRadio id="yesNoRadio" value ="#{user.yesNoRadio}" valueChangeListener="#{user.loadYesNo}">
<f:selectItem itemValue="1" itemLabel="YES"></f:selectItem>
<f:selectItem itemValue="0" itemLabel="NO"></f:selectItem>
<f:ajax event="change" execute="#form" render="userDetailsGrid "></f:ajax>
</h:selectOneRadio>
with
<h:selectOneRadio id="yesNoRadio" value ="#{user.yesNoRadio}">
<f:selectItem itemValue="1" itemLabel="YES"></f:selectItem>
<f:selectItem itemValue="0" itemLabel="NO"></f:selectItem>
<f:ajax execute="#form" listener="#{user.loadYesNo}" render="userDetailsGrid"></f:ajax>
</h:selectOneRadio>
and remove the ValueChangeEvent attribute from the method.