gpt4 book ai didi

java - 似乎无法从 ViewScoped 构造函数重定向

转载 作者:行者123 更新时间:2023-12-01 05:55:05 25 4
gpt4 key购买 nike

如果我们没有相关页面所需的信息,我在从 View 作用域 bean 进行重定向时遇到问题。 @PostContruct 中的日志条目在与尝试呈现自身而不是遵循我的重定向的 View 相关的 NPE 之前的日志中可见。为什么它忽略重定向?

这是我的代码:

@ManagedBean
public class WelcomeView {

private String sParam;
private String aParam;

public WelcomeView() {
super();

sParam = getURL_Param("surveyName");
aParam = getURL_Param("accountName");

project = fetchProject(sParam, aParam);

}

@PostConstruct
public void redirectWithoutProject() {
if (null == project) {
try {
logger.warn("NO project [" + sParam + "] for account [" + aParam + "]");
FacesContext fc = FacesContext.getCurrentInstance();
fc.getExternalContext().redirect("/errors/noSurvey.jsf");
return;
} catch (Exception e) {
e.printStackTrace();
}
}
}

....

public boolean getAuthenticated() {
if (project.getPasswordProtected()) {
return enteredPassword.equals(project.getLoginPassword());
} else return true;
}

}

这是堆栈跟踪:

SEVERE: Error Rendering View[/participant/welcome.xhtml]
javax.el.ELException: /templates/participant/welcome.xhtml @80,70 rendered="#{welcomeView.authenticated}": Error reading 'authenticated' on type com.MYCODE.general.controllers.participant.WelcomeView
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:107)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:190)
at javax.faces.component.UIComponentBase.isRendered(UIComponentBase.java:416)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1607)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1616)
at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:848)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1613)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1616)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1616)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:380)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:126)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:127)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:313)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.MYCODE.general.filters.StatsFilter.doFilter(StatsFilter.java:28)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:433)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:568)
at org.apache.catalina.authenticator.SingleSignOn.invoke(SingleSignOn.java:421)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:637)
Caused by: java.lang.NullPointerException
at com.MYCODE.general.controllers.participant.WelcomeView$$M$863c205f.getAuthenticated(WelcomeView.java:127)
at com.MYCODE.general.controllers.participant.WelcomeView$$A$863c205f.getAuthenticated(<generated>)
at com.MYCODE.general.controllers.participant.WelcomeView.getAuthenticated(WelcomeView.java:125)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.el.BeanELResolver.getValue(BeanELResolver.java:62)
at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:53)
at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72)
at org.apache.el.parser.AstValue.getValue(AstValue.java:118)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:102)
... 33 more

最佳答案

解决了!问题是我没有意识到无论我的重定向如何,页面都会被完全编译,因此空项目实际上是一个问题。对 getAuthenticated 的调用(实际上应该是 isAuthenticated,但这不会造成任何真正的麻烦)失败,因为项目为 null(如预期),我的逻辑是,如果重定向发生在构造函数中,则不应调用此方法。这是谎言。可以在构造函数中调用重定向,但 bean 和 View 都是在重定向实际发生之前构造的。

关于java - 似乎无法从 ViewScoped 构造函数重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3280192/

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