从PrimeFaces中的p:selectOneMenu中选择

时间:2013-09-24 13:59:22

标签: jsf primefaces selectonemenu

我想从Primefaces中的p:selectOneMenu组件(下拉列表)中选择一个值。我从Java Bean获取数据。我有以下代码:

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 + "]";
    }

}

转换器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();
    }

}

如果我按下按钮没有任何反应(也没有错误)。如果我从标签中删除参数“value”(即离开),按钮工作正常,但我当然没有得到所选项目。这有什么问题?

1 个答案:

答案 0 :(得分:2)

问题是您的转换器没有将提交的字符串值转换为InfoRowBean方法中的具体getAsObject()实例,而是返回原始提交的String值,因为您已生成在getAsString()方法中。这与selectedTask的类型不匹配,即InfoRowBean

您需要相应地修改转换器,以便getAsString()返回复杂对象的唯一字符串表示形式,通常是数据库标识符的风格(以便它可以在基于文本的格式中进一步使用,例如HTML输出和HTTP请求参数),getAsObject()将该唯一字符串表示完全转换回具体的复杂对象实例,通常通过使用唯一标识符作为键的DB调用。

另一种方法是使用JSF utility library OmniFacesomnifaces.SelectItemsConverter,这样您就不需要使用复杂对象作为值的<f:selectItem(s)>为组件创建自定义转换器。

另一种方法是将selectedTask更改为String而不是InfoRowBean(并删除整个转换器,因为它在此构造中完全无用)。

另见: