2012-09-10 23 views
4

我想使用JSoup使用ColdFusion清理一些HTML,但我遇到了以下錯誤:ColdFusion和JSoup - 該addTags方法未找到錯誤

The addTags method was not found. Either there are no methods with the specified method name and argument types or the addTags 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.

我的代碼如下:

<cfset jsoup = createObject('java','org.jsoup.Jsoup')> 
<cfset Whitelist = createObject("java", "org.jsoup.safety.Whitelist")> 

<cfset parsedhtml = jsoup.parse(form.contentrichtext)> 
<cfset post = parsedhtml.body().html()> 
<cfset post = jsoup.clean(post, Whitelist.none().addTags("span"))> 

我已經拋出了白名單對象,並且添加了標記方法。如果我刪除addTags()方法並使用諸如basic(),none()或relaxed()的標準JSoup白名單之一,那麼代碼將完美運行。據我可以從其他在線示例中看到,這是使用addTags()方法的正確語法。

我對在ColdFusion中使用Java對象相當陌生,所以這讓我很難過。

任何幫助將不勝感激。

謝謝, 邁克爾。

+0

也正好說了,我不知道該如何的範圍內實現JavaCast功能我代碼和這是否會有所幫助。 – Michael

回答

6

addTags方法需要一個字符串數組,而不僅僅是一個字符串。把值到一個數組第一:

<!--- create a CF array then cast it as type string[] ---> 
<cfset tagArray = javacast("string[]", ["span"]) > 
<cfset post = jsoup.clean(post, Whitelist.none().addTags(tagArray))> 

編輯:

As far as I can see from other online examples this is the correct syntax

爲了澄清,其正確的語法 - 爲Java。在Java中,您可以使用數組或此語法傳入variable number of argumentsaddTags("tag1", "tag2", ...)。但是,CF僅支持數組語法。所以,如果你cfdump的Java對象,你會看到方括號後的類名,這表明該參數是一個數組:

 method: addTags(java.lang.String[]) // array of strings 
+0

工作魅力。感謝您的明確解釋! – Michael

+0

歡迎:)很高興幫助。 – Leigh