SAP Hybris:如何在自定义错误页面控制器中加载应用上下文?
SAP Hybris: How to load app context in custom error page controller?
我在本地计算机上使用 SAP Hybris 1811。我在 web.xml
中有自定义错误页面处理程序
<error-page>
<exception-type>java.lang.NullPointerException</exception-type>
<location>/errors</location>
</error-page>
还有一个用于处理此错误的控制器(注意它没有扩展 AbstractPageController
,原因请进一步阅读)
@Controller
public class ErrorController {
@RequestMapping(value = "/errors", method = RequestMethod.GET)
public ModelAndView handleErrors(Model model, HttpServletRequest httpRequest) {
httpRequest.getLocale();
.... some code here
}
}
我需要获取正确的当前 locale
应用程序以便在错误页面中显示正确的语言,但它仍然只显示英语,尽管它应该是其他语言。
我尝试加载 i18nService
并且它是这样的区域设置,但它仍然是 "en":
SpringHelper.getSpringBean(httpRequest, "i18nService", DefaultI18NService.class, true).getCurrentLocale()
我认为问题是因为 ErrorController
没有扩展 AbstractPageController
,但是当我尝试这样做时,可以达到 none 的错误方法。
最后我能够像这样获得正确的语言环境:
Locale loc = ((Locale)((CommerceJaloSession)this.pageContext.getSession().getAttribute("jalosession")).getAttribute("locale"));
我们使用了 Spring MVC 的控制器建议概念,您应该可以在其中接收所有信息。同样,通过这种方式,错误将在店面本身中被捕获,您将在那里有很好的控制。
@ControllerAdvice(basePackages =
{ "com.custom", "de.hybris.platform", "org.springframework" })
public class GlobalControllerExceptionHandler
{
private static final Logger LOG = Logger.getLogger(GlobalControllerExceptionHandler.class);
private static final String FORWARD_TO_ERROR_PAGE = ControllerConstants.FORWARD_STMT
+ ControllerConstants.ControllerMappings.Error.ErrorController;
@ExceptionHandler(Exception.class)
public String handleException(final Exception exception, final HttpServletRequest request)
{
LOG.error("Exception caught :: " + exception.getMessage(), exception);
request.setAttribute(ControllerConstants.ControllerMappings.Error.ExceptionAttributeName, exception);
return FORWARD_TO_ERROR_PAGE;
}
}
我在本地计算机上使用 SAP Hybris 1811。我在 web.xml
<error-page>
<exception-type>java.lang.NullPointerException</exception-type>
<location>/errors</location>
</error-page>
还有一个用于处理此错误的控制器(注意它没有扩展 AbstractPageController
,原因请进一步阅读)
@Controller
public class ErrorController {
@RequestMapping(value = "/errors", method = RequestMethod.GET)
public ModelAndView handleErrors(Model model, HttpServletRequest httpRequest) {
httpRequest.getLocale();
.... some code here
}
}
我需要获取正确的当前 locale
应用程序以便在错误页面中显示正确的语言,但它仍然只显示英语,尽管它应该是其他语言。
我尝试加载 i18nService
并且它是这样的区域设置,但它仍然是 "en":
SpringHelper.getSpringBean(httpRequest, "i18nService", DefaultI18NService.class, true).getCurrentLocale()
我认为问题是因为 ErrorController
没有扩展 AbstractPageController
,但是当我尝试这样做时,可以达到 none 的错误方法。
最后我能够像这样获得正确的语言环境:
Locale loc = ((Locale)((CommerceJaloSession)this.pageContext.getSession().getAttribute("jalosession")).getAttribute("locale"));
我们使用了 Spring MVC 的控制器建议概念,您应该可以在其中接收所有信息。同样,通过这种方式,错误将在店面本身中被捕获,您将在那里有很好的控制。
@ControllerAdvice(basePackages =
{ "com.custom", "de.hybris.platform", "org.springframework" })
public class GlobalControllerExceptionHandler
{
private static final Logger LOG = Logger.getLogger(GlobalControllerExceptionHandler.class);
private static final String FORWARD_TO_ERROR_PAGE = ControllerConstants.FORWARD_STMT
+ ControllerConstants.ControllerMappings.Error.ErrorController;
@ExceptionHandler(Exception.class)
public String handleException(final Exception exception, final HttpServletRequest request)
{
LOG.error("Exception caught :: " + exception.getMessage(), exception);
request.setAttribute(ControllerConstants.ControllerMappings.Error.ExceptionAttributeName, exception);
return FORWARD_TO_ERROR_PAGE;
}
}