如何访问由 webrick 提供的 .rhtml 页面的 html 请求参数? [英] How to access html request parameters for a .rhtml page served by webrick?

查看:43
本文介绍了如何访问由 webrick 提供的 .rhtml 页面的 html 请求参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 webrick(内置的 ruby​​ 网络服务器)来提供 .rhtml文件(嵌入了 ruby​​ 代码的 html --like jsp).

I'm using webrick (the built-in ruby webserver) to serve .rhtml files (html with ruby code embedded --like jsp).

它工作正常,但我不知道如何访问参数(例如 http://localhost/mypage.rhtml?foo=bar)从 .rhtml 文件中的 ruby​​ 代码中.(请注意,我没有使用 rails 框架,只使用 webrick + .rhtml 文件)

It works fine, but I can't figure out how to access parameters (e.g. http://localhost/mypage.rhtml?foo=bar) from within the ruby code in the .rhtml file. (Note that I'm not using the rails framework, only webrick + .rhtml files)

谢谢

推荐答案

根据erbhandler的源码,rhtml文件是这样运行的:

According to the source code of erbhandler it runs the rhtml files this way:

    Module.new.module_eval{
      meta_vars = servlet_request.meta_vars
      query = servlet_request.query
      erb.result(binding)
    }

所以绑定应该包含一个 query(它包含查询字符串的哈希值)和一个 meta_vars 变量(它包含环境的哈希值,如 SERVER_NAME),您可以在 rhtml 文件中访问它(servlet_requestservlet_response 也可能可用,但我不确定它们).

So the binding should contain a query (which contains a hash of the query string) and a meta_vars variable (which contains a hash of the environment, like SERVER_NAME) that you can access inside the rhtml files (and the servlet_request and servlet_response might be available too, but I'm not sure about them).

如果不是这种情况,您也可以尝试查询 CGI 参数 ENV["QUERY_STRING"] 并解析它,但这应该只是最后的手段(并且它可能只适用于CGI 文件).

If that is not the case you can also try querying the CGI parameter ENV["QUERY_STRING"] and parse it, but this should only be as a last resort (and it might only work with CGI files).

这篇关于如何访问由 webrick 提供的 .rhtml 页面的 html 请求参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