Can't seem to redirect from a ViewScoped constructor
I'm having trouble redirecting from a view scoped bean in the case that we don't have the required info for the page in question. The log entry in the @PostContruct is visible in the log right before a NPE relating to the view trying to render itself instead of following my redirect. Why is it ignoring the redirect?
Here's my code:
@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;
}
}
Here's the stack trace:
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(ApplicationFi开发者_如何学运维lterChain.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
SOLVED! The problem was that I didn't realise the page would be fully compiled regardless of my redirect, thus the null project was in fact a problem. The call to getAuthenticated (which should actually be isAuthenticated, but this doesn't cause any real trouble) failed as project is null (as expected) and my logic was that this shouldn't be called if the redirect happens in the constructor. This is lies. The redirect may be called in the constructor, but the bean and the view are both constructed before the redirect actually happens.
精彩评论