我正在尝试填充下拉菜单,其中包含一些我从数据库中检索的对象。 所以,在阅读this topic之后,我做到了这一点:
<h:form id="register_form">
<h:panelGrid columns="3">
<h:outputLabel for="client" value="Client:" />
<h:selectOneMenu id="client" value="#{reuniaoc.cliente}" converter="#{clienteConverter}" >
<f:selectItems value="#{reuniaoc.clientes}" var="_cliente" itemValue="#{_cliente}" itemLabel="#{_cliente.id}" />
<f:ajax event="blur" render="m_client" />
</h:selectOneMenu>
<h:message id="m_client" for="client" />
</h:panelGrid>
</h:form>
这是我的controller
:
package control;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import model.Cliente;
import utility.Message;
import bean.ReuniaoBean;
import eao.ClienteEAO;
import eao.ReuniaoEAO;
@ManagedBean(name="reuniaoc")
@RequestScoped
public class ReuniaoController implements Serializable {
private static final long serialVersionUID = 7737795872305798041L;
@EJB ReuniaoEAO rEAO;
@EJB ClienteEAO cEAO;
private ReuniaoBean rb;
private Cliente cliente;
private List<Cliente> clientes;
@PostConstruct
public void init() {
clientes = cEAO.list();
}
private void addMessage(Message message){
FacesMessage fMessage = new FacesMessage(message.getMessage());
FacesContext.getCurrentInstance().addMessage(null, fMessage);
}
public void register(){
Message message = rEAO.create(rb.getReuniao());
addMessage(message);
}
public String getClientes(){
for (Cliente c : clientes)
System.out.println(c.getFantasia());
return "/funcionario/register/reuniao.xhtml?faces-redirect=true";
}
// get's and set's
public ReuniaoBean getRb() {
return rb;
}
public void setRb(ReuniaoBean rb) {
this.rb = rb;
}
public void setClientes(List<Cliente> clientes) {
this.clientes = clientes;
}
public Cliente getCliente() {
return cliente;
}
public void setCliente(Cliente cliente) {
this.cliente = cliente;
}
}
我的转换器:
package bean.converter;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import model.Cliente;
import eao.ClienteEAO;
@ManagedBean
@RequestScoped
public class ClienteConverter implements Converter {
@EJB private ClienteEAO clienteEAO;
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (!(value instanceof Cliente) || ((Cliente) value).getId() == null) {
return null;
}
return String.valueOf(((Cliente) value).getId());
}
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value == null || !value.matches("\\d+")) {
return null;
}
Cliente c = clienteEAO.find(Integer.valueOf(value));
if (c == null) {
throw new ConverterException(new FacesMessage("Unknown operation ID: " + value));
}
return c;
}
}
但是当我尝试加载页面时,给我这个错误:(对不起,这个例外很长)
java.lang.IllegalArgumentException
at com.sun.faces.renderkit.SelectItemsIterator.initializeItems(SelectItemsIterator.java:216)
at com.sun.faces.renderkit.SelectItemsIterator.hasNext(SelectItemsIterator.java:135)
at com.sun.faces.renderkit.html_basic.MenuRenderer.renderOptions(MenuRenderer.java:762)
at com.sun.faces.renderkit.html_basic.MenuRenderer.renderSelect(MenuRenderer.java:844)
at com.sun.faces.renderkit.html_basic.MenuRenderer.encodeEnd(MenuRenderer.java:298)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:312)
at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185)
at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1757)
at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1757)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:662)
对我而言,我的代码似乎非常一致,但这里有一些我不知道的东西。 有什么想法吗?
答案 0 :(得分:1)
public String getClientes(){
for (Cliente c : clientes)
System.out.println(c.getFantasia());
return "/funcionario/register/reuniao.xhtml?faces-redirect=true";
}
此<f:selectItems value>
getter不返回客户端列表。它返回一个导航案例字符串,该字符串未被识别为value
的有效<f:selectItems>
参数。看起来你正在将动作方法与属性getter混合。
相应地修复吸气剂是一个真正的吸气剂:
public List<Cliente> getClientes() {
return clientes;
}
该属性的setter方法是不必要的。它不会以任何方式用于此构造中。