Struts2 - Is it possible to access an action backend method in the JSP?

2

I'm using Struts2 to build a web application. I have a method in a class called BaseAction , where all other Actions extend it, as written below:

public boolean isUserFullyLogged() {
    final Boolean isLogado = (Boolean) this.retrieveSessionAttribute(Constantes.LOGADO);
    return (isLogado != null) && isLogado.booleanValue();
}

I want to access this method in my JSP to show or not certain content and have tried the syntax below for this:

  • <s:if test="#userFullyLogged">Conteúdo</s:if>
  • <s:if test="%{#userFullyLogged}">Conteúdo</s:if>
  • <s:if test="userFullyLogged">Conteúdo</s:if>
  • <s:if test="%{userFullyLogged}">Conteúdo</s:if>

But none of them worked and the method just is not called. Does anyone know where I am wrong and what is the correct syntax for calling a method in the backend?

    
asked by anonymous 31.03.2014 / 20:28

1 answer

1

The problem is that you are trying to access an userFullyLogged attribute, which does not exist.

Your call should simply be.

<s:if test="%{isUserFullyLogged()}">Conteúdo</s:if>

You can try a bit better on performance by making the is an attribute rather than a variable, doing as an example below.

private Boolean isLogado;

public boolean isUserFullyLogged() {
    if (this.isLogado == null) {
        this.isLogado = (Boolean) this.retrieveSessionAttribute(Constantes.LOGADO);
    }
    return this.isLogado.booleanValue();
}

In this way you will make a single call to the retrieveSessionAttribute method and also a single cast e this value will be unique for each Action instance that extends BaseAction.

    
25.04.2014 / 20:10