我的Salesforce页面中有一个购物车功能。我已经创建了一个CookieJar类来存储基于此tutorial的cookie我想要检索一个cookie列表。目前购物车页面只会检索1个包含1个产品的Cookie。我想检索我存储的所有cookie。我该怎么做?
APEX控制器:
public PageReference addToCart() {
for(DisplayProducts p : products) {
if(0 < p.qtyToBuy) {
//Create an instance of the cookieJar class, passing it the values entered in the fields
cookieJar c = new cookieJar(p.productid, p.name, String.valueOf(p.qtyToBuy));
}
}
PageReference pageRef = new PageReference('/apex/cart');
pageRef.setRedirect(true);
return pageRef;
}
public String getCartContents() {
String msg = '<ul>\n';
Cookie theCookie;
theCookie = ApexPages.currentPage().getCookies().get('productid');
if(theCookie != null)
msg +=theCookie.getValue();
theCookie = ApexPages.currentPage().getCookies().get('productName');
if(theCookie != null)
msg +=theCookie.getValue();
theCookie = ApexPages.currentPage().getCookies().get('qtyToBuy');
if(theCookie != null)
msg += theCookie.getValue();
}
public class cookieJar {
public cookieJar(String productid, String productname, String qtyToBuy) {
Cookie pId = new Cookie('productid', productid,null,315569260,false);
Cookie pName = new Cookie('productName', productname,null,315569260,false);
Cookie qty = new Cookie('qtyToBuy', qtyToBuy,null,315569260,false);
//Set the page cookies using the setCookies() method
ApexPages.currentPage().setCookies(new Cookie[]{pId, pName, qty});
}
}//end cookieJar inner class
}
VF CART PAGE:
<apex:form >
<apex:pageBlock title="Your Cart" id="shopping_cart">
<apex:outputText value="{!cartContents}" escape="false"/>
</apex:pageBlock>
</apex:form>
答案 0 :(得分:2)
您已经拥有了问题所需的代码:
ApexPages.currentPage().getCookies()
PageReference.getCookies()会返回从密钥到Cookie的地图。
Map<String, System.Cookie[]> cookieMap =
for(string cookieKey : cookieMap.keySet()) {
System.debug('Cookie Key: ' + cookieKey + ' value ' +
cookieMap.get(cookieKey).getValue());
}
顺便提一下,Salesforce Stackexchange网站是询问Salesforce特定问题的好地方。