gpt4 book ai didi

jsf - 当 OmniPartialViewContext#startDocument 执行透明重定向时 omnifaces 1.10 的错误

转载 作者:行者123 更新时间:2023-11-28 21:58:08 26 4
gpt4 key购买 nike

我正在使用以下堆栈:

  • PrimeFaces 5.3.1
  • 我的脸 2.2.8
  • OpenWebBeans 1.6.2
  • OmniFaces 1.10
  • DeltaSpike 1.5.1
  • Tomcat 8.0.28

AFAI 明白,只有 MyFaces 和 OmniFaces 在那里很重要。

当 session 已过期的客户端执行 ajax 请求时,以及当对页面的访问由 webapp web.xml 中的 控制时,我有一个错误。

在这种情况下,OmniPartialViewContext#startDocument 执行“透明重定向”,以提供更好的解释(参见 https://github.com/omnifaces/omnifaces/blob/master/src/main/java/org/omnifaces/context/OmniPartialViewContext.java#L275)(此功能从版本 1.10 到当前的 1.11 没有变化)。

    @Override
public void startDocument() throws IOException {
wrapped.startDocument();
String loginURL = WebXml.INSTANCE.getFormLoginPage();

if (loginURL != null) {
FacesContext facesContext = FacesContext.getCurrentInstance();
String loginViewId = normalizeViewId(facesContext, loginURL);

if (loginViewId.equals(getViewId(facesContext))) {
String originalURL = getRequestAttribute(facesContext, "javax.servlet.forward.request_uri");

if (originalURL != null) {
redirect(originalURL);
}
}
}
}

这是一个问题,因为在堆栈的较高位置,org.apache.myfaces.context.servlet.PartialViewContextImpl.processPartialRendering 执行第 466 行及以下行:

    {
String currentEncoding = writer.getCharacterEncoding();
writer.writePreamble("<?xml version=\"1.0\" encoding=\""+
(currentEncoding == null ? "UTF-8" : currentEncoding) +"\"?>");
writer.startDocument();

writer.writeAttribute("id", viewRoot.getContainerClientId(_facesContext),"id");

因此,当尝试写入 id 时会引发如下异常,因为当前没有打开任何元素。

16-Nov-2015 16:36:35.980 SEVERE [http-apr-8444-exec-10] org.omnifaces.exceptionhandler.FullAjaxExceptionHandler.logException FullAjaxExceptionHandler: An exception occurred during rendering JSF ajax response. Error page '/error.xhtml' will be shown.
java.lang.IllegalStateException: Must be called before the start element is closed (attribute 'id')
at org.apache.myfaces.shared.renderkit.html.HtmlResponseWriterImpl.writeAttribute(HtmlResponseWriterImpl.java:816)
at javax.faces.context.ResponseWriterWrapper.writeAttribute(ResponseWriterWrapper.java:109)
at org.apache.myfaces.context.PartialResponseWriterImpl.writeAttribute(PartialResponseWriterImpl.java:407)
at javax.faces.context.ResponseWriterWrapper.writeAttribute(ResponseWriterWrapper.java:109)
at javax.faces.context.ResponseWriterWrapper.writeAttribute(ResponseWriterWrapper.java:109)
at org.apache.myfaces.context.servlet.PartialViewContextImpl.processPartialRendering(PartialViewContextImpl.java:473)
at org.apache.myfaces.context.servlet.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:415)
at org.primefaces.context.PrimePartialViewContext.processPartial(PrimePartialViewContext.java:60)
at javax.faces.context.PartialViewContextWrapper.processPartial(PartialViewContextWrapper.java:85)
at javax.faces.component.UIViewRoot.encodeChildren(UIViewRoot.java:516)
at javax.faces.component.UIComponentBase.encodeAll(UIComponentBase.java:541)
at org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.renderView(FaceletViewDeclarationLanguage.java:1891)
at org.apache.myfaces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:313)
at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:58)
at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:58)
at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:58)
at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:58)
at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:58)
at org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:116)
at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:267)
at org.apache.deltaspike.jsf.impl.listener.request.DeltaSpikeLifecycleWrapper.render(DeltaSpikeLifecycleWrapper.java:111)
at javax.faces.lifecycle.LifecycleWrapper.render(LifecycleWrapper.java:31)
at org.apache.deltaspike.jsf.impl.listener.request.JsfClientWindowAwareLifecycleWrapper.render(JsfClientWindowAwareLifecycleWrapper.java:160)
at org.apache.deltaspike.jsf.impl.listener.request.DeltaSpikeLifecycleWrapper.render(DeltaSpikeLifecycleWrapper.java:111)
at javax.faces.lifecycle.LifecycleWrapper.render(LifecycleWrapper.java:31)
at org.apache.deltaspike.jsf.impl.listener.request.JsfClientWindowAwareLifecycleWrapper.render(JsfClientWindowAwareLifecycleWrapper.java:160)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:200)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.logging.log4j.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:71)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:720)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:466)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:391)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:318)
at org.apache.catalina.authenticator.FormAuthenticator.forwardToLoginPage(FormAuthenticator.java:384)
at org.apache.catalina.authenticator.FormAuthenticator.authenticate(FormAuthenticator.java:229)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:577)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.authenticator.SingleSignOn.invoke(SingleSignOn.java:291)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2503)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2492)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

