ColdFusion and JSoup - addTags not found

I am trying to use JSoup with ColdFusion to clear some HTML, but I see the following error:

AddTags method 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 reliably decrypt. ColdFusion found 0 methods that match the arguments provided. If it is a Java object and you have confirmed that the method exists, use the javacast function to reduce ambiguity.

My code is as follows:

<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"))> 

I reset the Whitelist object and the add label method is added. If I remove the addTags () method and use one of JSoup's standard whitelists, such as basic (), none () or relaxed (), then the code works fine. As far as I can see from other online examples, this is the correct syntax for using the addTags () method.

I'm new to using Java objects in ColdFusion, so it puzzled me.

Any help would be greatly appreciated.

Thanks Michael.

+4
source share
1 answer

The addTags method expects an array of strings, not just a single string. First put the value in an array:

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

Edit:

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

To clarify, this is the correct syntax - for java. In java, you can pass a variable number of arguments using either an array or this syntax: addTags("tag1", "tag2", ...) . However, CF only supports array syntax. Therefore, if you are a cfdump java object, you will see square brackets after the class name, which indicates that the argument is an array:

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

Source: https://habr.com/ru/post/1433399/


All Articles