Difference between @ManagedProperty and FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(“bean”)

混江龙づ霸主 提交于 2020-01-06 20:10:00

问题


What's the difference between:

public class GameController implements Serializable{

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

and

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.


回答1:


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.



来源:https://stackoverflow.com/questions/34250255/difference-between-managedproperty-and-facescontext-getcurrentinstance-getext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!