How to cast a javax.faces.model.ResultSetDataModel$ResultSetMap to java.util.HashMap

39 Views Asked by At

I am currently developing a web application using JSF. When I try to insert a hashmap from the datatable, it gives me an error, however it works when inserting a object into my function. I then try to cast this object into a hashmap, which gives the error.

Here is my method:

    public void addCart(Object product) {
        System.out.println("This is a test: " + product);
        HashMap<String, String> map = (HashMap<String, String>) product;
        System.out.println("This is a test: " + map.entrySet());
    }

Here is the datatable code:

            <h:form>
                <h:dataTable value="#{storeInfo.info}" var="p" summary="Product list" border="0" cellpadding="2" cellspacing="0" rowClasses="" rules="all" style="border:solid 1px" class="tablef table container">
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="Product name"/>
                        </f:facet>
                        <h:outputText value="#{p.itemName}"/>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="Product Description"/>
                        </f:facet>
                        <h:outputText value="#{p.itemDesc}"/>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="Product Price"/>
                        </f:facet>
                        <h:outputText value="#{p.price}"/>
                    </h:column>
                    <h:column>
                        <f:facet name="header">
                            <h:outputText value="Add To Cart"/>
                        </f:facet>
                        <h:commandButton action="#{storeInfo.addCart(p)}" value="Add Item"/>
                    </h:column>
                </h:dataTable>
            </h:form>

The data being passed into the datatable is in a resultset.

I have tried different paramater types, the only one that has worked is an object, which im not sure how to then use in order to access the key:values.

I expected it to take the information and then print it to console.

The result is similar to the error that is displayed above.

1

There are 1 best solutions below

1
BalusC On BEST ANSWER

Read the Javadoc of javax.faces.model.ResultSetDataModel:

public class ResultDataModel extends DataModel<SortedMap<String,Object>>

So it is a datamodel for SortedMap<String, Object>.

When we drill down into the Javadoc for java.util.SortedMap, then we see this:

All Superinterfaces:
Map<K,V>

It does thus absolutely not extend from HashMap. That would only give you a java.lang.ClassCastException.

Just use SortedMap or Map instead:

Map<String, String> map = (Map<String, String>) product;

Or, better, declare the argument type as-is so that you don't need an additional line to cast:

public void addCart(Map<String, String> product) {
    // ...
}