gpt4 book ai didi

java - 如何处理与 url 中的 cid 过期的对话?

转载 作者:行者123 更新时间:2023-12-01 14:18:02 27 4
gpt4 key购买 nike

我使用 ConversatonScoped bean 作为我的 JSF 支持 bean。有时,url 会附加 ?cid=XX。然而,当 session 作用域 bean 过期时,这通常会导致问题。它在浏览器中显示 500 错误,在服务器日志中显示 org.jboss.weld.context.ContextNotActiveException。我已将以下内容放入我的 web.xml

<error-page>
<exception-type>org.jboss.weld.context.ContextNotActiveException</exception-type>
<location>/faces/index.xhtml</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/faces/index.xhtml</location>
</error-page>

没成功。当 url 的 cid 错误时,会显示 500 错误。我该如何修复它?

最佳答案

您可以创建自己的“ViewExpiredExceptionHandler”并重定向到所需的页面。

这样的事情应该有效:

import java.util.Iterator;
import org.jboss.weld.context.NonexistentConversationException;

import javax.faces.FacesException;
import javax.faces.application.ViewExpiredException;
import javax.faces.context.ExceptionHandler;
import javax.faces.context.ExceptionHandlerWrapper;
import javax.faces.context.FacesContext;
import javax.faces.event.ExceptionQueuedEvent;
import javax.faces.event.ExceptionQueuedEventContext;

public class ViewExpiredExceptionHandler
extends ExceptionHandlerWrapper {

private ExceptionHandler wrapped;

public ViewExpiredExceptionHandler(ExceptionHandler wrapped) {
this.wrapped = wrapped;
}

@Override
public ExceptionHandler getWrapped() {
return this.wrapped;
}

@Override
public void handle()
throws FacesException {

for (Iterator<ExceptionQueuedEvent> i =
getUnhandledExceptionQueuedEvents().iterator();
i.hasNext();) {
ExceptionQueuedEvent event = i.next();
ExceptionQueuedEventContext context =
(ExceptionQueuedEventContext) event.getSource();
Throwable t = context.getException();

if (t instanceof ViewExpiredException
|| t instanceof NonexistentConversationException) {

FacesContext fc = FacesContext.getCurrentInstance();
try {

if (!fc.getExternalContext().isResponseCommitted()) {
fc.getExternalContext().redirect("index.xhtml");
}
fc.renderResponse();
break;
} catch (Exception ex) {
throw new FacesException(ex);
} finally {
i.remove();
}
}
}
getWrapped().handle();
}
}

像这样创建一个 ViewExpiredExceptionHandlerFactory:

import javax.faces.context.ExceptionHandler;
import javax.faces.context.ExceptionHandlerFactory;

public class ViewExpiredExceptionHandlerFactory
extends ExceptionHandlerFactory {

private ExceptionHandlerFactory parent;

public ViewExpiredExceptionHandlerFactory(
ExceptionHandlerFactory parent) {
this.parent = parent;
}

@Override public ExceptionHandler getExceptionHandler() {

ExceptionHandler result = parent.getExceptionHandler();
result = new ViewExpiredExceptionHandler(result);
return result;
}
}

将此添加到您的faces-config.xml中:

<factory>
<exception-handler-factory>
com.yourcode.ViewExpiredExceptionHandlerFactory
</exception-handler-factory>
</factory>

关于java - 如何处理与 url 中的 cid 过期的对话?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17932108/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com