我正在爲我公司的所有內部應用程序創建一個facelets模板。它的外觀基於用戶選擇的皮膚(如gmail主題)。跨多個WAR文件的Cookie
將用戶的首選皮膚存儲在cookie中是有意義的。
我的「用戶首選項」WAR可以看到這個cookie。但是,我的其他應用程序無法找到cookie。它們與用戶首選項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仍然不喜歡它(當然)。
感謝, 扎克
沒有這樣做。我在同一個域上。 我也嘗試設置路徑爲「/」,並沒有這樣做。 – 2009-08-25 15:52:08