2014-10-06 10 views
1

내 컨트롤러에는 가능한 답변 옵션이있는 "질문"컬렉션이 있습니다. 내 견해는 ui : repeat를 사용하여 질문을 반복합니다.지도를 사용하여 여러 개의 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 내가 선택한 옵션을 읽을 때, 나는 java.lang.ClassCastException가 얻을 ; @ 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

다소 비슷한 문제가 발생했습니다. 값 목록으로 정수 목록이있는 Map이 있습니다. 너처럼. 그리고 u : repeat 내에서 h : selectManyCheckbox를 사용하여 Map 값을 사용하고있었습니다. 그러나 MapCaseExceptions을 계속 사용하는 이유는 어떤 이유로 든 Map의 값 목록이 양식을 제출했을 때 대신 List of Strings 였기 때문입니다.

내지도에서 List as value 대신 IntegerArray 클래스를 사용하게되었습니다. 그런 다음이 클래스는 (내 경우에는) 정수 배열을 유지했습니다.

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; 
    } 
} 

.