This is my fragment:
<ui:fragment rendered="#{}">
<ui:include src="../includes/top.xhtml"/>
</ui:fragment>
My LoginController will redirect to the page home.html that extends MainController that have a boolean method showComponent so when I try to call this mainController.showComponent() I get a nullPointerException because, as I notice, Java loads the html first to see if it is calling any Java Class so when I try to access mainController(that is extended from HomeController, that was not yet called) I get this null pointer
. How to I check inside the fragment if the maisController is set?
Here is what I’ve tried so far
<ui:fragment rendered="#{not empty mainController ? true : false}">
It always returns true.
Here is my MainController class:
package com.erp3.gui.controllers;
import java.io.IOException;
import javax.faces.bean.ManagedBean;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
@ManagedBean
public class MainController {
public LoginController loginController;
public ExternalContext ec;
public void checkUserSession() throws IOException {
ec = FacesContext.getCurrentInstance().getExternalContext();
loginController = (LoginController) ec.getSessionMap().get("loginController");
loginController = (LoginController) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("loginController");
if (loginController == null || !loginController.getIsLoggedIn()) {
ec.redirect(ec.getRequestContextPath() + "/views/login.html");
}
}
public Boolean showComponent() {
return this.loginController.getIsLoggedIn();
}
}
My method showComponent() is returning a session object property from loginController
It throws
NullPointerExceptionbecauseloginControlleris apparentlynull. If it’s another@ManagedBean, then you need to inject it as a manged property. This way you don’t need to manually grab it from the session map. Also, you should give yourMainControllera valid bean scope. Without a scope, a new one will be created on every single EL expression#{mainController}.Then you can use it:
But why don’t you just access
#{loginController}directly?As to your question why
#{not empty mainController ? true : false}always evaluatestrueis because JSF@ManagedBeans are nevernull. If one doesn’t exist in EL scope, JSF will autocreate one.