是否可以访问ColdFusion中的矩阵参数(名称值对通过分号分隔)? [英] Is it possible to access the matrix parameters (name-value pair separated by semicolon) in ColdFusion?

查看:191
本文介绍了是否可以访问ColdFusion中的矩阵参数(名称值对通过分号分隔)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是矩阵参数的新手,我知道CF10可以通过他们的新的RESTful API支持。

I'm new to matrix parameter and I know CF10 can access them through their new RESTful API support.

但是,是否有一种方法可以在不使用RESTful API支持的情况下访问这些参数?

However, is there a way to access these parameters without using RESTful API support?

例如 http://moremaps.com/map/color.cfm;lat=50;long=20;scale=32000

推荐答案

您可以使用:

color.cfm;lat=50;long=20;scale=32000

然后获取param字符串:

Then get the param string with:

ListRest(getPageContext().getRequest().getRequestUri(),';')

这在CFMX中起作用 - 它不是特定于CF10或RESTful API的一部分,并且由于servlet容器(Tomcat / Jrun / etc)遵循servlet规范具有获取原始URL的能力。

This worked back in CFMX - it's not specific to CF10 or part of the RESTful API, and is available due to the servlet container (Tomcat/Jrun/etc) following the servlet spec with the ability to get the original URL.

(当然可以使用URL重写来隐藏用户的.cfm。)

没有矩阵范围,因为CF没有完全实现它 - 它是作为REST Web服务的一部分,(其中它作为具有适当 RestArgSource 属性的参数)。

There isn't a matrix scope because CF hasn't implemented it fully - it is done as part of REST webservices, (where it's as an argument with the appropriate RestArgSource attribute). Only the CF team can say why they designed it that way.

但是,您可以轻松地创建自己的作用域/结构,如下所示:

However, you can easily create your own scope/struct like so:

var MatrixString = ListRest(getPageContext().getRequest().getRequestUri(),';');
var Matrix = {};

for ( var CurParam in ListToArray(MatrixString,';') )
    Matrix[ UrlDecode( ListFirst(CurParam,'=') ) ] = UrlDecode( ListRest(CurParam,'=') );

(显然,如果不在函数内部使用,则删除var scoping。)

这可以直接和通过IIS工作,即使在path_info可能已被修改的情况下,也应该在其他服务器上正常工作。

That works both directly and via IIS, and should work fine on other servers too, even where path_info may have been modified.

更新:此回答不完整/不准确。

Update: This answer is incomplete/inaccurate.

进一步在矩阵参数上,它们实际上可以出现在request_uri(整个部分在host_name之后,在query_string之前)的任何点 - 也就是说,script_name和path_info都可以包含参数,而不影响它们的最终值。

Reading up further on matrix params, they can actually appear at any point in the request_uri (the entire part after the host_name, before the query_string) - that is, both script_name and path_info can contain parameters, without affecting their final value.

要澄清这一点,这两个网址:

To clarify this, both these URLs:

htp://domain.com/a/b.cfm/c/d
http://domain.com/a;alpha=1/b.cfm;beta=2/c;gamma=3/d;delta=4

这些CGI变量中的结果:

Result in these CGI vars:

script_name = /a/b.cfm
path_info = /c/d

(除了在IIS中,path_info执行不正确。)

显然,提取和操作这些属性比上面的代码更复杂 - 我会更新这个答案,一旦我确保我更完全理解他们。

Obviously extracting and acting upon these properties is more complex than the code above - I'll update this answer again once I've made sure I understand them more fully.

同时,这里有几个潜在的选项 - 第一个返回params的结构,如果一个路径元素有一个,第二个返回一个包含每个路径元素的数组 - 无论这些都是合适的将取决于矩阵参数如何要使用:

In the meantime, here's a couple of potential options - the first returns a struct of params if a path element has one, the second returns an array containing every path element - whether either of these are suitable will depend on how the matrix params are to be used:

<cffunction name="getMatrixStruct" returntype="Struct" output=false
    hint="Returns struct with item for each path element with params"
    >
    <cfargument name="RequestUri" type="String" required hint="The non-host and non-querystring part of a URL." />
    <cfscript>
        var Result = {};

        for ( var CurSegment in ListToArray(RequestUri,'/') )
        {
            var SegName = UrlDecode( ListFirst(CurSegment,';') );

            for ( var CurParam in ListToArray(ListRest(CurSegment,';')) )
                Result[SegName][UrlDecode( ListFirst(CurParam,'=') ) ] = UrlDecode( ListRest(CurParam,'=') );

        }

        return Result;
    </cfscript>
</cffunction>


<cffunction name="getMatrixArray" returntype="Array" output=false
    hint="Returns array of structs for all path element, with any parameters included."
    >
    <cfargument name="RequestUri" type="String" required hint="The non-host and non-querystring part of a URL." />
    <cfscript>
        var Result = [];
        var Pos = 0;

        for ( var CurSegment in ListToArray(RequestUri,'/') )
        {
            Result[++Pos] = { 'Name' = UrlDecode( ListFirst(CurSegment,';') ) };

            for ( var CurParam in ListToArray(ListRest(CurSegment,';')) )
                Result[Pos][UrlDecode( ListFirst(CurParam,'=') ) ] = UrlDecode( ListRest(CurParam,'=') );

        }

        return Result;
    </cfscript>
</cffunction>

这篇关于是否可以访问ColdFusion中的矩阵参数(名称值对通过分号分隔)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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