Dependent p:selectOneMenu Population of values - java

I am using JSF 2.0 with Primefaces 3.4.2
I have two p:selectOneMenu, first one parent and second child, based on parent value, child component gets populated.
Parent p:selectOneMenu
<p:selectOneMenu id="empl" value="#{empMB.employee}">
<f:selectItems value="#{empMB.employeeList}" var="emp"
itemLabel="#{emp.employeeName}" itemValue="#{emp.employeeNumber}"/>
<p:ajax update="department" />
</p:selectOneMenu>
Child p:selectOneMenu
<p:selectOneMenu id="department" value="#{deptMB.department}">
<f:selectItems value="#{deptMB.loadDepartments(<??>)}" var="dept"
itemLabel="#{dept.departmentName}" itemValue="#{dept.departmentCode}"/>
</p:selectOneMenu>
I have a method in department ManagedBean called loadDepartments with one argument
public void loadDepartments(String employeeNumber)
How can I pass value to loadDepartments in child component so that it will load all the departments based on the code selected in parent component?
If I am substituting #{deptMB.loadDepartments(empMB.employee.employeeCode)} I am getting
Error Parsing: #{deptMB.loadDepartments({empMB.employee.employeeCode})}
Any help is highly appreciable?

I'd do this:
Add a list variable to your bean (and the appropriate getter): this list will hold the values for the child combo box;
Add a listener to the p:ajax call on the parent combo box: the listener populates the list of child values (you'll have access to the selected parent item inside its listener); and
Update your xhtml to use the values from the list created on step 1 instead of the loadDepartments method you're trying to invoke.
This is normally how I do this sort of thing and it should work out for you.
EDIT
Code for the page:
<p:selectOneMenu id="empl" value="#{empMB.employee}" converter="#{employeeConverter}">
<f:selectItems value="#{empMB.employeeList}" var="emp" itemLabel="#{emp.employeeName}" itemValue="#{emp.employeeNumber}"/>
<p:ajax update="department" listener="#{empMB.onEmployeeSelect}" process="#this"/>
</p:selectOneMenu>
<p:selectOneMenu id="department" value="#{deptMB.department}" converter="#{departmentConverter}">
<f:selectItems value="#{empMB.departmentList}" var="dept" itemLabel="#{dept.departmentName}" itemValue="#{dept.departmentCode}"/>
</p:selectOneMenu>
Snippet for the bean:
public class EmpMB{
...
private List<Department> departmentList;
private Employee employee;
public List getDepartmentList(){
return departmentList;
}
public void onEmployeeSelect(){
departmentList = someService.getDepartmentsForEmployee(employee);
}
...
}
Converter sample (note that it's a spring component so that I can inject my service layer into it, but you don't HAVE to do it this way):
#Component("employeeConverter")
public class EmployeeConverter implements Converter {
#Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
//TODO: implement this
}
#Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
//TODO: implement this
}
}

Related

selectOneMenu value null when using selectItems from a different backing bean

