0

我正在尝试使用从数据库中检索到的一些对象来填充下拉菜单。所以,在阅读了这个主题之后,我做了这个:

<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)

对我来说,我的代码似乎非常一致,但是我在这里缺少一些东西。任何的想法 ?

4

1 回答 1

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 修复为真正的 getter:

public List<Cliente> getClientes() {
    return clientes;
}

顺便说一句,该属性的 setter 方法是不必要的。在此构造中不会以任何方式使用它。

也可以看看:

于 2012-11-20T17:07:30.270 回答