Marcos
Marcos

Reputation: 1265

JSF h:selectManyCheckbox doesn't work if the 'value' attribute is a Map. JSF changes the Map values to other type

I'm facing one of those mystery problems.

First have a look at the files below (sorry if there are still some names in Portuguese, but I think they don't get in the way):

PaginaDevolucao.java

@ViewScoped
@Named("devolution")
public class PaginaDevolucao implements Serializable
{
    private static final long serialVersionUID = 7041646476610810719L;

    private List<Emprestimo> _loans;
    private Map<Emprestimo, List<ChaveEmprestada>> _selectedKeys = new HashMap<>();

    @PostConstruct
    public void findLoans()
    {
        _loans = ...
    }

    public List<Emprestimo> getLoans()
    {
        return _loans;
    }

    public Map<Emprestimo, List<ChaveEmprestada>> getSelectedKeys()
    {
        return _selectedKeys;
    }

    public void setSelectedKeys(Map<Emprestimo, List<ChaveEmprestada>> selectedKeys)
    {
        _selectedKeys = selectedKeys;
    }

    public void returnKeys(Emprestimo loan)
    {
        List<ChaveEmprestada> keys = _selectedKeys.get(loan);
    }
}

devolution.xhtml (just the relevant parts)

<table jsf:id="loansTable">
    <caption>Loans</caption>
    <thead>
        <tr>
            <th>Person</th>
            <th>Loan date</th>
        </tr>
        <tr>
            <th colspan="2">Keys</th>
        </tr>
    </thead>
    <tbody>
        <ui:repeat id="loans" value="#{devolution.loans}" var="loan">
            <tr>
                <td>
                    #{loan.solicitante.nome}
                </td>
                <td>
                    #{loan.dataHora}
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <h:selectManyCheckbox id="keys" value="#{devolution.selectedKeys[loan]}" layout="pageDirection" converter="conv.ChaveEmprestadaConverter">
                        <f:selectItems value="#{loan.chaves}" var="key" itemLabel="#{key.chave.numero} - #{key.chave.nome}" itemValue="#{key}" />
                        <f:ajax execute="@this" render="btnReturn" />
                    </h:selectManyCheckbox>
                    <p:commandButton id="btnReturn" value="Return" disabled="#{empty devolution.selectedKeys[loan]}" process="@this @previous" update="form:loansTable" action="#{devolution.returnKeys(loan)}" />
                </td>
            </tr>
        </ui:repeat>
    </tbody>
</table>

Now, the problem:

Look at the returnKeys method again:

public void returnKeys(Emprestimo loan)
{
    List<ChaveEmprestada> keys = _selectedKeys.get(loan);
}

When this method is executed I get, for my surprise, a class cast exception:

19:07:14,444 SEVERE [org.primefaces.application.exceptionhandler.PrimeExceptionHandler] (default task-263) class [Ljava.lang.Object; cannot be cast to class java.util.List ([Ljava.lang.Object; and java.util.List are in module java.base of loader 'bootstrap'): java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class java.util.List ([Ljava.lang.Object; and java.util.List are in module java.base of loader 'bootstrap')
    at deployment.chaves.ear.visao.war//br.edu.ifce.chaves.visao.PaginaDevolucao.returnKeys(PaginaDevolucao.java:62)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at [email protected]//javax.el.ELUtil.invokeMethod(ELUtil.java:245)
    at [email protected]//javax.el.BeanELResolver.invoke(BeanELResolver.java:338)
    at [email protected]//javax.el.CompositeELResolver.invoke(CompositeELResolver.java:198)
    at [email protected]//com.sun.el.parser.AstValue.invoke(AstValue.java:257)
    at [email protected]//com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:237)
    at [email protected]//org.jboss.weld.module.web.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
    at [email protected]//org.jboss.weld.module.web.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
    at [email protected]//org.jboss.weld.module.web.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
    at [email protected]//org.jboss.weld.module.web.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
    at [email protected]//com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:65)
    at [email protected]//com.sun.faces.application.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:66)
    at [email protected]//com.sun.faces.application.ActionListenerImpl.getNavigationOutcome(ActionListenerImpl.java:82)
    at [email protected]//com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:71)
    at [email protected]//javax.faces.component.UICommand.broadcast(UICommand.java:222)
    at [email protected]//com.sun.faces.facelets.component.UIRepeat.broadcast(UIRepeat.java:999)
    at [email protected]//javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:847)
    at [email protected]//javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1396)
    at [email protected]//com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:58)
    at [email protected]//com.sun.faces.lifecycle.Phase.doPhase(Phase.java:76)
    at [email protected]//com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:177)
    at [email protected]//javax.faces.webapp.FacesServlet.executeLifecyle(FacesServlet.java:707)
    at [email protected]//javax.faces.webapp.FacesServlet.service(FacesServlet.java:451)
    at [email protected]//io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)
    at [email protected]//io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
    at io.opentracing.contrib.opentracing-jaxrs2//io.opentracing.contrib.jaxrs2.server.SpanFinishingFilter.doFilter(SpanFinishingFilter.java:52)
    at [email protected]//io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
    at [email protected]//io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
    at deployment.chaves.ear.visao.war//br.edu.ifce.chaves.visao.seguranca.AutorizacaoPaginaFilter.doFilter(AutorizacaoPaginaFilter.java:38)
    at deployment.chaves.ear.visao.war//org.omnifaces.filter.HttpFilter.doFilter(HttpFilter.java:108)
    at [email protected]//io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
    at [email protected]//io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
    at deployment.chaves.ear.visao.war//br.edu.ifce.visao.NaoCacheRecursosFilter.doFilter(NaoCacheRecursosFilter.java:38)
    at [email protected]//io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
    at [email protected]//io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
    at [email protected]//io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
    at [email protected]//io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
    at [email protected]//io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:68)
    at [email protected]//io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
    at [email protected]//org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
    at [email protected]//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at [email protected]//io.undertow.servlet.handlers.RedirectDirHandler.handleRequest(RedirectDirHandler.java:68)
    at [email protected]//io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:132)
    at [email protected]//io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
    at [email protected]//io.undertow.server.handlers.DisableCacheHandler.handleRequest(DisableCacheHandler.java:33)
    at [email protected]//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at [email protected]//io.undertow.security.handlers.AuthenticationConstraintHandler.handleRequest(AuthenticationConstraintHandler.java:53)
    at [email protected]//io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
    at [email protected]//io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
    at [email protected]//io.undertow.servlet.handlers.security.ServletSecurityConstraintHandler.handleRequest(ServletSecurityConstraintHandler.java:59)
    at [email protected]//io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
    at [email protected]//io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
    at [email protected]//io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
    at [email protected]//io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
    at [email protected]//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at [email protected]//org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
    at [email protected]//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at [email protected]//org.wildfly.extension.undertow.security.jaspi.JASPICSecureResponseHandler.handleRequest(JASPICSecureResponseHandler.java:48)
    at [email protected]//org.wildfly.extension.undertow.deployment.GlobalRequestControllerHandler.handleRequest(GlobalRequestControllerHandler.java:68)
    at [email protected]//io.undertow.servlet.handlers.SendErrorPageHandler.handleRequest(SendErrorPageHandler.java:52)
    at [email protected]//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at [email protected]//io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:269)
    at [email protected]//io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:78)
    at [email protected]//io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:133)
    at [email protected]//io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:130)
    at [email protected]//io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
    at [email protected]//io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
    at [email protected]//org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create$0(SecurityContextThreadSetupAction.java:105)
    at [email protected]//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1530)
    at [email protected]//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1530)
    at [email protected]//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1530)
    at [email protected]//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1530)
    at [email protected]//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1530)
    at [email protected]//io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:249)
    at [email protected]//io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:78)
    at [email protected]//io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:99)
    at [email protected]//io.undertow.server.Connectors.executeRootHandler(Connectors.java:387)
    at [email protected]//io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:841)
    at [email protected]//org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
    at [email protected]//org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1990)
    at [email protected]//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
    at [email protected]//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
    at [email protected]//org.xnio.XnioWorker$WorkerThreadFactory$1$1.run(XnioWorker.java:1280)
    at java.base/java.lang.Thread.run(Thread.java:834)

So, to be sure about what is happening I change the returnKeys method to this:

public void returnKeys(Emprestimo loan)
{
    Object keys = _selectedKeys.get(loan);
    System.out.println(keys.getClass());
}

And indeed, I don't have a List<ChaveEmprestada> as a Map value anymore. I have an Object class. It prints:

class [Ljava.lang.Object;

That's why, of course, it can't be casted to a List.

JSF just changed Map<Emprestimo, List<ChaveEmprestada>> to Map<Emprestimo, Object>.

So, can you tell me what I'm doing wrong or how I can fix this?

I've already done a lot of research on this, but none solved the problem. They just teach to do the way I am doing:

JSF h:selectManyCheckbox inside ui:repeat via Map entry

ui:repeat h:selectManyCheckbox i can t get values inside a list

Collection of selectManyCheckbox inside a ui:repeat knows which element of the repeater it belongs to

Upvotes: 0

Views: 248

Answers (0)

Related Questions