@ManagedProperty和FacesContext.getCurrentInstance()之间的区别.getExternalContext()。getSessionMap()。get(“bean”)

时间:2015-12-13 10:46:35

标签: jsf managed-property externalcontext

有什么区别:

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

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

1 个答案:

答案 0 :(得分:0)

@ManagedProperty方式将自动创建bean,如果它还不存在的话。如果预先没有(自动)创建bean,getSessionMap()方式将不会返回null

以最少的努力和关注实现同样的代码更好。