使用Map填充多个SelectManyCheckboxes - ClassCastException

时间:2014-10-06 06:39:48

标签: jsf generics jsf-2 converter selectmanycheckbox

在我的控制器中,有一个"问题"有可能的答案选项。我的观点用ui重复问题:重复。当我尝试读取所选的选项时,我得到一个java.lang.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)print&#34; selectedItems = {0 = [Ljava.lang.String; @ 30185d04,1 = [Ljava.lang.String; @ 661abe68}&#34;

(2)print&#34; selectedItems [0] = [Ljava.lang.String; @ 30185d04&#34;

查看:

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

我甚至需要转换器吗?我做错了什么?

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题。我有一个带有整数列表的Map作为值。很像你。我在一个ui:repeat中使用了一个h:selectManyCheckbox的Map值。但是我一直得到ClassCastExceptions,因为我提交表单时,由于某种原因Map值是一个字符串列表。

我最终在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;
    }
}

那很有效。