Difference between @ManagedProperty and FacesConte

2019-09-08 04:24发布

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条回答
Lonely孤独者°
2楼-- · 2019-09-08 04:48

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.

查看更多
登录 后发表回答