java如何在多个Struts 2操作类中获取存储的会话值? 2 月,3 周 Questions & Answers 1551 如何在多个Struts 2操作类中获取存储的会话值 我不想在所有操作中使用SessionAware接口
# 1 楼答案 你有三个选择: 正确地使用SessionAware李> 正确操作,并使用使用SessionAware的基本操作类李> 做错事并使用ActionContext: Map attibutes = ActionContext.getContext().getSession(); 记录在Struts 2 wiki的How do we get access to the session下 为什么不你想使用SessionAware,让你的行为更容易测试
# 2 楼答案 如果不想在多个类中使用SessionAware,那么至少可以使用一个抽象类或动作类扩展的接口。它将向动作类实例注入SessionMap 获得SessionMap或直接HttpSession的其他方法是从here获得: If you want to put something to the session you should get the session map from the action context Map<String, Object> session = >ActionContext.getContext().getSession(); session.put("username", username); session.put("role", 1); or use servlet session directly HttpSession session = >ServletActionContext.getRequest().getSession(); session.setAttribute("username", username); session.setAttribute("role", 1); But the first case is preferable, due to it's supported by the framework. 更多其他选项(您至少还有五个选项): 实施^{} interceptor李> 使用^{} interceptor李> 使用限定范围的bean configuration和inject将其应用于操作李> 使用自定义注释(即Conversation plugin) 使用Spring plugin和spring bean scopes李>
# 1 楼答案
你有三个选择:
SessionAware
李>SessionAware
的基本操作类李>ActionContext
:记录在Struts 2 wiki的How do we get access to the session下
为什么不你想使用
SessionAware
,让你的行为更容易测试# 2 楼答案
如果不想在多个类中使用
SessionAware
,那么至少可以使用一个抽象类或动作类扩展的接口。它将向动作类实例注入SessionMap
获得
SessionMap
或直接HttpSession
的其他方法是从here获得:更多其他选项(您至少还有五个选项):