Page 1 of 1

ProductController - Class cast problem?

Posted: Wed Jul 30, 2014 10:02 am
by gowthamgutha
I am getting this error, when I am trying to cast Product in the ProductController.. My ProductController looks similar to this..

Code: Select all

 @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelAndView model = new ModelAndView();
       
        System.out.println("Casting to MyProductImpl");
        MyProductImpl product = (MyProductImpl) request.getAttribute(ProductHandlerMapping.CURRENT_PRODUCT_ATTRIBUTE_NAME);
        assert(product != null);
       
        model.addObject(MODEL_ATTRIBUTE_NAME, product);

        addDeepLink(model, deepLinkService, product);

        if (StringUtils.isNotEmpty(product.getDisplayTemplate())) {
            model.setViewName(product.getDisplayTemplate());   
        } else {
            model.setViewName(getDefaultProductView());
        }
        return model;
    }


I am getting the following error while accessing the product page..

org.broadleafcommerce.core.catalog.domain.ProductImpl cannot be cast to com.mycompany.core.catalog.domain.MyProductImpl

My Question is where is this attribute set and how could I set this? If I could set the MyProductImpl to the request attribute, then it will be ok? But how could I do this?

Thanks in advance. Hope you will reply as soon as possible.

Re: ProductController - Class cast problem?

Posted: Wed Aug 06, 2014 3:00 pm
by RapidTransit
Are you sure it's an extended entity? Make sure products id is also in the extended entities table.

Also I recommend a check

Code: Select all

Product product = request.getAttribute(ProductHandlerMapping.CURRENT_PRODUCT_ATTRIBUTE_NAME);
if(product != null && product.getClass().isAssignableFrom(MyProductImpl.class){
...
}


I always prefer using isAssingableFrom() with potentially enhanced classes and a null check is needed because unlike instanceof which would return false, isAssignableFrom() will throw a null pointer exception.

Re: ProductController - Class cast problem?

Posted: Sat Aug 09, 2014 8:54 am
by gowthamgutha
It is actually not of the type of my own ProductImpl. Could you tell me where is the request attribute set? So that I could change it there?

Thanks in advance.

Re: ProductController - Class cast problem?

Posted: Sat Aug 09, 2014 9:17 am
by RapidTransit
org.broadleafcommerce.core.web.catalog.ProductHandlerMapping