Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cookies 如何在JSF中添加cookie?_Cookies_Jsf 2 - Fatal编程技术网

Cookies 如何在JSF中添加cookie?

Cookies 如何在JSF中添加cookie?,cookies,jsf-2,Cookies,Jsf 2,当我添加cookie时,如下所示: FacesContext.getCurrentInstance().getExternalContext().addResponseCookie("Test", "Test", null); Map<String, Object> properties = new HashMap<>(); properties.put("domain", "test"); properties.put("maxAge", 31536000); pro

当我添加cookie时,如下所示:

FacesContext.getCurrentInstance().getExternalContext().addResponseCookie("Test", "Test", null);
Map<String, Object> properties = new HashMap<>();
properties.put("domain", "test");
properties.put("maxAge", 31536000);
properties.put("secure", false); 
properties.put("path","/");
FacesContext.getCurrentInstance().getExternalContext().addResponseCookie("Test", "Test", properties);
然后它工作得很好,但是cookie变成了会话cookie,最大年龄为
-1

当我尝试以下方法时:

FacesContext.getCurrentInstance().getExternalContext().addResponseCookie("Test", "Test", null);
Map<String, Object> properties = new HashMap<>();
properties.put("domain", "test");
properties.put("maxAge", 31536000);
properties.put("secure", false); 
properties.put("path","/");
FacesContext.getCurrentInstance().getExternalContext().addResponseCookie("Test", "Test", properties);
Map properties=newhashmap();
属性。放置(“域”、“测试”);
出售(“maxAge”,31536000);
属性。放置(“安全”,假);
properties.put(“路径”、“/”);
FacesContext.getCurrentInstance().getExternalContext().addResponseCookie(“测试”,“测试”,属性);
那我就看不到饼干了。我不明白为什么

我正在使用Tomcat 7。

试试这个:

public class CookieHelper {

  public void setCookie(String name, String value, int expiry) {

    FacesContext facesContext = FacesContext.getCurrentInstance();

    HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
    Cookie cookie = null;

    Cookie[] userCookies = request.getCookies();
    if (userCookies != null && userCookies.length > 0 ) {
        for (int i = 0; i < userCookies.length; i++) {
            if (userCookies[i].getName().equals(name)) {
                cookie = userCookies[i];
                break;
            }
        }
    }

    if (cookie != null) {
        cookie.setValue(value);
    } else {
        cookie = new Cookie(name, value);
        cookie.setPath(request.getContextPath());
    }

    cookie.setMaxAge(expiry);

    HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
    response.addCookie(cookie);
  }

  public Cookie getCookie(String name) {

    FacesContext facesContext = FacesContext.getCurrentInstance();

    HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
    Cookie cookie = null;

    Cookie[] userCookies = request.getCookies();
    if (userCookies != null && userCookies.length > 0 ) {
        for (int i = 0; i < userCookies.length; i++) {
            if (userCookies[i].getName().equals(name)) {
                cookie = userCookies[i];
                return cookie;
            }
        }
    }
    return null;
  }
}
公共类CookieHelper{
公共void setCookie(字符串名称、字符串值、int到期){
FacesContext FacesContext=FacesContext.getCurrentInstance();
HttpServletRequest=(HttpServletRequest)facesContext.getExternalContext().getRequest();
Cookie=null;
Cookie[]用户cookies=request.getCookies();
if(userCookies!=null&&userCookies.length>0){
for(int i=0;i0){
for(int i=0;i
您的特定案例失败,因为域设置错误。Cookie是特定于域的。不能在其他域上设置cookie。如果您没有指定域,那么它将默认为当前请求URI的域。
Cookie#setDomain()
仅当您打算在公共或不同的子域上设置Cookie时才有用。例如,如果您有
foo.example.com
bar.example.com
,则可以通过该方法为其他域设置cookie,或将域设置为
.example.com
(带前导句点!),以便在两个子域之间共享cookie

因此,在您的特定情况下,这应该可以做到:

String name = "cookiename";
String value = "cookievalue";
Map<String, Object> properties = new HashMap<>();
properties.put("maxAge", 31536000);
properties.put("path", "/");
externalContext.addResponseCookie(name, URLEncoder.encode(value, "UTF-8"), properties);
请注意,我在设置/检索cookie值之前对cookie值进行URL编码/解码,否则您将遇到以下相关问题中提出的问题:和

也就是说,我同意JSFAPI在检索和设置cookie方面有点不透明。JSF实用程序库中包含了用于此目的的,它隐式地将sane默认值设置为cookie属性,URL对该值进行编码/解码

// Getting a cookie value.
String value = Faces.getRequestCookie(name);

// Setting a session cookie in current path.
Faces.addResponseCookie(name, value, -1);

// Setting a session cookie in current domain.
Faces.addResponseCookie(name, value, "/", -1);

// Setting a (sub)domain-wide session cookie.
Faces.addResponseCookie(name, value, ".example.com", "/", -1);

// Setting a cookie with max age of 1 year in current domain.
Faces.addResponseCookie(name, value, "/", (int) TimeUnit.DAYS.toSeconds(365));

// Removing a cookie from current domain.
Faces.removeResponseCookie(name, "/");

美丽而简单。我缺少设置cookie的HttpServletResponse逻辑。现在一切都好了。我使用这个方法
Faces.removeResponseCookie(mykey,“/”),但该值仍保留在cookies中。我使用的是
omnifaces-1.14
version。那么域和/或路径就不一样了。在浏览器的开发人员工具集中检查它。@BalusC,我正在开发中,正在生产www.my-app.com。是否可以将生产中的cookie设置为“/”路径,并将开发中的cookie设置为“我的应用程序/”?我尝试了externalContext.getRequestContextPath(),但它总是在“/”之后返回第一部分。您要查找的是
externalContext.getApplicationContextPath()
。可能是,它面临.addResponseCookie(名称、值,“.example.com”、“/”、-1);是否自动添加前置期间?我将此函数用于domain.de,并为.domain.de添加了一个cookie。