Salesforce Session变量,在Session中设置和获取变量

发布于 2024-10-16 11:45:10 字数 211 浏览 8 评论 0原文

我希望能够在 Salesforce 站点页面的当前会话中读取/写入一些变量。

我有一个使用 Salesforce 站点构建的站点,我需要在所有页面上存储/检索一些值(考虑到我正在构建类似于购物车的东西)。 但是,我找不到任何关于如何在会话(匿名用户)中读取和写入变量的好示例。

我正在使用 Visualforce 页面以及 Apex 中内置的多个控制器。

问候

I want to be able to read / write some variables to the current session in my Salesforce site pages.

I have a site built using Salesforce Sites, I need to store/retrieve some values across all the pages (consider that I am building something similar to a shopping cart).
However I cant find any good example on how to read and write variables to the session (anonymous user).

I am using Visualforce pages with several controllers built in Apex.

Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

私野 2024-10-23 11:45:10

如果您正在构建诸如购物车或“向导”之类的东西,您需要将控制器变量保留在从一个页面视图到另一个页面视图的上下文中,那么在 VisualForce 中执行此操作的最佳方法是使用相同的控制器。

当用户提交表单(通过actionFunctions、commandButtons或commandLinks等)并且您的控制器返回页面引用时,如果新的视觉力页面使用相同的控制器,则视图状态将被保留。

通过这种方式,您可以让用户使用第一页上的 apex:inputField 标签输入姓名和电子邮件地址。他们导航到第二页,该页面使用与第一页相同的控制器,并且该页面可以引用相同的控制器变量。本质上,控制器仍然在范围内,所有更新的变量也是如此。

示例:

第一页:

<apex:page controller="myController">
   Please enter your name <apex:inputText value="{!shopper_name}"/>
   <br/>
   <apex:commandButton action="{!pageTwo}" value="Click for page two"/>
</apex:page>

第二页:

<apex:page controller="myController">
   You entered: <apex:outputText value="{!shopper_name}" />.
</apex:page>

控制器:

public class myController {
  public string shopper_name { get; set; }
  public myController() {
    shopper_name = null;
  }
}

If you are building something like a shopping cart, or a "wizard" where you need to keep controller variables in context from one page view to another, then the best way to do this in VisualForce is to use the same controller.

When the user submits a form ( through actionFunctions, commandButtons, or commandLinks, etc.), and your controller returns a page Reference, the view state is preserved if the new visual force page uses the same controller.

In this way, you could, for example, have the user enter their name and email address using apex:inputField tags on page one. They navigate to page two, which uses the same controller as page one, and the page could reference the same controller variables. Essentially, the controller is still in scope, and so are all the variables that were updates.

Example:

Page one:

<apex:page controller="myController">
   Please enter your name <apex:inputText value="{!shopper_name}"/>
   <br/>
   <apex:commandButton action="{!pageTwo}" value="Click for page two"/>
</apex:page>

Page two:

<apex:page controller="myController">
   You entered: <apex:outputText value="{!shopper_name}" />.
</apex:page>

Controller:

public class myController {
  public string shopper_name { get; set; }
  public myController() {
    shopper_name = null;
  }
}
一张白纸 2024-10-23 11:45:10

自定义设置在应用程序级别缓存,也许这就是上面链接中建议的原因。我不确定我是否会推荐这种方法,但您也许可以让它发挥作用。

如果您创建名为“SessionData”的自定义设置,并添加自定义字段(表示您要在会话中存储的数据),则可以将数据保存到其中,如下所示:

Database.SaveResult result = Database.insert(new SessionData__c(YourFieldHere='Your value here etc'));
System.debug(result.getID());

然后使用生成的自定义设置 ID 存储在 cookie 中。虽然可以使用普通 SOQL 访问自定义设置,但优点是数据被缓存并可以像这样访问:

if (SessionData__c.getAll().containsKey('unique ID from cookie here'))
{
     System.debug(SessionData__c.getInstance('unique ID from cookie here').YourFieldHere);
}

请记住,自定义设置并不是真正为此设计的,因此您需要定期清除旧的自定义设置数据,就像普通的会话管理系统一样。

请参阅 Apex 自定义设置文档 了解更多详情。

Custom settings are cached at the application level, maybe that's why it was suggested in the link above. I'm not sure if I'd recommend that approach, but you might be able to get it to work.

If you create a Custom Setting named "SessionData", and add your custom fields (that represent the data you want to store in session), you could save data to it like this:

Database.SaveResult result = Database.insert(new SessionData__c(YourFieldHere='Your value here etc'));
System.debug(result.getID());

Then use the resulting custom setting ID to store in a cookie. While custom settings can be accessed using normal SOQL, the advantage is that the data is cached and can be accessed like this:

if (SessionData__c.getAll().containsKey('unique ID from cookie here'))
{
     System.debug(SessionData__c.getInstance('unique ID from cookie here').YourFieldHere);
}

Keep in mind that custom settings weren't really designed for this, so you'll need to periodically purge old custom settings data, as normal session management systems do.

See the Apex Custom Settings documentation for more details.

梦醒时光 2024-10-23 11:45:10

我认为 Visualforce 视图状态 可能对您有用:

包含表单组件的 Visualforce 页面还包含一个加密的隐藏表单字段,该字段封装了页面的视图状态。该视图状态是自动创建的,顾名思义,它保存页面的状态 - 状态包括组件、字段值和控制器状态。

I think Visualforce View State might be useful to you:

Visualforce pages that contain a form component also contain an encrypted, hidden form field that encapsulates the view state of the page. This view state is automatically created, and as its name suggests, it holds the state of the page - state that includes the components, field values and controller state.

蓝梦月影 2024-10-23 11:45:10

为此,您应该使用 Javascript cookie

您还可以使用 Apex cookie,但是您'需要确保每个请求都到达服务器(而不是缓存层)。

对于 Apex Cookie,您可以使用以下代码:

//Setting Cookie
public void setCookie() {
    Cookie userCookie = new Cookie('CookieName', fieldValueToBeStoredAsCookie, null, 315569260, false); //Here 315569260 represents cookie expiry date = 10 years. You can set this to what ever expiry date you want. Read apex docs for more details.
    ApexPages.currentPage().setCookies(new Cookie[] {
        userCookie
    });
}

//Reading Cookie 
Cookie cookie = ApexPages.currentPage().getCookies().get('CookieName');
if (cookie != null) {
    String fieldValueToBeStoredAsCookie = cookie.getValue();        
}

You should use Javascript cookies for this.

You could also use Apex cookies, but then you'd need to make sure that each request hits the server (and not the caching layer).

for Apex Cookie you can use following code:

//Setting Cookie
public void setCookie() {
    Cookie userCookie = new Cookie('CookieName', fieldValueToBeStoredAsCookie, null, 315569260, false); //Here 315569260 represents cookie expiry date = 10 years. You can set this to what ever expiry date you want. Read apex docs for more details.
    ApexPages.currentPage().setCookies(new Cookie[] {
        userCookie
    });
}

//Reading Cookie 
Cookie cookie = ApexPages.currentPage().getCookies().get('CookieName');
if (cookie != null) {
    String fieldValueToBeStoredAsCookie = cookie.getValue();        
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文