如何覆盖BroadleafCategoryController [Broadleaf Commerce]

时间:2018-06-25 20:41:42

标签: spring-boot e-commerce broadleaf-commerce

当我尝试覆盖BroadleafCategoryController handleRequest方法时,一切对我来说都很好。但是当我尝试使用Device参数调用如下所示的相同方法来标识用户设备

在我的Java类CategoryController.java

@Controller("blCategoryController")
        public class CategoryController extends BroadleafCategoryController {

            private static final Logger logger = LoggerFactory.getLogger(CategoryController.class);

            @RequestMapping("/")
            public ModelAndView handleRequest(Device currentDevice, HttpServletRequest request, HttpServletResponse response) throws Exception {

        return super.handleRequest(request, response);

            }

        }

然后,当它进入super方法(handleRequest)时,我得到Category Object值为NULL。

Category category = (Category) request.getAttribute(CategoryHandlerMapping.CURRENT_CATEGORY_ATTRIBUTE_NAME);
The above value should have been set through CategoryHandlerMapping

        public class CategoryHandlerMapping extends BLCAbstractHandlerMapping {
                    public static final String CURRENT_CATEGORY_ATTRIBUTE_NAME = "category";

            protected String defaultTemplateName = "catalog/category";

            private String controllerName = "blCategoryController";


            @Override
            protected Object getHandlerInternal(HttpServletRequest request) throws Exception {
                Category category = null;
                if (allowCategoryResolutionUsingIdParam()) {
                    category = findCategoryUsingIdParam(request);
                }

                if (category == null) {
                    category = findCategoryUsingUrl(request);
                }

                if (category != null) {
                    request.setAttribute(CURRENT_CATEGORY_ATTRIBUTE_NAME, category);
                    return controllerName;
                }

                return null;
            }

CategoryHandlerMapping(由Broadleaf Commerce提供)已在带有@Configuration批注的类中配置,并且在带有@Bean批注的方法中创建了对象。

@Bean
public HandlerMapping categoryHandlerMapping() {
    CategoryHandlerMapping mapping = new CategoryHandlerMapping();
    mapping.setOrder(5);
    return mapping;
}

注意:-与Device(Spring-Mobile)相关的所有配置都可以 请让我知道我是否缺少像applicationContext.xml这样的xml配置?     在此先感谢!!!

1 个答案:

答案 0 :(得分:0)

类别控制器有些不同,RequestMappingHandlerMapping不能解决(这就是解决您的@RequestMapping的问题)。相反,正如您提到的,调用handleRequest()的{​​{1}}方法的唯一方法是通过CategoryController是处理类别URL的那一部分。

您似乎只是在试图找出类别页面上的当前CategoryHandlerMapping。 Spring Mobile只需使用Device就可以做到这一点。所以:

  1. DeviceUtils.getCurrentDevice(request)中删除@RequestMapping
  2. 像这样重写您的方法:

    handleRequest()