I have a form where I create a user. In my form, I have multiple properties for that user (I actually use a User object for the retainment of data on submit to the backing bean)
create.xhtml
<h:form>
<h:outputLabel for="user_name" value="Name:" />
<h:inputText id="user_name" value="#{createUserView.newUser.username}" />
<br/><br/>
<h:outputLabel for="user_password" value="Default Password*:" />
<h:inputSecret id="user_password" value="#{createUserView.newUser.password}"></h:inputSecret><br/><br/>
<h:outputLabel for="user_organization" value="Organization:" />
<h:selectOneMenu id="user_organization" disabled="true" value="#{createUserView.newUser.organizationId}">
<f:selectItems
value="#{organizationBean.allOrganizations}"
var="org"
itemLabel="#{org.organizationName}"
itemValue="#{org.id}" />
</h:selectOneMenu><br/><br/>
<h:commandButton value="Create" action="#{createUserView.createNewUser}" />
</h:form>
CreateUserView
#ManagedBean(name = "createUserView")
#RequestScoped
public class CreateUserView {
private UserServices userSerivces;
private User newUser;
#ManagedProperty(value="#{organizationBean}")
private OrganizationBean organizationBean;
public CreateUserView() {
newUser = new User();
userSerivces = new UserServices();
}
public void createNewUser() {
userSerivces.createNewUser(newUser);
}
// Getters and Setters
}
OrganizationBean
#ManagedBean(name = "organizationBean")
#RequestScoped
public class OrganizationBean {
private List<Organization> allOrganizations;
private OrganizationServices orgServices;
public OrganizationBean() {
orgServices = new OrganizationServices();
allOrganizations = orgServices.retrieveAllOrganizations();
}
// Getters and Setters
}
The issue here is that when I reference the newUser object in the backing bean, the organizationId value is null.
I assume this is because OrganizationBean (excuse the confusing in naming, refactoring) is either not rendered for my current view or I need to somehow inject.
I've tried a managed property in the CreateUserView backing bean that references the OrganizationBean, but no luck. The organizationID value in the newUser object is null.
Do I need to populate a list in the CreateUserView bean using the OrganizationBean injection, so that it has it's own list it can render?
What am I missing? Feeling foolish.
JSF 2.0
The problem, as stated in the comments is that you don't have a Converter for your Organization class.
You must have it in order to know what Organization matches every SelectItem. The converter must be something like:
#FacesConverter(forClass = Organization.class, value = "organizationConverter")
public class OrganizationConverter implements Converter
{
#Override
public Object getAsObject(FacesContext fc, UIComponent uic, String id)
{
if (StringUtils.isEmpty(id))
{
return null;
}
// Convert id to an Organizacion
return organization;
}
#Override
public String getAsString(FacesContext fc, UIComponent uic, Object o)
{
if (o instanceof Organization)
{
return ...;//Convert organization to id
}
return null;
}
}
And then in your selectonemenu:
<h:selectOneMenu id="user_organization" disabled="true" value="#{createUserView.newUser.organizationId}"
converter="organizationConverter">

JSF primefaces showing list of object not list of string

I already tried to search through this site, but maybe I'm not getting understand well.
Please kindly advice for my cases..
I'm using Netbeans 7.3 + Primefaces + Hibernate.
I want to show a list from my query.
My query already in.. and there's no error showup, but the display is not what I want (I think it returning an object or something, not sure).
Please kindly correct me if I'm missed something.
Here's my PtlLovBean
#ManagedBean(name = "ptlLovBean")
#SessionScoped
public class PtlLovBean implements Serializable {
private static final String FLIGHT = "LOV_FLIGHT";
private List lovFlight;
public List getLovFlight() {
PtlLovDao ptlLovDao = new PtlLovDaoImpl();
return ptlLovDao.getByKey(FLIGHT);
}
}
Here's ptlLovDao
public interface PtlLovDao {
public List getByKey(String key);
}
Here's PtlLovDaoImpl
public class PtlLovDaoImpl implements PtlLovDao {
#Override
public List getByKey(String key) {
Session session = HibernateUtil.getSessionFactory().openSession();
Query query = session.createQuery("from PtlLov where LOV_KEY = :param");
query.setParameter("param", key);
return query.list();
}
}
Here's my JSF :
<p:selectOneMenu id="flightName" value="#{wizard.user.selectedFlightName}">
<f:selectItem itemLabel="Select Flight" itemValue="" />
<f:selectItems value="#{ptlLovBean.lovFlight}" />
</p:selectOneMenu>
Display after Code:
Sorry I'm not capable to insert picture, so here's the image link :
http://i117.photobucket.com/albums/o56/po_se_for/PIC_zps88ec4983.png
You can either override the toString method in your PtlLov class or define itemValue and itemLabel properties in your f:selectItems tag.
Something like this:
<f:selectItems value="#{ptlLovBean.lovFlight}" var="flight"
itemValue="#{flight}" itemLabel="#{flight.description}" />

<p:selectOneMenu listener method returns null when a value is selected

