JSF selectManyListBox与转换器不工作

时间:2014-01-01 22:36:16

标签: java jsf ejb converter selectmanylistbox

直截了当,我得到了ClassCastException:java.lang.String无法强制转换为com.jsf.domain.Owner我认为转换器存在问题,但我不知道它在哪里。

Html:

<h:selectManyListbox value="#{myBean.target}" converter="#{myConverter}">
    <f:selectItems value="#{myBean.source}" var="c" itemValue="#{c}"    itemLabel="#{c.name} #{c.surname}"/>
</h:selectManyListbox>
<h:commandButton value="Ok" action="#{myBean.add()}"/>

Bean只剩下最重要的部分:

@SessionScoped
@Named("myBean")
public class MyFormBean implements Serializable {

    //Car has manytomany relationship with the owner
    private Car car = new Car();    
    private List<Owner> target; 

    @Inject
    OwnerManager om;    
    @Inject
    CarManager mm;

    public String add(){
        mm.addOwners(car.getId(), target);  
        retur null;
    }

    public List<Owner> getSource(){
        om.getAllOwners();
    }   

    public List<Owner> getTarget(){
        return target;  
    }

    public void setTarget(List<Owner> target){
        this.target = target;
    }
}

这是我的转换器:

@FacesConverter("myConverter")
public class myConverter implements Converter {

    @Inject
    CarManger mm;

    @Override Object getAsObject(FacesContext context, UIComponent component, String value){
        if(value == null){
            return null;
        }
        return mm.getEntityManager.find(Owner.class, LongValueOf(value));
    }

    @Override String getAsString(FacesContext context, UIComponent component, Object value){
        if(value == null){
            return null;
        }
        if(value instanceof Car){
            return String.valueOf(((Owner) value).getId());
        }
        return null;
    }
}

另外一个问题,希望我不会被告知为它单独发一个帖子。我是否正确理解当源是一个复杂的对象时,首先在检索它时调用getAsString方法,并在将它返回给源时调用getAsObject?我感谢所有的帮助。

编辑:

    [2014-01-01T22:03:34.266+0100] [glassfish 4.0] [WARNING] [] [javax.enterprise.web] [tid: _ThreadID=18 _ThreadName=http-listener-1(1)] [timeMillis: 1388610214266] [levelValue: 900] [[
      StandardWrapperValve[FacesServlet]: Servlet.service() for servlet FacesServlet threw exception
    java.lang.ClassCastException: java.lang.String cannot be cast to com.jsf.domain.Owner
        at com.jsf.domain.CarManager.addOwnerToCar(CarManager.java:33)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081)
        at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153)
        at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4695)
        at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:630)
        at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
        at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582)
        at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:46)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
        at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
        at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582)
        at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163)
        at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:140)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
        at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
        at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369)
        at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4667)
        at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4655)
        at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212)
        at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
        at com.sun.proxy.$Proxy386.addOwnerToCar(Unknown Source)
        at com.jsf.service.__EJB31_Generated__CarManager__Intf____Bean__.addOwnerToCar(Unknown Source)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.jboss.weld.util.reflection.Reflections.invokeAndUnwrap(Reflections.java:396)
        at org.jboss.weld.bean.proxy.EnterpriseBeanProxyMethodHandler.invoke(EnterpriseBeanProxyMethodHandler.java:108)
        at org.jboss.weld.bean.proxy.EnterpriseTargetBeanInstance.invoke(EnterpriseTargetBeanInstance.java:56)
        at org.jboss.weld.bean.proxy.InjectionPointPropagatingEnterpriseTargetBeanInstance.invoke(InjectionPointPropagatingEnterpriseTargetBeanInstance.java:63)
        at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:101)
        at com.jsf.service.CarManager$Proxy$_$$_Weld$EnterpriseProxy$.addOwnerToCar(Unknown Source)
        at com.jsf.web.MyBean.addCar(MyBean.java:61)
        at com.jsf.web.MyBean$Proxy$_$$_WeldClientProxy.addCar(Unknown Source)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at javax.el.ELUtil.invokeMethod(ELUtil.java:326)
        at javax.el.BeanELResolver.invoke(BeanELResolver.java:536)
        at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:256)
        at com.sun.el.parser.AstValue.invoke(AstValue.java:269)
        at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
        at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
        at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
        at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
        at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
        at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
        at javax.faces.component.UICommand.broadcast(UICommand.java:315)
        at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
        at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
        at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
        at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
        at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
        at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
        at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
        at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
        at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
        at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
        at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
        at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
        at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
        at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
        at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
        at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
        at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
        at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
        at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
        at java.lang.Thread.run(Thread.java:724)
    ]]

addOwners方法

  @PersistenceContext
  EntityManager em;

  public void addOwners(Long carId, List<Owner> owners){
      Car car = em.find(Car.class, carId);
      for(Owner o : owners){
          car.getOwners().add(o);
      }
  }

1 个答案:

答案 0 :(得分:0)

第一个问题是Converter不能从其getAsString方法返回null。来自the javadoc

  

返回:零长度字符串,如果值为null,否则为转换结果

如果你违反了该合同并返回null,那么事情就会破裂。 (说真的,我看过它在Mojarra引起了一些非常奇怪的行为。)

第二个问题是converter="#{myConverter}"是一个表达式,它需要一个名为myConverter的范围变量,但是没有这样的变量。 @FacesConverter("myConverter")注释不会将变量放入任何范围,因此它不会以任何方式使#{myConverter}成为有效的表达式。相反,使用注释注册转换器ID 下的JSF应用程序中使用addConverter(String, String) method of javax.faces.application.Application作为注释的value参数的转换器类。

删除converter=属性,并在&lt; h:selectManyListBox&gt;内添加子元素。像这样:

<f:converter converterId="myConverter"/>