2009-08-25 5 views
1

私の会社の社内アプリケーションすべてに対してfaceletsテンプレートを作成しています。その外観は、ユーザーが選択するスキン(Gmailのテーマなど)に基づいています。複数のWARファイルにまたがるクッキー

ユーザーの好みのスキンをクッキーに保存することは理にかなっています。

私の "user-preferences" WARはこのクッキーを見ることができます。しかし、私の他のアプリケーションは、クッキーを見つけることができません。それらは、ユーザー設定WARと同じドメイン/サブドメインにあります。

これには何らかの理由がありますか?

ここでは、好みのスキンを作成/検索するために使用される私のbeanです。この同じファイルはすべてのプロジェクトで使用されています:

// BackingBeanBase is just a class with convenience methods. Doesn't 
// really affect anything here. 
public class UserSkinBean extends BackingBeanBase { 

    private final static String SKIN_COOKIE_NAME = "preferredSkin"; 

    private final static String DEFAULT_SKIN_NAME = "classic"; 


    /** 
    * Get the name of the user's preferred skin. If this value wasn't set previously, 
    * it will return a default value. 
    * 
    * @return 
    */ 
    public String getSkinName() { 

     Cookie skinNameCookie = findSkinCookie(); 

     if (skinNameCookie == null) { 
      skinNameCookie = initializeSkinNameCookie(DEFAULT_SKIN_NAME); 
      addCookie(skinNameCookie); 
     } 

     return skinNameCookie.getValue(); 

    } 


    /** 
    * Set the skin to the given name. Must be the name of a valid richFaces skin. 
    *  
    * @param skinName 
    */ 
    public void setSkinName(String skinName) { 

     if (skinName == null) { 
      skinName = DEFAULT_SKIN_NAME; 
     } 

     Cookie skinNameCookie = findSkinCookie(); 

     if (skinNameCookie == null) { 
      skinNameCookie = initializeSkinNameCookie(skinName); 
     } 
     else { 
      skinNameCookie.setValue(skinName);  
     } 

     addCookie(skinNameCookie); 
    } 

    private void addCookie(Cookie skinNameCookie) { 
     ((HttpServletResponse)getFacesContext().getExternalContext().getResponse()).addCookie(skinNameCookie); 
    } 

    private Cookie initializeSkinNameCookie(String skinName) { 

     Cookie ret = new Cookie(SKIN_COOKIE_NAME, skinName); 
     ret.setComment("The purpose of this cookie is to hold the name of the user's preferred richFaces skin."); 

     //set the max age to one year. 
     ret.setMaxAge(60 * 60 * 24 * 365); 
     ret.setPath("/"); 
     return ret; 
    } 


    private Cookie findSkinCookie() { 
     Cookie[] cookies = ((HttpServletRequest)getFacesContext().getExternalContext().getRequest()).getCookies(); 

     Cookie ret = null; 
     for (Cookie cookie : cookies) { 
      if (cookie.getName().equals(SKIN_COOKIE_NAME)) { 
       ret = cookie; 
       break; 
      } 
     } 

     return ret; 
    } 
} 

私は間違っていることを誰にでも見せてくれるでしょうか?

更新:私はそれを狭めました...それはFFでうまく動作しますが、IEはまだ(もちろん)それを好きではありません。

おかげで、 ザック

答えて

0

私は解決策を見つけました。

私はちょうどクッキーを作成するためにクライアント側でjavascriptを使用しました。

これは正常に機能しました。

0

私はあなたがクッキーにドメイン/サブドメインを割り当てることが必要だと思います。

と同様に、(ドメインはドットで始める必要があることに注意してください)

ret.setDomain(".test.com"); 
ret.setDomain(".test.co.uk"); 

http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Cookies.html

+0

それはしませんでした。私は同じドメインにいる。 私はまたパスを "/"に設定しようとしましたが、それはしませんでした。 –

関連する問題