0

I'am trying to pass a profile name for a webpage (xhtml) to the user bean validator. This is my approoach:

<f:event name="verifica" listener="#{RegistroBean.verificaUsuarioLogado}" type="preRenderView">            
        <f:attribute name="perfil" value="profesor"/>
    </f:event>

In the backing bean code, the event attributes are empty, but they shouldn't:

  public void verificaUsuarioLogado(ComponentSystemEvent event) {
        log.debug("URL " + UtilidadesBean.getRequestURL());
        log.debug("Verificando si está logado");
        if (event.getComponent().getAttributes().isEmpty()) {
            log.debug("No hay nada");
        } else {
            Iterator itr = event.getComponent().getAttributes().entrySet().iterator();
            while (itr.hasNext()) {
                Entry entrada = (Entry) itr.next();
                System.out.print("Clave " + (String) entrada.getKey() + " Valor " + (String) entrada.getValue());
            }
            String perfil = (String) event.getComponent().getAttributes().get("perfil");
            log.debug("Tengo el perfil de la página " + perfil);
        }
        String perfil = (String) event.getComponent().getAttributes().get("perfil");
        log.debug("Tengo el perfil de la página " + perfil);

Log output:

14-abr-2013 12:29:20 DEBUG beans.RegistroBean.verificaUsuarioLogado:231 - URL /spum/generainforme.xhtml
14-abr-2013 12:29:20 DEBUG beans.RegistroBean.verificaUsuarioLogado:232 - Verificando si está logado
14-abr-2013 12:29:20 DEBUG beans.RegistroBean.verificaUsuarioLogado:235 - No hay nada
14-abr-2013 12:29:20 DEBUG beans.RegistroBean.verificaUsuarioLogado:246 - Tengo el perfil de la página null

Ideas?

minyatur
  • 5
  • 1
  • 5

1 Answers1

1

The <f:attribute> is only supported in an UIComponent parent. The <f:event> isn't such one.

Either move the <f:attribute> outside the <f:event>.

<f:event name="verifica" listener="#{RegistroBean.verificaUsuarioLogado}" type="preRenderView" />
<f:attribute name="perfil" value="profesor"/>

Or just pass it as method argument in EL, this is new since EL 2.2 which is supported by Tomcat 7.

<f:event name="verifica" listener="#{RegistroBean.verificaUsuarioLogado('profesor')}" type="preRenderView" />

with

public void verificaUsuarioLogado(String perfil) {
    // ...
}

Unrelated to the concrete problem, given the method name you seem to want to perform some authentication checks. A JSF view is the wrong place for this. See also How to handle authentication/authorization with users in a database?

Further, managed bean names must start with lowercase. As you have now is like

RegistroBean RegistroBean = new RegistroBean();

which is not conform standard Java naming conventions. Just omit the name attribute from @ManagedBean, it'll default to the right one already.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Your suggestion solves the problem. About the authentication checks, my authentication works with user/passwords/profiles stored in xml files. I agree with you about it isn't the best place, but it is easy of develop and maintain. I should create a complex servlet filter unsafer... Do you agree? – minyatur Apr 14 '13 at 12:20