我该如何纠正这个问题(除了放弃 OmniFaces,这真的很酷,而且我真的很想保留它。:-))?

最佳答案

最终,我使用了以下解决方案,这可以看作是一种变通方法,因为我无法为 MyFaces 或 OmniFaces 打补丁...

我在处理链的顶部添加了我自己的 PartialViewContext。这样,当我看到它正在尝试获取登录页面时,我可以对 ajax 请求执行干净的重定向。

要做到这一点,必须:

  • 实现一个 PartialViewContextFactory
  • 实现一个 PartialViewContext
  • 在 faces-config.xml 文件中声明 PartialViewContextFactory

因此,在我的 webapp faces-config.xml 中,我输入:

<factory>
<partial-view-context-factory>fr.senat.context.SenatPartialViewContextFactory</partial-view-context-factory>
</factory>

我的 PartialViewContextFactory 非常简单:

package fr.senat.context;

import javax.faces.context.FacesContext;
import javax.faces.context.PartialViewContext;
import javax.faces.context.PartialViewContextFactory;
import lombok.Getter;

/**
*
* @author lpenet
*/
public class SenatPartialViewContextFactory extends PartialViewContextFactory {
@Getter
private final PartialViewContextFactory wrapped;

public SenatPartialViewContextFactory(PartialViewContextFactory wrapped) {
this.wrapped = wrapped;
}

@Override
public PartialViewContext getPartialViewContext(FacesContext context) {
return new SenatPartialViewContext(wrapped.getPartialViewContext(context));
}
}

并且 PartialViewContext 也非常简单:

package fr.senat.context;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.context.PartialResponseWriter;
import javax.faces.context.PartialViewContext;
import javax.faces.context.PartialViewContextWrapper;
import javax.faces.event.PhaseId;
import javax.servlet.http.HttpServletRequest;
import lombok.Getter;
import org.apache.myfaces.context.servlet.PartialViewContextImpl;
import org.omnifaces.config.WebXml;
import static org.omnifaces.util.FacesLocal.getRequestAttribute;
import static org.omnifaces.util.FacesLocal.getViewId;
import static org.omnifaces.util.FacesLocal.normalizeViewId;

/**
*
* @author lpenet
*/
public class SenatPartialViewContext extends PartialViewContextWrapper {

@Getter
private final PartialViewContext wrapped;

public SenatPartialViewContext(PartialViewContext wrapped) {
this.wrapped = wrapped;
}

private void processPartialRendering() throws IOException
{
FacesContext facesContext = FacesContext.getCurrentInstance();
UIViewRoot viewRoot = facesContext.getViewRoot();

String loginURL = WebXml.INSTANCE.getFormLoginPage();
if (loginURL != null) {
String loginViewId = normalizeViewId(facesContext, loginURL);

if (loginViewId.equals(getViewId(facesContext))) {
String originalURL = getRequestAttribute(facesContext, "javax.servlet.forward.request_uri");

if (originalURL != null) {
PartialResponseWriter writer = facesContext.getPartialViewContext().getPartialResponseWriter();
writer.startDocument();
HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
writer.redirect(request.getContextPath() + loginURL);
writer.endDocument();
return;
}
}
}

wrapped.processPartial(PhaseId.RENDER_RESPONSE);
}

@Override
public void processPartial(PhaseId phaseId)
{
if (phaseId == PhaseId.RENDER_RESPONSE)
{
try {
processPartialRendering();
}
catch (IOException ex)
{
Logger log = Logger.getLogger(PartialViewContextImpl.class.getName());
if (log.isLoggable(Level.SEVERE))
{
log.log(Level.SEVERE, "", ex);
}

}
} else {
wrapped.processPartial(phaseId);
}
}

}

关于jsf - 当 OmniPartialViewContext#startDocument 执行透明重定向时 omnifaces 1.10 的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33786117/

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