当我尝试覆盖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配置? 在此先感谢!!!
答案 0 :(得分:0)
类别控制器有些不同,RequestMappingHandlerMapping
不能解决(这就是解决您的@RequestMapping
的问题)。相反,正如您提到的,调用handleRequest()
的{{1}}方法的唯一方法是通过CategoryController
。 那是处理类别URL的那一部分。
您似乎只是在试图找出类别页面上的当前CategoryHandlerMapping
。 Spring Mobile只需使用Device
就可以做到这一点。所以:
DeviceUtils.getCurrentDevice(request)
中删除@RequestMapping
像这样重写您的方法:
handleRequest()