找不到ColdFusion Java方法异常
这个问题快把我逼疯了。我试图在ColdFusion中使用Java来创建一个文档对象。当我这样做时:
nd = createObject("java","javax.xml.parsers.DocumentBuilder");
我可以转储nd并查看它是否正确加载了所有方法:
object of javax.xml.parsers.DocumentBuilder Class Name javax.xml.parsers.DocumentBuilder
Method / Return Type
getDOMImplementation() / org.w3c.dom.DOMImplementation
getSchema() / javax.xml.validation.Schema
isNamespaceAware() / boolean
isValidating() / boolean
isXIncludeAware() / boolean
newDocument() / org.w3c.dom.Document
parse(java.io.File) / org.w3c.dom.Document
parse(java.lang.String) / org.w3c.dom.Document
parse(org.xml.sax.InputSource) / org.w3c.dom.Document
parse(java.io.InputStream, java.lang.String) / org.w3c.dom.Document
parse(java.io.InputStream) / org.w3c.dom.Document
reset() / void
setEntityResolver(org.xml.sax.EntityResolver) / void
setErrorHandler(org.xml.sax.ErrorHandler) / void
我正在尝试调用newDocument()方法。我已经在cfscript和cfset中尝试了以下所有操作:
nd.newDocument();
nd.newDocument(JavaCast("null",""));
nd = createObject("java","javax.xml.parsers.DocumentBuilder").newDocument();
nd = createObject("java","javax.xml.parsers.DocumentBuilder").newDocument(JavaCast("null",""));
但是,无论我尝试哪种方法,我都会出现以下错误:
Either there are no methods with the specified method name and argument types or the isNamespaceAware method is overloaded with argument types that ColdFusion cannot decipher reliably. ColdFusion found 0 methods that match the > provided arguments. If this is a Java object and you verified that the method > exists, use the javacast function to reduce ambiguity.
我可以看到该方法已加载。。该方法没有重载。。它不需要任何参数。。而且,即使我显式地告诉CF我正在传递null,它也找不到方法
我尝试访问类中的其他方法,但它也找不到这些方法。。我不确定为什么我可以转储类的内容,而且我可以看到所有的方法。。但是,不知怎的,当我试着给他们打电话时,CF被弄糊涂了,找不到他们
任何想法都会非常有用
谢谢
# 1 楼答案
我在java类上调用ColdFusion的方法时遇到了同样的错误。我试图使用setPropertyName方法,如下所示
事实证明,ColdFusion希望您直接访问EJB中的属性。您还需要像下面这样显式地调用EJB构造函数:
CF隐式地为您调用set方法,如Adobe Docs中所述。你不能直接叫它
# 2 楼答案
您必须为documentBuilder factory创建一个对象。在factory的帮助下,您可以获得真实的xml信息。在这里,我创建了这个对象,并使用documentbuilderfactory调用了parse方法。 此外,您还必须注入一个newInstance(),然后只有您才能访问newdocument()方法。 我的Xml内容:testParse。xml
*CF代码:
我希望这对你有帮助。谢谢