-1

I´m with a strange problem. I´m using the picklist component but it seems like when I use the picklist my commandbutton stop working: Here´s the code:

xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
  xmlns:p="http://primefaces.org/ui">
<h:head>
    <title>PickList Test</title>
</h:head>
<h:body>
            <h:form id="form">
                     <p:pickList value="#{pickListBean.employeeList}" var="employee" itemLabel="#{employee.employeeName}" itemValue="#{employee.employeeCode}" />  
                     <p:commandButton value="Save" action="#{pickListBean.message}" style="margin-left: 12px;"/>
            </h:form>


</h:body>
</html>

bean

@ManagedBean
@RequestScoped
public class PickListBean {
@EJB
private BussinessList bl = new BussinessList();
private DualListModel<Employee> employeeList;

private Employee employee;
/**
 * Creates a new instance of PickListBean
 */
public PickListBean() {
    List<Employee> source = new ArrayList<Employee>();
    List<Employee> target = new ArrayList<Employee>();
    source = bl.getEmployee();
    employeeList = new DualListModel<Employee>(source, target);
}
public void message(){
    System.out.println("CommandButton is working");
}
public DualListModel<Employee> getEmployeeList() {
    return employeeList;
}

public void setEmployeeList(DualListModel<Employee> employeeList) {
    this.employeeList = employeeList;
}
public Employee getEmployee() {
    return employee;
}

public void setEmployee(Employee employee) {
    this.employee= employee;
}
}

When I click in the commandbutton the message method is not called, but when I remove the picklist from my xhtml the commandbutton call the message method.

I´m using jsf 2.2, primefaces 4.0...

Vanilson Lourenço
  • 117
  • 1
  • 2
  • 9

1 Answers1

0

Lots of errors on this code

Before starting with the errors, I can see 2 seriously poor naming choices in the method named getEmployeePickList

  • It's not a PickList in the first place. its a DataList
  • the difference from EmployeeList is that it keeps instances of Employee instead of Functionario. The names you chose, give the impression that one of them is a PickList, while the other is an ArrayList, and they keep instances of the same Class

Now, your xhtml might missing something in the declarations. I tried your code, and even when i deleted the pickList, your commandbutton would not fire. Try starting with this (drop the xml declaration)

<!DOCTYPE html PUBLIC "-W3CDTD XHTML 1.0 TransitionalEN" "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:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">

Also, the Employee Class is not a String, so your pickList will not work without a converter. See this for example

Finally, your PickList will search for a setEmployeePickList() method in your Class. I tried using your pickList working with String instead of Employee, to get past the converter issue, and naturally, I got this error:

javax.el.PropertyNotFoundException: test.xhtml @21,140 value="#{pickListBean.employeePickList}": Property 'employeePickList' not writable on type org.primefaces.model.DualListModel

The solution for this, would be , feeding the pickList with #{pickListBean.employeeList} instead of #{pickListBean.employeePickList}. You can populate your Employee List somewhere else, like in the PickListBean constructor, instead of re-creating in getEmployeePickList.

Community
  • 1
  • 1
yannicuLar
  • 3,083
  • 3
  • 32
  • 50
  • 1
    yannicular, I wrote the code in another language(Portuguese) and translate the code to english, sorry for the mistake...I already edit the code... – Vanilson Lourenço Apr 02 '14 at 09:53
  • ok, then your problem still is that there is no setter method. when you use ' value="#{pickListBean.employeePickList}" ', then the pickList component will look for a method named 'setEmployeePickList( DualListModel list)' – yannicuLar Apr 02 '14 at 10:00
  • yannicular, the picklist is working fine, there´s no problem in the picklist. The problem is in the button, when I click in the button the message method is not called – Vanilson Lourenço Apr 02 '14 at 10:08
  • what if you try – yannicuLar Apr 02 '14 at 10:24
  • Any error messages when you interact with the PickList ? – yannicuLar Apr 02 '14 at 16:54
  • I'm surprised you don't get any errors. maybe you're doing something wrong. Anyway, I tried your code, and updated my answer. There's a lot of thing you need fixing – yannicuLar Apr 03 '14 at 07:54
  • yannicular, I test the picklist example of the primefaces site and it works just fine as you suggest I populate the picklist in the constructor and it works just fine, when I click in the commandbutton the message method is called. So when I try to populate the picklist in the constructor using my code I get the following a exception(See the edition of the code in my first message): om.sun.faces.mgbean.ManagedBeanCreationException: Cant instantiate class: managedBean.PickListBean. – Vanilson Lourenço Apr 03 '14 at 16:21
  • the error you are getting has nothing to do with the initial question. If both picklist and commandButton can now work together, you should close this question, and start a new one – yannicuLar Apr 04 '14 at 06:50