I am using JSF 2.0 with Primefaces 3.4.2
I have the following in JSF page
<p:selectOneMenu value="#{mb.employee}">
<f:selectItems value="#{mb.employeeList}" var="emp"
itemLabel="#{emp.employeeName}" itemValue="#{emp.employeeCode}"/>
<p:ajax listener="#{mb.changeMethod}" />
</p:selectOneMenu>
Problem is when I select a value in selectOneMenu, I am getting null in changeMethod of ManagedBean, for this System.out.println("val "+employee.getEmployeeName());
What could be the reason for this? How can I resolve this problem?
Any hep is highly appreciable.
ManagedBean Code
#Named("mb")
#ViewAccessScoped
public class MyBean implements Serializable {
private Employee employee;
private List<Employee> employeeList;
#Inject
EmployeeService employeeService;
#PostConstruct
public void loadEmployees() {
employeeList = employeeService.getEmployees();
}
public void changeMethod() {
System.out.println("val "+employee.getEmployeeName());
}
/* getters and setters for employee and employeeList */
.... methods
/* */
You are not specifying on the <p:ajax> component which form elements to process, so the ajax request may be submitting multiple values that could be conflicting with other field validation. Remmeber that if a value is submitted and it fails validation, then none of the request values get set to the model. When you reach the Application(Event) phase, the model values will not reflect any submitted request values.
Try this:
<p:selectOneMenu value="#{mb.employee}">
<f:selectItems value="#{mb.employeeList}" var="emp"
itemLabel="#{emp.employeeName}" itemValue="#{emp.employeeCode}"/>
<p:ajax process="#this" partialSubmit="true" listener="#{mb.changeMethod}" />
</p:selectOneMenu>
Above you will be submitting just the current component request value to be applied to the model.
EDIT:
Actually it probably isn't a validation issue so much as there are no request values being submitted on the ajax event.
According to the Primefaces Manual:
process null String Component(s) to process in partial request.
update null String Component(s) to update with ajax.
The second column is Default Value. In the standard <f:ajax> the execute attribute defaults to #this however this is not the case for <p:ajax>. If you want to submit the current component then you must specify this in the process attribute.
You need to add converter to p:selectOneMenu to let it know that you are dealing with employee object. Create the converter class and use it with converter attribute. Here is the sample converter.
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
#FacesConverter("employeeConverter")
#Configurable
public class EmployeeConverter implements Converter {
#Autowired
EmployeeService employeeService;
public Object getAsObject(FacesContext context, UIComponent component,
String value) {
if (value == null || value.length() == 0) {
return null;
}
Long id = Long.parseLong(value);
return employeeService.findUser(id);
}
public String getAsString(FacesContext context, UIComponent component,
Object value) {
return value instanceof Employee ? ((Employee) value).getId().toString() : "";
}
}

Display values in drill-down SelectOneMenus (Parent-Child)

I have irritating problem when I populate a drill-down selection of two selectOneMenu. The first menu is Sector which controls the other one Categorty. I load the selection for the Category depending on Sector and all of this is working fine.
But when I edit a business object that has a Category (has one) Sector attached to it. The Category doesn't get display when first loading the edit.xhtml page. I know the value is correct in the SessionScoped backing bean. If I select another Sector-Category and then go back to the initial Sector, the Category get properly set at it was persisted in the first place.
The POJO classes (Sector-Category) has the hashCode() and equals(Object object) functions.
I have CRUD generated JSF converters for the POJO object, but I don't think this is causing the problem. I think the second selectOneMenu don't display its value as it should for some reason. If I flip the parent (Sector) to something else and the back to the initial state, the correct value from the manged bean get displayed.
Ho can I make the Category selectOneMenu component display the value in the managed bean?
Greetings Chris
Faces Error Message
FacesMessage(s) have been enqueued, but may not have been displayed.
sourceId=null[severity=(ERROR 2), summary=(No activity selected), detail=(No activity selected)]
Edit.xhtml
...
<h:outputLabel value="Sector:" />
<h:selectOneMenu id="sectorSelector" value="#{activityController.selectedSector}" title="#{bundle.CreateSectorLabel_sectorName}" required="false" requiredMessage="#{bundle.CreateSectorRequiredMessage_sectorName}"
valueChangeListener="#{activityController.changeSectorMenu}"
disabled="#{activityController.activityStatusOngoing or activityController.activityStatusComplete}">
<f:ajax event="change" execute="#this" render="categoryMenu"/>
<f:selectItems value="#{sectorController.itemsAvailableSelectOne}"/>
</h:selectOneMenu>
<h:outputLabel value="Category:" />
<h:selectOneMenu id="categoryMenu" value="#{activityController.selectedCategory}" title="#{bundle.CreateSectorLabel_sectorName}"
required="true" requiredMessage="#{bundle.CreateCategoryRequiredMessage_sector}"
disabled="#{activityController.activityStatusOngoing}" rendered="true">
<f:selectItems value="#{activityController.categorySelection}"/>
</h:selectOneMenu>
...
Controller bean for Category
#ManagedBean(name = "categoryController")
#SessionScoped
public class CategoryController implements Serializable{
....
#FacesConverter(forClass = Category.class)
public static class CategoryControllerConverter implements Converter {
#Override
public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
if (value == null || value.length() == 0) {
return null;
}
CategoryController controller = (CategoryController) facesContext.getApplication().getELResolver().
getValue(facesContext.getELContext(), null, "categoryController");
return controller.ejbFacade.find(getKey(value));
}
java.lang.Integer getKey(String value) {
java.lang.Integer key;
key = Integer.valueOf(value);
return key;
}
String getStringKey(java.lang.Integer value) {
StringBuffer sb = new StringBuffer();
sb.append(value);
return sb.toString();
}
#Override
public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
if (object == null) {
return null;
}
if (object instanceof Category) {
Category o = (Category) object;
return getStringKey(o.getIdCategory());
}
else {
throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + CategoryController.class.getName());
}
}
}
Part of POJO object
...
public class Category implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#Column(name = "idCategory")
private Integer idCategory;
...
Option (not so difficult as I recently made a switch) is to include Seam Faces and Persistence modules. Seam Faces module enables ViewScope without #ManagedBean annotation.
But SeamFaces forces us (which is justified) to use CDI annotations (#Named instead of #ManagedBean etc).
Please refer this post. (You can use #EJB and also Inject any bean annotated with #Named)
It was not the Converters as I first thought. It was the rendering of the two selectOneMenu. This is the changes I did to the xhml file. Thank you for your time and effort, greetings Chris.
Edit.xhml
<h:outputLabel value="Sector:" />
<h:selectOneMenu id="sectorSelector" value="#{activityController.selectedSector}" title="#{bundle.CreateSectorLabel_sectorName}" required="false" requiredMessage="#{bundle.CreateSectorRequiredMessage_sectorName}"
valueChangeListener="#{activityController.changeSectorMenu}" immediate="true"
disabled="#{activityController.activityStatusOngoing or activityController.activityStatusComplete}">
<a4j:ajax event="change" execute="#this categoryMenu" render="categoryMenu"/>
<f:selectItems value="#{sectorController.itemsAvailableSelectOne}"/>
</h:selectOneMenu>
<h:outputLabel value="Category:" />
<h:selectOneMenu id="categoryMenu" value="#{activityController.selectedCategory}" title="#{bundle.CreateSectorLabel_sectorName}"
binding="#{activityController.categoryMenu}"
required="true" requiredMessage="#{bundle.CreateCategoryRequiredMessage_sector}"
disabled="#{activityController.activityStatusOngoing}">
<f:selectItems value="#{activityController.categorySelection}"/>
</h:selectOneMenu>

JSF/SEAM: How to pre-select checkboxes in a form

I have a form where i have to preselect some checkboxes. How is that possible with jsf/seam? In plain html you would just add the "checked" (or checked="checked") as attribute to the checkboxes. But with f:selectItems I have no clue...also the Object "SelectItem" does not provide any setter for this...
You need to preset them in the property behind the component's value attribute as you usually do for every UIInput component. You can do that in the bean's constructor or initialization block.
Here's a basic example:
<h:selectManyCheckbox value="#{bean.selectedItems}">
<f:selectItems value="#{bean.selectItems}" />
</h:selectManyCheckbox>
Bean:
private List<String> selectedItems; // +getter +setter.
private List<SelectItem> selectItems; // +getter.
public Bean() {
// Preset the selected items.
this.selectedItems = new ArrayList<String>();
this.selectedItems.add("valueToBePreselected1");
this.selectedItems.add("valueToBePreselected2");
// Those values should be exactly the same as one of the SelectItem values.
// I.e. the Object#equals() must return true for any of them.
}
Populate the property you use in "value" before rendereing the page (for example using a phase listener)
<h:selectManyCheckbox value="#{selectManyCheckBoxBean.selectedItems}">
<f:selectItem itemLabel="India" itemValue="India" />
<f:selectItem itemLabel="China" itemValue="China" />
<f:selectItem itemLabel="Germany" itemValue="Germany" />
<f:selectItem itemLabel="USA" itemValue="USA" />
</h:selectManyCheckbox>

Categories

Resources