Skip navigation
ik1hsr
Currently Being Moderated

Casting Cf struct in Hashtable Java

May 30, 2007 1:00 AM

Hi;
i need to convert a Coldfusion Struct in a Java Hashtable (as th CFMX manual tell);
I have this java class:


public class Pratica {
private java.util.Hashtable my;
public Pratica(){
my=null;
}
public Pratica(java.util.Hashtable dbprop){
my=dbprop;
}
public void set(java.util.Hashtable dbprop){
my=dbprop;
}

}

and in the CF page i have this code:

<cfset ma = createObject("java","java.util.Hashtable")>
<cfset ma.init(glb)> // glb is a struct

<cfobject action="CREATE" type="JAVA" class="Pratica" name="pra">
<cfset pra.init(ma)>

but when i run...:
"Unable to find a constructor for class Pratica that accepts parameters of type ( java.util.Hashtable )."

I'm waiting your suggestions!!!
thanks to all


Andrea
ps:sorry for my english.
 
Replies
  • Currently Being Moderated
    May 30, 2007 6:28 AM   in reply to ik1hsr
    There does not seem to be a constructur for Hashtable that accepts a CF structure as a parameter. Take a look at the java documentation:

    http://java.sun.com/j2se/1.4.2/docs/api/java/util/Hashtable.html#Hasht able(int,%20float)

    Given, I haven't tried to go from CF struct to hashtable before - could you do something like this?

    1) Initialize an empty Hashtable
    <cfset myHashTable= createObject("java","java.util.Hashtable")>
    <cfset myHashTable.init()>

    2) loop over the CF structure collection and call the Hashtable's put() method to populate the hashtable
    <cfloop collection="#glb#" item="key">
    <cfset myHashTable.put(key, glb[key])>
    </cfloop>
     
    |
    Mark as:
  • Currently Being Moderated
    Jun 6, 2007 4:44 PM   in reply to ik1hsr
    ik1hsr,

    In MX6/7 a ColdFusion Struct is a Hashtable. So you don't need to create a new one. Just pass in the CF Struct object.

    Java Class:

    public class Pratica {
    private java.util.Hashtable my;
    public Pratica(){
    my=null;
    }
    public Pratica(java.util.Hashtable dbprop){
    my=dbprop;
    }
    public void set(java.util.Hashtable dbprop){
    my=dbprop;
    }

    public String getKeyValue(Object key) {
    if (my != null) {
    return my.get(key).toString();
    }
    return "";
    }
    }


    ColdFusion Code:

    <cfset theKey = "abc" />
    <cfset cfStruct = structNew()>
    <cfset cfStruct[theKey] = "abc" />
    <cfset pra = createObject("java", "Pratica").init(cfStruct) />
    <cfoutput>#theKey# value = #pra.getKeyValue(theKey)#</cfoutput>
     
    |
    Mark as:
  • Currently Being Moderated
    Apr 3, 2012 6:10 AM   in reply to cf_dev2

    cf_dev2, Thanks for that!  Picky syntax was holding me up 'till I found your specific example, so thanks!

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points