2014-10-06 39 views
1

在我的控制器中,有一組「問題」和可能的答案選項。我的觀點用ui重複提問:重複。當嘗試讀所選的選項,得到了一個java.lang.ClassCastException:使用Map來填充多個SelectManyCheckboxes - ClassCastException

javax.faces.FacesException: #{cbtestController.submit}: java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.util.List 
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118) 
    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.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) 
    at org.omnifaces.facesviews.FacesViewsForwardingFilter.doFilter(FacesViewsForwardingFilter.java:130) 
    at org.omnifaces.filter.HttpFilter.doFilter(HttpFilter.java:77) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316) 
    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:415) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282) 
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459) 
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167) 
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201) 
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175) 
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235) 
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112) 
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77) 
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561) 
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112) 
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117) 
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56) 
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137) 
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565) 
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: javax.faces.el.EvaluationException: java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.util.List 
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101) 
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) 
    ... 41 more 
Caused by: java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.util.List 
    at de.zulu.zuluvoting.controller.cbtestController.submit(cbtestController.java:71) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:483) 
    at com.sun.el.parser.AstValue.invoke(AstValue.java:289) 
    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) 
    ... 42 more 

控制器:

@ManagedBean 
@ViewScoped 
public class cbtestController implements Serializable, Converter { 

    private Map<Long, List<SelectItem>> availableItems; 
    private Map<Long, List<String>> selectedItems; 

    // ... getters and setters 

    public List<Long> getIds() { 
     List<Long> result = new ArrayList<>(availableItems.keySet()); 
     return result; 
    } 

    public List<SelectItem> availableItemsById(Long id) { 
     return availableItems.get(id); 
    } 

    @PostConstruct 
    private void init() { 
     selectedItems = new HashMap<>(); 
     availableItems = new HashMap<>(); 
     availableItems.put(0L, new ArrayList<SelectItem>()); 
     availableItems.get(0L).add(new SelectItem("option0_1", "Option 0.1")); 
     availableItems.get(0L).add(new SelectItem("option0_2", "Option 0.2")); 
     availableItems.put(1L, new ArrayList<SelectItem>()); 
     availableItems.get(1L).add(new SelectItem("option1_1", "Option 1.1")); 
     availableItems.get(1L).add(new SelectItem("option1_2", "Option 1.2")); 
    } 

    public void submit() { 
     System.out.println("selectedItems = " + selectedItems); // (1) 
     for (Long id : selectedItems.keySet()) { 
      System.out.println("selectedItems[" + id + "] = " + selectedItems.get(id)); // (2) 
      for (String selectedItem : selectedItems 
        .get(id)) // ClassCastException here 
      { 
       System.out.println(" > " + selectedItem); 
      } 
     } 
    } 

(1)打印「selectedItems = {0 = [Ljava.lang.String ; @ 30185d04,1 = [Ljava.lang.String; @ 661abe68}」

(2)打印 「selectedItems [0] = [Ljava.lang.String; @ 30185d04」

檢視:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:h="http://xmlns.jcp.org/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core" 
     xmlns:ui="http://xmlns.jcp.org/jsf/facelets"> 
    <head> 
     <title>TODO supply a title</title> 
     <meta name="viewport" content="width=device-width, initial-scale=1.0"/> 
    </head> 
    <body> 

     <h:form> 
      <ui:repeat value="#{cbtestController.ids}" 
         var="id"> 
       <h:selectManyCheckbox value="#{cbtestController.selectedItems[id]}" 
             layout="pageDirection"> 
        <f:selectItems value="#{cbtestController.availableItemsById(id)}" /> 
       </h:selectManyCheckbox> 
      </ui:repeat> 
      <h:commandButton action="#{cbtestController.submit}" 
          value="Submit"/> 
     </h:form> 

    </body> 
</html> 

我一直試圖讓這個工作一段時間,現在它變得非常令人沮喪。我也試着寫一個轉換器,但我仍然得到一個ClassCastException(不知道這是正確的):

@Override 
public Object getAsObject(FacesContext context, UIComponent component, String value) { 
    for (Long id : availableItems.keySet()) { 
     List<SelectItem> items = availableItems.get(id); 
     for (SelectItem item : items) { 
      if (((String)item.getValue()).equals(value)) { 
       return item; 
      } 
     } 
    } 
    return null; 
} 

@Override 
public String getAsString(FacesContext context, UIComponent component, Object value) { 
    return (String)value; 
} 

難道我甚至需要轉換?我究竟做錯了什麼?

回答

0

我遇到過類似的問題。我有一個整數列表作爲值的地圖。很像你。我在ui:repeat中也使用了h:selectManyCheckbox的Map值。但是我不斷收到ClassCastExceptions,因爲當我提交表單時,出於某種原因Map值是一個字符串列表。

我結束了在我的地圖中使用IntegerArray類而不是List作爲值。那個類然後舉行(在我的情況下)整數數組。

IntegerArray:

public class IntegerArray implements Serializable { 

    private Integer[] array; 

    public IntegerArray() { 
    } 

    public IntegerArray(Integer[] array) { 
     this.array = array; 
    } 

    public Integer[] getArray() { 
     return array; 
    } 

    public void setArray(Integer[] array) { 
     this.array = array; 
    } 
} 

這工作。