【问题标题】:Getting selection from p:selectOneMenu in PrimeFaces从 PrimeFaces 中的 p:selectOneMenu 获取选择
【发布时间】:2013-09-24 13:59:21
【问题描述】:

我想从 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();
    }

}

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

【问题讨论】:

    标签: jsf primefaces selectonemenu


    【解决方案1】:

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

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

    另一种方法是使用JSF utility library OmniFaces 中的omnifaces.SelectItemsConverter,这样您就无需使用&lt;f:selectItem(s)&gt; 为具有复杂对象作为值的组件创建自定义转换器。

    另一种选择是将selectedTask 更改为String 而不是InfoRowBean(并去掉整个转换器,因为它在这个结构中完全没用)。

    另见:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-19
      • 2013-05-22
      • 2012-12-17
      • 1970-01-01
      • 2014-09-28
      相关资源
      最近更新 更多