开发者

Saving session attributes in an Object(Java)

开发者 https://www.devze.com 2023-02-11 06:56 出处:网络
How can I save session attributes on a class using Java and JSF? Is there any annotation th开发者_高级运维at helps me do it?

How can I save session attributes on a class using Java and JSF? Is there any annotation th开发者_高级运维at helps me do it? Help me out, I need some session attributes, and I guess would be better if I save them on Object. If You guys know the command too, to save in a different way, I'd appreciate it.


Just use a @SessionScoped @ManagedBean.

@ManagedBean
@SessionScoped
public class SessionBean {

    private SomeObject someObject; // +getter+setter

}

You can access it from other managed beans by @ManagedProperty.

@ManagedBean
@RequestScoped
public class OtherBean {

    @ManagedProperty(value="#{sessionBean}")
    private SessionBean sessionBean; // +setter

    public void someAction() {
        SomeObject someObject = sessionBean.getSomeObject();
        // ...
    }

}

Or if you really insist in storing it outside managed beans, use the session map.

Map<String, Object> sessionMap = externalContext.getSessionMap();
sessionMap.put("someObject", someObject);

It'll be available in other beans by

Map<String, Object> sessionMap = externalContext.getSessionMap();
SomeObject someObject = (SomeObject) sessionMap.get("someObject");

It maps under the covers to HttpSession#get/setAttribute().


EL expression to access objects in session map

<h:inputText id="sample" value="#{sessionScope.someProperty}"/>
0

精彩评论

暂无评论...
验证码 换一张
取 消