如何在JSF的会话范围内获取和设置对象? [英] How do I get and set an object in session scope in JSF?
问题描述
我只需要在JSF应用程序的会话范围内保留一个对象.我在哪里定义会话变量,以及如何从视图文件或支持bean中获取和设置会话变量?
I need to persist just one object in session scope of my JSF application. Where do I define a session variable, and how do I get and set it from either a view file or backing bean?
推荐答案
两种常用方法:
-
将其存储在
再后来:
SomeObject yourVariable = (SomeObject) sessionMap.get("somekey");
-
或者,使其成为您在
@RequestScoped
bean中注入的@SessionScoped
bean的属性. Or, make it a property of a
@SessionScoped
bean which you inject in your@RequestScoped
bean.sessionBean.setSomeVariable(yourVariable);
再后来:
SomeObject yourVariable = sessionBean.getSomeVariable();
您可以通过
@Inject
将@Named @SessionScoped
转换为@Named @RequestScoped
.@Inject private SessionBean sessionBean;
或者,如果您尚未使用CDI,则可以通过
@ManagedProperty
将@ManagedBean @SessionScoped
转换为@ManagedBean @RequestScoped
.Or, if you're not using CDI yet, you can get a
@ManagedBean @SessionScoped
into a@ManagedBean @RequestScoped
via@ManagedProperty
.@ManagedProperty("#{sessionBean}") private SessionBean sessionBean; // +getter+setter
这篇关于如何在JSF的会话范围内获取和设置对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!