JSF <f:event type="preRenderView" ...> works, but proper CSS isn't loaded

Jochen Schmitz picture Jochen Schmitz · Jun 19, 2012 · Viewed 9k times · Source

I am trying to check authentication on each view with the

<f:event type="preRenderView" listener="{#loginControl.checkAuthentication}" /> 

tag.

The mechanism works, but the look'n'feel is broken, it seems that there's some CSS missing. When I remove the check, my page is displayed as it should.

This is one of my views, in which is check the authentication:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<f:event listener="#{loginControl.checkAuthorization}" type="preRenderView" />
<h:head>
...

the following method is called:

public void checkAuthorization(ComponentSystemEvent evt){
  FacesContext ctx = FacesContext.getCurrentInstance();
  ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler)      
  ctx.getApplication().getNavigationHandler();

  // navigate to login-screen
  if(this.user==null){          
   nav.performNavigation("login");
  } else {
   nav.performNavigation("welcome");
  }
}

The output is this:

http://www.convince-it.de/Auswahl_002.jpeg

http://www.convince-it.de/Auswahl_003.jpeg

As you can see the components are not rendered as they should when I check the authorization. The first picture is the screenshot of the view generated with authorization check enabled. The second view is the rendered view without authorization check, but with the wanted look.

Any ideas?

Answer

BalusC picture BalusC · Jun 19, 2012

Apparently the necessary PrimeFaces CSS/JS couldn't be added/changed when the view has been changed by the navigation handler during prerender.

I suggest to send a redirect instead of a forward.

if (user == null) {          
    nav.performNavigation("login?faces-redirect=true");
} else {
    nav.performNavigation("welcome?faces-redirect=true");
}