我想从 Primefaces 中的 ap: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 ),按钮工作正常,但当然我没有得到所选项目。这里有什么问题?