Salesforce Session变量,在Session中设置和获取变量
我希望能够在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您正在构建诸如购物车或“向导”之类的东西,您需要将控制器变量保留在从一个页面视图到另一个页面视图的上下文中,那么在 VisualForce 中执行此操作的最佳方法是使用相同的控制器。
当用户提交表单(通过actionFunctions、commandButtons或commandLinks等)并且您的控制器返回页面引用时,如果新的视觉力页面使用相同的控制器,则视图状态将被保留。
通过这种方式,您可以让用户使用第一页上的 apex:inputField 标签输入姓名和电子邮件地址。他们导航到第二页,该页面使用与第一页相同的控制器,并且该页面可以引用相同的控制器变量。本质上,控制器仍然在范围内,所有更新的变量也是如此。
示例:
第一页:
第二页:
控制器:
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:
Page two:
Controller:
自定义设置在应用程序级别缓存,也许这就是上面链接中建议的原因。我不确定我是否会推荐这种方法,但您也许可以让它发挥作用。
如果您创建名为“SessionData”的自定义设置,并添加自定义字段(表示您要在会话中存储的数据),则可以将数据保存到其中,如下所示:
然后使用生成的自定义设置 ID 存储在 cookie 中。虽然可以使用普通 SOQL 访问自定义设置,但优点是数据被缓存并可以像这样访问:
请记住,自定义设置并不是真正为此设计的,因此您需要定期清除旧的自定义设置数据,就像普通的会话管理系统一样。
请参阅 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:
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:
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.
我认为 Visualforce 视图状态 可能对您有用:
I think Visualforce View State might be useful to you:
为此,您应该使用 Javascript cookie。
您还可以使用 Apex cookie,但是您'需要确保每个请求都到达服务器(而不是缓存层)。
对于 Apex Cookie,您可以使用以下代码:
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: