将 Java 对象数组返回到 Coldfusion 组件

发布于 2024-11-08 01:43:08 字数 346 浏览 1 评论 0原文

在 Coldfusion 中创建 Java 对象相当简单:

variable = CreateObject("JAVA", "java.object").init(JavaCast("primitiveType", cfVar));

但是,如果 Java 方法返回 Java 对象列表,则使用 Java 返回类型就没那么简单了:

newVariable = variable.returnJavaObjectCollection();

是否有最佳实践? > 用于处理以数组或 ArrayCollection 形式返回的 Java 对象?

It is fairly straightforward to create Java objects in Coldfusion:

variable = CreateObject("JAVA", "java.object").init(JavaCast("primitiveType", cfVar));

However, it isn't as straightforward to work with Java return types if, say, a Java method returns a list of Java objects:

newVariable = variable.returnJavaObjectCollection();

Is there a best practice for working with Java objects that are returned in an array or an ArrayCollection?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枕花眠 2024-11-15 01:43:08

如果我认为我正确理解了你的问题(抱歉,你不是 100% 清楚) - 你应该能够在 ColdFusion 中调用 Java 对象的方法,就像在 Java 中通常调用的方法一样。

例如,您可以调用 java.lang 上的方法。 String 对象,在 ColdFusion 中处理 String 时。

所以没有什么问题:

<cfscript>
myString = JavaCast("string", "FooBar"); //definitely a String object.
</cfscript>

<cfoutput>    
String length: #myString.length()#
Starts with 'Foo': #myString.startsWith("Foo")#
Upper case string: myString.toUpperCase()#
</cfoutput>

如果您正在处理 java.util.List 的实例,您会发现 9/10 次,ColdFusion 的本机数组函数可以很好地工作 - 因为 ColdFusion 数组实际上是 java.util 的实现.列表也是如此。

所以应该仍然有效,ArrayAppend()、ArrayContains() 等都应该按预期工作。

如果失败,您仍然可以访问 List 的底层 API:
http://download.oracle.com/javase/6 /docs/api/java/util/List.html

但要记住的是,一切都从 0 开始,而不是索引 1。

因此,要获取 Java 列表中的第一个值,应该是

myItem = myList.get(0);

:比CF方式:

myItem = myList[1];

否则,这就是它的真正意义。

您可能应该注意到,直接调用 Java 方法的开销很小,因为它是使用 Reflection,因此,如果您可以使用原生 CF 函数,那通常会更好,但除了原生交互之外,您没有其他办法。

有关详细信息,您可能需要尝试ColdFusion Java 文档

If I think I understand your question correctly (sorry, you aren't 100% clear) - you should be able to call methods on Java objects in ColdFusion, much as you would normally within Java.

For example, you can call methods on a java.lang.String object, when dealing with a String in ColdFusion.

So there is nothing wrong with:

<cfscript>
myString = JavaCast("string", "FooBar"); //definitely a String object.
</cfscript>

<cfoutput>    
String length: #myString.length()#
Starts with 'Foo': #myString.startsWith("Foo")#
Upper case string: myString.toUpperCase()#
</cfoutput>

If you are dealing with an instance of java.util.List, you will find that 9/10 times, ColdFusion's native array functions will work just fine with - as ColdFusion Arrays are actually implementations of java.util.List as well.

So <cfloop array="#foo#" ...> should still work, ArrayAppend(), ArrayContains(), etc should all work as expected.

Failing that, you still have access to the underlying API for List:
http://download.oracle.com/javase/6/docs/api/java/util/List.html

But the thing to remember, is that everything is from 0, rather than an index of 1.

So to get the first value in a Java List would be:

myItem = myList.get(0);

Rather than the CF way of:

myItem = myList[1];

Otherwise, that's really about it.

You should probably note that there is a small overhead on calling Java methods directly, as it is done on the fly using Reflection, so if you can use the native CF functions instead, that is usually better, but something you have no other recourse but to interact natively.

For more information you may want to try the ColdFusion Java Documentation

只等公子 2024-11-15 01:43:08

如果您返回一个 Vector,它将与 ColdFusion 的数组实用程序一起使用,并且 HashMap 将与 ColdFusion 的 Struct 实用程序一起使用。但需要注意的是:

ColdFusion 中没有定义 null 对象,因此如果数组中的元素为 null,或者映射中的值为 null,或者您只是返回 null,则它们各自的变量将是未定义的。

请记住,您仍然可以在 ColdFusion 中调用复杂 java 对象上的 java 方法——包括除向量和映射之外的复杂集合上的方法。例如:

<cfset iterator = myJavaObj.myJavaFuncReturnsCollection().iterator() />
<cfloop condition="iterator.hasNext()">
    <cfset currObj = iterator.next() />
    <cfset currObj.myFunction() />
</cfloop>

If your return a Vector<Object> it will work with ColdFusion's array utilities, and a HashMap<String,Object> will work with ColdFusion's Struct utilities. A few notes though:

null objects are not defined in ColdFusion, so if an element in the array is null, or a value in the map is null, or you just return null, their respective variables will be undefined.

Keep in mind that you can still call java methods on complex java objects in ColdFusion--including methods on sophisticated collections besides Vectors and Maps. For example:

<cfset iterator = myJavaObj.myJavaFuncReturnsCollection().iterator() />
<cfloop condition="iterator.hasNext()">
    <cfset currObj = iterator.next() />
    <cfset currObj.myFunction() />
</cfloop>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文