Question

I want to access a session attribute from some actions that implement the SessionAware interface;

I want to perform some test according to the type of the user logged in, the information which it relates are stored in the attributes of the session, I can get this attribute in my JSP page with the following code:

<s:property value="%{#session['attribute']}"/> 

My LoginAction:

public class LoginAction extends ActionSupport implements SessionAware {

    private static final long serialVersionUID = 1L;
    private String userName;
    private String password;

    private Map<String, Object> session;
        private Service service;
        Utilisateur user; 
    // ---------------------------- Log Out register user


    public String logOut() {
        session.remove("loginId");
        session.clear();
        addActionMessage("You Have Been Successfully Logged Out");
        return SUCCESS;
    }

    // ---------------------------- Login register user

    public String loginRegisterUser() {

        service = new ServiceImpl();
         user = service.checkUsernamePassword(userName, password);

        if (user != null) 
        {
            session.put("loginId", userName);
            session.put("mdp", password);
            session.put("role", user.getRole().getRole());
            return "success";
        } 
        else
        {
                addActionError("Please Enter Valid emailId or Password");
            return LOGIN;
        }
    }

    public void setSession(Map<String, Object> map) {
        this.session = map;
    }

How can I get them in my action classes ?

EDIT :

I try to get it from other action with this code :

public class GestionMissions extends ActionSupport implements  SessionAware, ModelDriven
{
    private Map<String, Object> session;


    @Override
    public void setSession(Map<String, Object> s) {
        this.session = s;

    }

    public String getsession() 
    {
        String test;
        test = (String) session.get("loginId");

        System.out.println(test);
                return "success";

}
Was it helpful?

Solution

The session variables are contained in the map you get in the setSession method, simply call session.get('attribut') to get the value you want.

Here is a longer explanation with some examples.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top