0

I have a jsf form in my application which should display several input fields for creating / editing employees. There exists several departments which I want to display with a <h:selectOneMenu>. My other components are basically primefaces components.

If I select an existing employee from my datatable, the department is set correctly (in frontend and bean). I checked the converter and it's getAsString() method in debugging mode. Both seem to work fine.

But when I refresh the page with [F5] it changes the value in the <h:selectOneMenu> back to it's previous one but the bean's value is still fine. It seems that the binding between my session bean and the jsf component isn't working properly.

In addition, there's another problem when I want to save any employee. The validation shows me that the selectOneMenu value is invalid. This error occurs after the getAsObject() method successfully returns my department pojo. I didn't define any validators on this specific component but I use several input components who must not be empty. This error might occur because there's a problem with setting the value as I stated above.

DepartmentConverter.java

@ManagedBean(name="departmentConverter")
public class DepartmentConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        DepartmentDAOImpl deptUtils = new DepartmentDAOImpl();

        try {

            int code = Integer.parseInt(value);
            return deptUtils.getDepartmentById(code);

        } catch (NumberFormatException nfe) {
            System.out.println("Couldn't transform department code from string to int");
            nfe.printStackTrace();
        }

        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return ((Department) value).getCode() + "";
    }

}

SampleJSFBean.java

@ManagedBean(name = "sampleBean")
@SessionScoped
public class SampleJSFBean {

    private Task employee;
    private List<Task> employees;
    private List<Department> departments;
    private Department department;

    public String showEditEmployeePanel() {
        // shows panel
    }

    public String updateEmployee() {
        // saves employee
    }

    // getter and setter

}

registration.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<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="/templates/BasicTemplate.xhtml">

<ui:define name="content">

    <h:form id="formResult">
        <p:panel>
            <p:dataTable id="employee" var="task" value="#{sampleBean.employees}">
                <p:column headerText="Code">
                    <h:outputText value="#{task.employee.code}" />
                </p:column>
                <p:column headerText="Name">
                    <h:outputText value="#{task.employee.lastname}" />
                </p:column>
                <p:column headerText="Department">
                    <h:outputText value="#{task.employee.department.name}" />
                </p:column>
                <p:column headerText="edit">
                    <p:commandButton id="btnMut" icon="ui-icon-pencil" title="edit employee" disabled="#{task.jobActive == true}" action="#{sampleBean.showEditEmployeePanel}"
                        update=":formEmployee" resetValues="true">
                        <f:setPropertyActionListener value="#{task}" target="#{sampleBean.task}" />
                    </p:commandButton>
                </p:column>
            </p:dataTable>
        </p:panel>
    </h:form>

    <h:form id="formEmployee">
        <p:growl autoUpdate="false" />
        <p:panel id="panelEmployee">
            <h:outputText value="Lastname" styleClass="labelForm" />
            <p:inputText id="inputLastname" value="#{sampleBean.task.employee.lastname}" required="true" requiredMessage="Please enter lastname." />
            <br />
            <h:outputText value="Department" styleClass="labelForm" />
            <h:selectOneMenu value="#{sampleBean.department}" converter="#{departmentConverter}">
                <f:selectItems value="#{sampleBean.departments}" var="dept" itemLabel="#{dept.code} - #{dept.name}" />
            </h:selectOneMenu>
            <br />
            <p:commandButton value="save" action="#{sampleBean.updateEmployee}" icon="ui-icon-disk" update=":formResult :formEmployee" />
        </p:panel>
    </h:form>

</ui:define>

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
yves.beutler
  • 972
  • 1
  • 14
  • 32
  • 1
    So, you got this exact error? http://stackoverflow.com/questions/9069379/validation-error-value-is-not-valid In other words, the `equals()` method of your `Department` object is missing or broken? – BalusC Dec 22 '15 at 10:20
  • Thanks to BalusC who saved my day once again! The `equals()` method was missing and so I was unable to save. There's still an error because the component always shows the old value if I refresh the page, but that's a minor bug.. – yves.beutler Dec 22 '15 at 11:01

1 Answers1

-1

Have you tried with primefaces component instead <h:selectOneMenu> use <p:selectOneMenu>. I'm not sure it helps, but I always use primeFaces's selectOneMenu and always works.

Victor1125
  • 642
  • 5
  • 16