gpt4 book ai didi

java - session 感知 : How can I get session attribute from action

转载 作者:行者123 更新时间:2023-12-01 12:59:26 25 4
gpt4 key购买 nike

我想从一些实现 SessionAware 接口(interface)的操作中访问 session 属性;

我想根据登录用户的类型执行一些测试,它相关的信息存储在 session 的属性中,我可以使用以下代码在我的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";

}

最佳答案

session 变量包含在您通过setSession方法获取的映射中,只需调用session.get('attribut')即可获取您想要的值。

Here is a longer explanation with some examples.

关于java - session 感知 : How can I get session attribute from action,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23637496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com