1

I would like to know if it's possible to check with the <s:if> tag of Struts 2 if the user is in session.

If the user is in session, I need it to be shown Logout otherwise Login.

I use the interface SessionAware for login action and this class User.

package it.pwm.wynd.pojo.user;


public class User implements java.io.Serializable {

    private static final long serialVersionUID = 1L;
    private Integer idUser;
    private String name;
    private String username;
    private String password;
    private String email;

    public User() {
    }

    public User(String name, String username, String password, String email) {
        this.name = name;
        this.username = username;
        this.password = password;
        this.email = email;
    }

    public Integer getIdUser() {
        return this.idUser;
    }

    public void setIdUser(Integer idUser) {
        this.idUser = idUser;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }


}
Roman C
  • 49,761
  • 33
  • 66
  • 176
Jdvlp
  • 13
  • 3
  • 1
    Yes, it is possible. Accessing session variables is covered in the [Struts 2 Tags documentation tree](https://struts.apache.org/tag-developers/) in multiple places, but it might be best to take a quick step back and review the [OGNL overview](https://struts.apache.org/tag-developers/ognl). – Dave Newton Nov 12 '21 at 13:42

1 Answers1

0

Struts2 tags cannot work without associated filer. If you use Struts tags in JSP make sure you read first about tags.

The session map can be injected into the action if it is SessionAware.

If you want to put some object like an user to the session you can use authentication interceptor like this.

It checks if an user in session and let the action invoke, otherwise it returns a login result.

The user might want to login several times under different accounts, so you should not restrict him/her from doing it.

In the JSP you could check the user in session with the <s:if> tag using a #session context variable.

<s:if test="#session.user == null">
 <s:a action="login">Login</s:a>
</s:if>
<s:else>
  <s:a action="logout">Logout</s:a>
</s:else>   
Roman C
  • 49,761
  • 33
  • 66
  • 176