![](/img/trans.png)
[英]Set session variable using JSTL and accessing it in servlet/controller class
[英]Accessing Session variable in action class
我在我的項目中有struts2.0,我通過執行SessionAware來將用戶名存儲在會話中,如下所示
public class NewProduct extends ActionSupport implements SessionAware{
private Map session;
public String execute() {
session.put("username", "Rangappa");
return "success"
}
public void setSession(Map session) {
this.session = session;
}
public Map getSession() {
return session;
}
在這里我在這里設置會話變量,即時通訊沒有得到如何在其他類中獲取此會話變量,我做了如下但它不起作用
public class NewProduct extends ActionSupport implements SessionAware{
public String execute() throws Exception {
String username = (String) session.get("username");
System.out.println("username: "+username );
return "success"
}
}
這兩堂課都在同一包中,請讓我知道解決方法,謝謝
導入適當的程序包后使用此代碼
Map session = ActionContext.getContext().get("session");
String userName = (String)(session.get("username"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.