在ColdFusion组件(CFC)中,是否有必要对变量范围的变量使用完全限定名称?
如果我改变这个,我会不会遇到麻烦:
<cfcomponent>
<cfset variables.foo = "a private instance variable">
<cffunction name = "doSomething">
<cfset var bar = "a function local variable">
<cfreturn "I have #variables.foo# and #bar#.">
</cffunction>
</cfcomponent>
Run Code Online (Sandbox Code Playgroud)
这个?
<cfcomponent>
<cfset foo = "a private instance variable">
<cffunction name = "doSomething">
<cfset var bar = "a function local variable">
<cfreturn "I have #foo# and #bar#.">
</cffunction>
</cfcomponent>
Run Code Online (Sandbox Code Playgroud) (a)你应该在什么情况下改变范围变量?(b)你应该在ColdFusion组件中改变范围的情况是什么?
在cfmodule的cfm中,通过使用Caller范围返回值.如果我在CFC中的函数内调用cfmodule,调用者映射到CFC的变量范围是否正确?我可以将值返回到CFC函数的本地范围吗?
谢谢
我正在为我们的客户创建一个简单的Web服务,他们希望能够检索购物车信息并在购物车中添加/更新商品.我为每个人编写了一个带有远程方法的CFC.现在,显然当这些CFC方法设置为时access="remote",整个世界可以按原样调用它们.但是,我需要启用安全性以确保唯一可以远程调用这些方法的人(不是来自我的网站)是我已经允许的那些人.而且我不希望它是侵入性的(强制登录等).
例如,Web服务存在于http://www.mywebsite.com上,我只想允许来自http://www.yoursite1.com和http://www.yoursite2.com的请求.使用HTTP_REFER并不好,因为这可能是欺骗性的.我怎样才能做到这一点?是否可以使用自签名证书以某种方式验证请求是否被允许?
注意:我也希望能够将这些Web服务用于我们自己网站的呼叫,因此我需要一个适用于这两种方案的解决方案.
我有一组CFC,我从两个单独的Applicaiton范围访问.
一个/Application.cfc在Root中.
另一个应用程序位于/Admin/Application.cfc中cfcs位于/ _cfc /中
如果我从(例如)/Admin/members/edit.cfm中的页面调用cfc(使用createObject()),这个cfc是否从以下位置获取它的应用程序范围:
应用程序1:/Application.cfc
要么
应用2:/Admin/Application.cfc
调用页面位于应用程序2下,但CFC本身位于应用程序1下.
我希望我有意义.
谢谢
贾森
我正在尝试找出<cfscript>在ColdFusion 9中调用动态方法的正确语法.我已经尝试了许多变体并进行了很好的搜索.
<cfinvoke>显然是我想要的标签,遗憾的是,我不能在我的纯cfscript组件中使用它,因为它是在ColdFusion 10中实现的.
我在我的CFC中尝试了以下内容:
/** Validate the method name **/
var resources = getResources();
if (structKeyExists(variables.resources, name)) {
variables.resourceActive[name] = true;
var reflectionMethod = resources[name];
var result = "#reflectionMethod.getMethodName()#"(argumentCollection = params);
}
Run Code Online (Sandbox Code Playgroud)
返回值reflectionMethod.getMethodName()是我想要调用的方法名称.它100%返回正确定义和访问该方法的正确值(方法名称),
我的错误是该行的语法错误.
为了便于解释,我简化了这段代码.
我有一个cfm页面,用户点击表格行并获取ID.我想将该ID发送到CFC,在那里运行查询,并将结果返回到cfm页面.
这是JQuery的样子.
$.ajax({
url: "test.cfc?method=testFunction",
data: {ID:123456},
success: function(response) {
$("#div1").html(response);
}
});
Run Code Online (Sandbox Code Playgroud)
这就是cfc的样子.
<cfcomponent>
<cffunction name="testFunction" access="remote" returnType="query" returnFormat="JSON">
<cfquery name="testQuery" datasource="x">
Select ID, NAME
From Table
Where ID = '#url.ID#'
</cfquery>
<cfreturn testQuery>
</cffunction>
</cfcomponent>
Run Code Online (Sandbox Code Playgroud)
编辑 - 替代CFC方法
<cffunction name="testFunction" access="remote">
<cfquery name="testQuery" datasource="x">
Select ID, NAME
From Table
Where ID = '#url.ID#'
</cfquery>
<cfset response = [] />
<cfoutput query="testQuery">
<cfset obj = {
"ID" = ID,
"NAME" = NAME
} />
<cfset arrayAppend(response, obj) /> …Run Code Online (Sandbox Code Playgroud) 我正在尝试向cfc添加一个接口,其中包含cfml文件中的一些函数,但是它会抛出错误消息"component [...]没有实现接口的函数[..]"它的功能抱怨是在包含的cfml文件中实现的,我已经在railo 4和lucee 5中测试了这个并且在两者中都得到了相同的错误但它在coldfusion 11中有效吗?是否有人知道在lucee中是否有解决方法或修复此问题或railo?
下面是重现错误的示例代码.
interface {
public numeric function func() output="false";
}
Run Code Online (Sandbox Code Playgroud)
component implements="int" {
include "inc.cfm";
}
Run Code Online (Sandbox Code Playgroud)
<cfscript>
public numeric function func() output="false"{
return 2;
}
</cfscript>
Run Code Online (Sandbox Code Playgroud)
<cfscript>
cfc = createObject("component", "comp");
writedump(cfc.func());
</cfscript>
Run Code Online (Sandbox Code Playgroud) 我希望能够在CFML/LUCEE组件中动态编写一组getter和setter(没有硬编码的cfproperty标签).
<!--- MyComp.cfc --->
<cfcomponent displayname="MyComp" hint="MyComp" accessors="true">
<cffunction name="init">
<cfargument name="dynamicprops" type="array">
<cfloop array="#dynamicprops#" index="item">
<!---
Now what? I cannot do a cfsavecontent and write props here.
It demands cfproperty just after the cfcomponent begins. I
tried to do with closures but they are not acually setters
and getters. Does anyone know how to better do it?
--->
</cfloop>
</cffunction>
</cfcomponent>
<!--- example call --->
<cfset mc = CreateObject("component","MyComp").init( [{"name"="a","default"=1}] ) />
Run Code Online (Sandbox Code Playgroud)
然后我希望能够调用mc.setA(100)和mc.getA().但是没有发生. …
我创建了一个javascript对象
var spanglist = {
one: q1,
two:q2,
three:q3,
four: q4};
Run Code Online (Sandbox Code Playgroud)
我创建了一个ajax jquery对象来将数据发送到CFC:
$.ajax({
url: 'gridly/components/pay.cfc',
type:"POST",
dataType:' json',
data: {method: "structFromJSobjt",
returnFormat:"json",
jsStruct: spanglist}
});
Run Code Online (Sandbox Code Playgroud)
在我的cfc中,我有以下简单的代码:
<cffunction name="structFromJSobj" access="remote" output="false" >
<cfargument name="jsStruct" required="true" default="" />
<!--- AT this point I would like to work with the data contained in the jsStruct object. I can't access the data regardless of the typeI make the cfargument --->
</cffunction>
Run Code Online (Sandbox Code Playgroud)
一旦有人在数据中,有人可以指导我使用数据.
cfc ×10
coldfusion ×10
ajax ×2
jquery ×2
json ×2
lucee ×2
cfml ×1
coldfusion-9 ×1
components ×1
railo ×1
security ×1
var ×1
variables ×1
web-services ×1