ContextContainer
The 'context' that keeps on popping up (Request context, Snakelet context, Session context, Application context) is just a simple holder for any value you like. It is an 'empty' object that you can add your own attributes to, for instance:
所有出现的过的 ‘context’(Request context, Snakelet context, Session context, Applictaion constext)只是用来存储您需要的变量的简单固定器。它是一个空对象,您在其中可以任意添加属性,比如:
- self.getAppContext().userName = req.getParameter("userName")
复制代码
And in your pages you can access them easily, for instance:
在您得页面中访问起来也很容易,比如:
- <h1>;Hello, <%=ApplicationCtx.userName%>;!</h1>;
复制代码
You have to use the ContextContainers to store information! It is not a good idea to store your data directly in the Snakelet, Webapp or whatever other objects!
您必须使用ContextContainer 来存储信息!直接将数据存放在Snakelet,Webapp或其他对象中并不是一个很好的主意。
TIP: to avoid AttributeError exceptions when you're accessing attributes that have not been defined in the context, you can use getattr with a default parameter:
提示:如果您没有在context中定义属性,为了避免您访问这些不存在的属性所造成的AttributeError 异常,您可以使用getattr附加一个默认值:
- <h1>;Hello, <%=getattr(ApplicationCtx,"userName","unknown name")%>;!</h1>;
复制代码
TIP: the Application context is perfectly suited to store stuff that you want to have access to (from your Ypages/snakelets) during the whole application, such as utility classes or persistent variables. You can do this in the __init__ method of one of your snakelets, for instance. The Session context is very important: you can store stuff here that is associated with the user session (it is unique per user, and is removed when the user is no longer on the site).
提示:Application context使用来存储您可以在整个Application中都可以访问的数据,就像公有类或固定值。您可以在每一个snakelets的 __init__方法中来实现他。Session context是非常重要的:用来存储与用户session相关联的信息(对于每个用户唯一,当用户下线后销毁)。
The Request context is volatile and only exists during the processing of a single request. You can put values here that need to be displayed on the target page.
Request context 是可变的,他仅存在于处理单独请求的时候。您可以用它来传递你要在页面中显示的数据。
The Snakelet context is not very useful (its inside a single snakelet, you could use regular class attributes for the same purpose).
Snakelet context 不是经常使用的(它存在于单独的snakelet中,您可以使用 regular class attributes来达到同样的目的)。
Server Context (there is none)
The WebApp runs inside the server. You cannot access this server. This is due to security restrictions, and most interesting stuff is already accessible trough the other interfaces). Because of this, the 'largest' scope is the web app scope. You cannot exchange information with another web app by using context storage, for example. You can store things on the web app context and read them from other snakelets or Ypages, within the same webapp.
WebApp在server中运行。您不能访问server。这是由于安全限制,很多有趣的信息已经可以通过其他界面访问。因此,’最大’范围是指web app的范围。您在web app之间不能通过context交换变量。您只能在同一个web app中使用snakelet 或 Ypage 对数据进行操作。 |