@ManagedProperty和FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("bean")之间的区别 [英] Difference between @ManagedProperty and FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("bean")

查看:111
本文介绍了@ManagedProperty和FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("bean")之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之间有什么区别

public class GameController implements Serializable{

@ManagedProperty(value="#{Gamebean}")
private Game game;

public class GameController implements Serializable{

private Game game;

public GameController(){
game =(Game)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("Gamebean");
}

如果没有区别,哪种方法更好? 干杯.

and if there's no difference what method is better ? Cheers.

推荐答案

如果@ManagedProperty方法尚不存在,它将自动创建bean. getSessionMap()方法不会,因此,如果不是事先(自动)创建bean,则可能会返回null.

The @ManagedProperty way will autocreate bean if it doesn't exist yet. The getSessionMap() way won't and may thus return null if bean isn't (auto)created beforehand.

以最少的精力和精力实现相同目标的代码会更好.

The code which achieves the same with least effort and concern is better.

这篇关于@ManagedProperty和FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("bean")之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