[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
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)只是用来存储您需要的变量的简单固定器。它是一个空对象,您在其中可以任意添加属性,比如:
  1. self.getAppContext().userName = req.getParameter("userName")
复制代码


And in your pages you can access them easily, for instance:
在您得页面中访问起来也很容易,比如:
  1. <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附加一个默认值:
  1. <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 对数据进行操作。

TOP

WebApp

Snakelets run inside a WebApp context (snakeserver.webapp.WebApp). You can get the current WebApp object by calling getWebApp() on the request or the snakelet object itself. You can then use the following methods from the webapp:
Snakelet在WebApp 环境中运行(snakeserver.webapp.WebApp)。您可以在请求或snakelet对象中通过调用getWebApp()得到当前的WebApp对象。


Method                  Description


      getName()
      a tuple: (short name, full name). The short name is taken from the webapp directory it is in. Full name is specified in the webapp descriptor __init__.py file.a tuple: (short name ,full name)(好像是参数吧?)Name从webapp的目录中得到。全名被指定在webapp的描述文件 __init__.py中。

      getVirtualHost()
      a tuple (hostname, portnumber) of the 'virtual host' this webapp is running on. Does not take vhost aliasing in account; if you need the 'real' vhost and port that the request arrived on, use request.getServerName().正在运行该webapp的’虚拟主机’ 的tuple (hostname, portnumber)。请不要与vhost混淆;当请求到达是如果您需要真实vhost和端口,请使用 request.getServerName()。

      isEnabled()
      is the webapp enabled? (True or False)webapp是否允许?(True / False)。

      setEnabled(enabled)
      enable or disable this webapp开启或关闭webapp

      getFileSystemPath()
      the filesystem path where the webapp's files are located得到当前的webapp文件的文件系统路径

      getDocRootPath()
      the web document root, where the pages, images, etc are locatedweb 文档根,放置页面,图片的地方。

      getContext()
      webapp context (ContextContainer object). Scope: web app. shared for all users/requestswebapp context(ContextContainer 对象)。范围:web app。对所有用户和请求共享

      getURLprefix()
      the URL prefix that is used for urls inside this webapp (example: "/shop/" This prefix includes the (optional) server-wide prefix.得到 用于webapp url中的URL前缀(比如:”/shop/”),包括(可选择)server-wide前缀。

      getAssetprefix()
      the asset base location as specified in the WebApp's config file.得到在WebApp的配置文件中所制定的资源路径前缀。

      mkUrl(path)mkUrl(path, arg="", params=[], htmlescape=True)
      create a correct URL based on the webapp's url prefix, and automatically html-escapes the resulting string so you can directly use it in your html output. arg (optional) can be given a string that should become the url 'arg'. params (optional) can be given a dict or a sequence that contains the name/values that should become the query params. By setting the optional htmlescape param to False, no escaping will be done (and the resulting string can no longer be directly used in html output because there is a risk of generating invalid html) 建立正确的基于webapp url的前缀,并且对结果字符串自动进行html-escapes ,因此您可以在html输出中直接使用。arg(可选)参数,可以传递字符串作为url ‘arg’。 params (可选)参数,可以传递包括 name/values的dict或序列。设置 htmlescape参数为False,将不会进行html的escape操作(结果将不能直接用于html输出,因为这里在产生错误页面是存在安全问题)。


      mkAssetUrl(path)
      mkAssetUrl(path, htmlescape=True)
      create a correct URL for the static asset (images, files etc) based on the asset base location. It automatically html-escapes the resulting string so you can directly use it in your html output. By setting the optional htmlescape param to False, no escaping will be done (and the resulting string can no longer be directly used in html output because there is a risk of generating invalid html) 为静态资源创建正确的URL(images, files 等等)基于本地资源。自动对结果字符串进行html-escape,您可以在html的输出中直接使用它。通过修改 htmlescape参数选项可以关闭此功能。

      getSnakelet(urlpattern)
      returns the snakelet instance belonging to the given urlpattern (from the webapp init file)从webapp的init文件中返回属于给出的urlpattern(url样式)的snakelet实例。

      getConfigItems()
      the dictionary of all user config items所有用户配置选想的目录。

      getConfigItem(item)
      the value of a single user config item单个用户配置项的值。

      addPlugin(plugin)
      add a webapp-specific plugin (for instance to do things at session creation time). plugin is your plugin instance. If a plugin with the same name already exists, a ValueError will be raised. If your webapp is deployed on multiple vhosts, you should be prepared for this (the webapp's init code will be called multiple times). For more info, see the section on plugins.添加一个特殊插件。如果此插件名称已经存在,将产生一个ValueError的错误,如果您的webapp搭建在复合vhost上,您应该提前做好准备(wbapp的初始化代码段会被重复调用)。更多信息,请见章节 plugins


      serveStaticFile(filename, response, useResponseHeaders=False)
      Use the server's internal code to serve a static file to the client. This is useful in snakelets that return files (such as download controllers). You can fool around with setting the correct headers, and reading/writing the file efficiently to the response stream yourself, but it's usually much better to use this function instead. The response object must not be used prior to this call. You cannot set custom HTTP headers, but the required ones will be generated for you automatically (such as content-lenth). The filename must be the absolute path to to file that must be served. This must be the last action in your snakelet that generates output (the response's output must not be used at all) The file serve loop is quite efficient, and will use the sendfile system call if available. NOTE: the server's RequestPlugins are not invoked on static files! (partly due to the efficient stripped down processing code for static files to obtain higher performance) The useResponseHeaders parameter tells the server if it should add the HTTP headers from the request object (such as Cache-Control, Content-Disposition and other headers). Defaults to False. 使用内部代码为客户端提供静态文件服务。对于返回文件来说这是非常实用的(比如下载控制)。你可以通过设置正确的header 和 有效率地 读/写 文件 来反馈数据流,但是通常最好的做法是用这个函数来代替它。Response 对象一定不能在调用这个函数之前使用。您不能设置自定义的HTTP header,被请求的响应会自动帮您生成相应的header(比如 content-lenth)。提供文件服务的文件名必须是绝对路径。这必须是在snakelet中最后执行的动作,它提供的文件输出(反馈输出一定不能使用)服务循环是很高效的、如果可能还将运用到文件传送系统。主意:RequestPlugin 不能在静态文件中被导入!(部分由于分开的处理代码分别对静态文件进行处理,所以效率很高??)useResponseHeader参数告诉server 是否添加HTTP header(比如 Cache-Control, Content-Disposition 或者其他)。默认是False.

原文:http://bbs.chinaunix.net/thread-563498-1-1.html

TOP

看不懂英语?这不是有翻译嘛

TOP

返回列表