Stefanos Kargas
Stefanos Kargas

Reputation: 11053

Getting selection from p:selectOneMenu in PrimeFaces

I want to select a value from a p:selectOneMenu component (a dropdownlist) in Primefaces. I get my data from a Java Bean. I have the following 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: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:body>
    <h:form>
        <p:messages id="errorMessages" style="color:red;margin:8px;" />
        <br></br>

        <p:panelGrid columns="2" style="margin-bottom:10px" cellpadding="5">

            <h:outputText value="Tasks: "/>
            <p:selectOneMenu value="#{devTestController.selectedTask}">
                <f:selectItems value="#{devTestController.tasks}" var="task" itemLabel="#{task.label}" itemValue="#{task.value}"/>
                <f:converter converterId="infoRowBeanConverter" />
            </p:selectOneMenu>

        </p:panelGrid>

        <br/>
        <p:commandButton value="Execute Task" update = "errorMessages" action="#{devTestController.executeTask()}"/>


    </h:form>

</h:body>
</html>

Java Bean DevTestController.java:

package mypackage;

import java.util.LinkedList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

@ManagedBean
@RequestScoped
public class DevTestController
{
    private InfoRowBean              selectedTask;
    private static List<InfoRowBean> tasks;

    @PostConstruct
    public void initList()
    {
        if (tasks == null)
        {
            tasks = new LinkedList<>();
            tasks.add(new InfoRowBean("Task 1", "Task 1"));
            tasks.add(new InfoRowBean("Task 2", "Task 2"));
        }
    }

    public InfoRowBean getSelectedTask()
    {
        return selectedTask;
    }

    public void setSelectedTask(InfoRowBean selectedTask)
    {
        this.selectedTask = selectedTask;
    }

    public List<InfoRowBean> getTasks()
    {
        return tasks;
    }

    public void executeTask()
    {
        System.out.println("Executing task " + selectedTask.label);
    }

}

InfoRowBean.java:

package mypackage;

import java.util.List;

public class InfoRowBean
{
    String label = null;
    String value = null;

    public InfoRowBean(String label, String value)
    {
        setLabel(label);
        setValue(value);
    }

    public String getLabel()
    {
        return label;
    }

    public void setLabel(String label)
    {
        this.label = label;
    }

    public String getValue()
    {
        return value;
    }

    public void setValue(String value)
    {
        this.value = value;
    }

    // This must return true for another InfoRowBean object with same label/id.
    public boolean equals(Object other)
    {
        return other instanceof InfoRowBean && (label != null) ? label.equals(((InfoRowBean) other).label) : (other == this);
    }

    // This must return the same hashcode for every InfoRowBean object with the same label.
    public int hashCode()
    {
        return label != null ? this.getClass().hashCode() + label.hashCode() : super.hashCode();
    }

    // Override Object#toString() so that it returns a human readable String representation.
    // It is not required by the Converter or so, it just pleases the reading in the logs.
    public String toString()
    {
        return "InfoRowBean[" + label + "," + value + "]";
    }

}

Converter InfoRowBeanConverter.java:

package mypackage;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

@FacesConverter("infoRowBeanConverter")
public class InfoRowBeanConverter implements Converter
{

    public Object getAsObject(FacesContext context, UIComponent component, String value)
    {
        return value;
    }

    public String getAsString(FacesContext context, UIComponent component, Object value)
    {
        return value.toString();
    }

}

If I press the button nothing happens (no error also). If I remove the parameter "value" from tag (namely leave ), the button works fine, but of course I don't get the selected item. What is the problem here?

Upvotes: 0

Views: 8439

Answers (1)

BalusC
BalusC

Reputation: 1108537

The problem is that your converter isn't converting the submitted string value to a concrete InfoRowBean instance in getAsObject() method, but instead returning the raw submitted String value as you have generated in getAsString() method. This doesn't match the type of selectedTask, which is InfoRowBean.

You need to fix your converter accordingly in such way that getAsString() returns the unique string representation of the complex object, usually in flavor of the database identifier (so that it can be used further in text based formats such as HTML output and HTTP request parameters), and that getAsObject() converts exactly that unique string representation back to the concrete complex object instance, usually via a DB call using the unique identifier as key.

An alternative is to use omnifaces.SelectItemsConverter of the JSF utility library OmniFaces, so that you never need to create custom converters for components using <f:selectItem(s)> with complex objects as values.

Another alternative is to change selectedTask to be String instead of InfoRowBean (and get rid of the whole converter as it is completely useless in this construct).

See also:

Upvotes: 2

Related Questions