Daniel Becker
Daniel Becker

Reputation: 83

Why do I get an unexpected TypeConversion error

I have a site in Primefaces 10. There is a subsite fur listing the results. I want to have a selectManyCheckbox filled with a list of enums, which results in an array of those enums. On one subsite it works and on another I do almost the same thing (other enum) and I get the conversion error when I send the form. My xhtml looks like this:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui"
    template="../layout.xhtml">

    <ui:define name="header">
        <div id="header">
            Dashboard
        </div>
    </ui:define>

    <ui:define name="main">
        <style type="text/css">
            .ui-chart {
                margin-bottom: 50px;
            }
        </style>
        <h:form id="dashboardForm">
            <p:messages id="dashboardErrMsg" globalOnly="true"/>

            <p:selectManyCheckbox id="abfSelector" value="#{dashboardHandler.selectedAbfragen}" layout="grid" columns="3" styleClass="grid-checkbox">
                <f:selectItems value="#{dashboardHandler.abfragen}" var="abfrage" itemLabel="#{abfrage.name}" itemValue="#{abfrage}"/>
            </p:selectManyCheckbox>
            
            <p:commandButton id="showButton" value="Anzeigen" icon="pi pi-search" action="#{dashboardHandler.anzeigen}"/>
    
            <div class="card">
                <p:lineChart model="#{dashboardHandler.lineModel}" style="width: 100%; height: 500px;"/>
            </div>
    
        </h:form>
    </ui:define>
</ui:composition>

and the bean looks like this:

@Named("dashboardHandler")
@ViewScoped
public class DashboardHandler implements Serializable {

    private List<Abfrage> abfragen = new ArrayList<>();
    private Abfrage[] selectedAbfragen;
    
    private LineChartModel lineModel;

    @PostConstruct
    private void init() {
        abfragen = UcmdbAbfTableHandler.getAbfragenBySystem(ReferenceSystem.UCMDB);
        createLineModel();
    }

    public void anzeigen() {
    //do some search
    }

    public List<Abfrage> getAbfragen() { return abfragen; }
    public LineChartModel getLineModel() { return lineModel; }
    public Abfrage[] getSelectedAbfragen() { return selectedAbfragen; }

    public void setAbfragen(List<Abfrage> abfragen) { this.abfragen = abfragen; }
    public void setLineModel(LineChartModel lineModel) { this.lineModel = lineModel; }
    public void setSelectedAbfragen(Abfrage[] selectedAbfragen) { this.selectedAbfragen = selectedAbfragen; }
}

I can't see my error why there is a type conversion error. I've tried changing the Abfrage-Array to a List then I'm getting a list with Strings of the classname of abfrage like when I make an abfrage.toString().

I also don't understand why my messages isn't triggered. I always get the error in my Logfile with unhandeled facesmessage. So I think I'm doing something wrong here too.

Upvotes: 0

Views: 79

Answers (1)

Brooksie
Brooksie

Reputation: 371

When the selectItems are Enums, the issue is answered here: Use enum in h:selectManyCheckbox.

When the selectItems are regular POJOs, you need to include a specific faces converter. There are several answers on StackOverflow for converters, such as this one: How create a custom converter in JSF 2?. Omnifaces, a JSF utility library, includes a SelectItemsConverter that can often be used off-the-shelf, without additional coding.

The messages issue is because the messages tag isn't being ajax updated from the commandButton, or you can use p:autoUpdate on the messages tag (see PF Showcase). Note that messages will not capture system errors like conversion errors as you describe. For that you need to include error pages in the web.xml file (e.g. see WEB.XML error-page in JSF 2.0). You should also capture and display ajax errors. (See PrimeFaces error handling). And ensure that ProjectStages context parameter is set to something other than Production - e.g. Development.

Upvotes: 1

Related Questions