sessionAware:如何从操作中获取会话属性

时间:2014-05-13 17:04:58

标签: java session struts2

我想从实现SessionAware接口的一些操作中访问会话属性;

我想根据登录用户的类型执行一些测试,它所涉及的信息存储在会话的属性中,我可以在JSP页面中使用以下代码获取此属性:

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

我的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;
    }

如何在行动课程中获取它们?

编辑:

我尝试使用此代码从其他操作中获取它:

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";

}

1 个答案:

答案 0 :(得分:1)

会话变量包含在setSession方法中的地图中,只需调用session.get('attribut')即可获得所需的值。

Here is a longer explanation with some examples.