【问题标题】:Difference between @ManagedProperty and FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("bean")@ManagedProperty 和 FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("bean") 之间的区别
【发布时间】:2015-12-13 10:46:35
【问题描述】:

两者有什么区别:

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");
}

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

【问题讨论】:

    标签: jsf managed-property externalcontext


    【解决方案1】:

    @ManagedProperty 方法会在 bean 不存在时自动创建它。 getSessionMap() 方式不会并且因此可能返回 null 如果没有事先(自动)创建 bean。

    以最少的努力和关注达到相同效果的代码更好。

    【讨论】:

    • 感谢您的回复:D
    猜你喜欢
    • 2013-03-06
    • 2011-05-05
    • 1970-01-01
    • 1970-01-01
    • 2011-10-11
    • 2011-05-16
    • 2011-06-10
    • 2015-09-17
    • 2019-11-05
    相关资源
    最近更新 更多