以下は、FacesContext を介してセッションから User オブジェクトを追加および取得するために、LoginBean によって呼び出されるユーティリティ クラスです。
アプリケーションはSpringを使用しています。アノテーションを使用する必要がありますか、それともこの種のクラスに静的メソッドを含めることは受け入れられている方法ですか? アノテーションが推奨される場合、@Component または @Service を使用する必要がありますか?
// Annotate as Service/Component?
public class WebUtils {
// Add user object to session
public void setUser( User user ){
FacesContext context = FacesContext.getCurrentInstance();
context.getExternalContext().getSessionMap().put( "user", user );
}
// Get user from session
public User getUser( FacesContext context ){
if( context != null )
return (User) context.getExternalContext().getSessionMap().get("user");
return null;
}