首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否有方法将Springs @ExceptionHandler与Joinfaces集成

是否有方法将Springs @ExceptionHandler与Joinfaces集成
EN

Stack Overflow用户
提问于 2022-04-12 15:40:25
回答 1查看 44关注 0票数 0

我想问一下,是否有一种方法可以启用Springs /Primefaces的@ExceptionHandler功能。

目前,我能够处理全局@ControllerAdvice bean,但如果@ExceptionHandler@Controller类中,则不能处理。

对于如何解决这个问题,有什么建议吗?

这是我迄今所写的代码

代码语言:javascript
复制
@Slf4j
public class SpringJsfExceptionHandler extends ExceptionHandlerWrapper {

    public SpringJsfExceptionHandler(ExceptionHandler wrapped) {
        super(wrapped);
    }

    @Override
    public void handle() throws FacesException {
        final Iterator<ExceptionQueuedEvent> queue = getUnhandledExceptionQueuedEvents().iterator();

        while (queue.hasNext()) {
            ExceptionQueuedEvent item = queue.next();
            ExceptionQueuedEventContext exceptionQueuedEventContext = (ExceptionQueuedEventContext) item.getSource();

            try {
                Throwable throwable = exceptionQueuedEventContext.getException();

                FacesContext context = FacesContext.getCurrentInstance();

                handleException(context, (Exception) throwable);

            } finally {
                queue.remove();
            }
        }
    }

    private void handleException(FacesContext context, Exception throwable) {
        WebApplicationContext applicationContext = resolveApplicationContext(context);

        Collection<HandlerExceptionResolver> exceptionResolvers = listExceptionHandlerResolvers(applicationContext);

        for (HandlerExceptionResolver resolver : exceptionResolvers) {
            resolver.resolveException(request(context), response(context), null, throwable);
        }
    }

    private Collection<HandlerExceptionResolver> listExceptionHandlerResolvers(WebApplicationContext context) {
        return context.getBeansOfType(HandlerExceptionResolver.class).values();
    }

    private HttpServletRequest request(FacesContext context) {
        return (HttpServletRequest) context.getExternalContext().getRequest();
    }

    private HttpServletResponse response(FacesContext context) {
        return (HttpServletResponse) context.getExternalContext().getResponse();
    }

    private WebApplicationContext resolveApplicationContext(FacesContext context) {
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        return WebApplicationContextUtils.findWebApplicationContext(request.getServletContext());
    }

}
代码语言:javascript
复制
public class SpringJsfExceptionHandlerFactory extends ExceptionHandlerFactory {

    public SpringJsfExceptionHandlerFactory() {
    }

    public SpringJsfExceptionHandlerFactory(ExceptionHandlerFactory wrapped) {
        super(wrapped);
    }

    @Override
    public ExceptionHandler getExceptionHandler() {
        return new SpringJsfExceptionHandler(getWrapped() != null ? getWrapped().getExceptionHandler() : null);
    }
}

这样做是可行的:

代码语言:javascript
复制
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public void handleCalculationException(CalculationException e) {
        FacesContext.getCurrentInstance().
                addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), e.getMessage()));
    }
}

这样做是行不通的:

代码语言:javascript
复制
@Data
@Controller
@ViewScoped
public class CalculatorController implements Serializable {

    @ExceptionHandler
    public void handleCalculationException(CalculationException e) {
        FacesContext.getCurrentInstance().
                addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), e.getMessage()));
    }
[...]

提前感谢

EN

回答 1

Stack Overflow用户

发布于 2022-05-21 01:02:53

TLDR:否

@ExceptionHandler是Spring的一部分。

Spring和JSF是独立的web框架。

Joinfaces允许您在Spring应用程序中使用JSF,也可以在同一个应用程序中使用Spring。但是,每个请求都将由Spring (即DispatcherServlet)或JSF (即FacesServlet)处理。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71845545

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档