Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to convert java object in a request scope to javascript object???

 
ss kumar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
I have a java object in some scope and want to convert it into a client side object.
Consider a senario. I have a dropdown list and when I change the select a option other attributes should change depending on the option I selected. Presently when I do this selection it goes to the server and gets back the new corresponding values and displays them. Here there is a call to the server side. But I don't want to make a call to the server each time when I select. want to handle it in one request to the server. Get a whole Object(java object) to the client side and convert it to a javascript object and use it.
Hope the question is clear
Thanks for your time.
cheers,
shoban
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you want to do is look for a double commbo script in JavaScript
basically you have to write out all of the options for the drop down in a 2D array when you load the page. The javascript will take care of loading the second field based on the first...
You can get the basic idea of the JavaScript code here...
http://www.javascriptkit.com/script/cut183.shtml

Eric
 
Yuriy Fuksenko
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I just thought that might be interesting.
Reading this post I got an idea and wrote a class, that uses reflection to create JavaScript object out of Java object.
For example for the following class:
class test{
public String s = "testString";
public String nulS = null;
public int tr = 99;
private ArrayList dates = new ArrayList();
public long[] t = new long[]{1L,2L};
public ArrayList getDates() {
if(dates.size() == 0){
for(int i=0;i<2;i++){
dates.add(new Date());
}
}
return dates;
}
}
it creates:
testObj = new Array();
testObj['s']='testString';
testObj['nulS'] = null;
testObj['tr']= 99.0;
testObj['t'] = new Array();
testObj['t'][0]= 1.0;
testObj['t'][1]= 2.0;
testObj['getDates'] = new Array();
testObj['getDates'][0]= new Date(1078439971890);
testObj['getDates'][1]= new Date(1078439971890);
method main the class below contains a test - you don't really need it.
This class will transform all public fields if they are not defined in java.lang.Object.
It will transform public method, if method have return type other than void, don't have parameters and not defined in java.lang.Object.
Both methods and fields are transformed into fields.(see 'getDates' in the example).
Here is the source code:
package com;
import java.util.Collection;
import java.util.Iterator;
import java.util.Date;
import java.util.ArrayList;
import java.lang.reflect.*;
/**
* User: yfuksenk
* Date: Mar 4, 2004
* Time: 3:35:14 PM
*/
public class JavaToScript {
public static void main(String[] args){
class test{
public String s = "testString";
public String nulS = null;
public int tr = 99;
private ArrayList dates = new ArrayList();
public long[] t = new long[]{1L,2L};
public ArrayList getDates() {
if(dates.size() == 0){
for(int i=0;i<2;i++){
dates.add(new Date());
}
}
return dates;
}
public void setDates(ArrayList dates) {
this.dates = dates;
}
}
test obj = new test();
Method t = null;
try {
t = obj.getClass().getMethod("notify",null);
System.out.println(t.getReturnType().getName());
} catch (NoSuchMethodException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
System.out.println(getJavaScriptObject("testObj", obj));
}
public static final String getJavaScriptObject(String jsName,Object obj){
if(obj == null) return jsName + " = null;\n";
String result = null;
String declClass = null;
Boolean definesToString = new Boolean(false);
try {
Method method = obj.getClass().getDeclaredMethod("toString",null);
declClass = method.getDeclaringClass().getName();
//System.out.println("Found toString " + aClass.getName());
definesToString = new Boolean( method != null
&& Modifier.isPublic(method.getModifiers()));
} catch (NoSuchMethodException e) {}
if(obj instanceof Collection){
Collection cl = (Collection) obj;
StringBuffer sb = new StringBuffer("\n" + jsName + " = new Array();");
int j = 0;
for (Iterator iterator = cl.iterator(); iterator.hasNext() {
Object o = (Object) iterator.next();
sb.append(getJavaScriptObject("\n" + jsName + "[" + j + "]", o));
j++;
}
result = sb.toString();
}
else if(obj.getClass().isArray()){
StringBuffer sb = new StringBuffer("\n" + jsName + " = new Array();");
int length = Array.getLength(obj);
for (int i = 0; i < length; i++) {
sb.append(getJavaScriptObject("\n" + jsName + "[" + i + "]", Array.get(obj,i)));
}
result = sb.toString();
}
else if(obj.getClass().getName().equals("java.util.Date")){
result = "\n" + jsName + "= new Date("+ ((Date) obj).getTime() + ");\n";
}
else if(obj instanceof Number){
result = "\n" + jsName + "= " + ((Number) obj).doubleValue() + ";\n";
}
else if(obj instanceof Boolean){
result = "\n" + jsName + "= " + ((Boolean) obj).booleanValue() + ";\n";
}
else if(definesToString.booleanValue()){
result = "\n" + jsName + "='" + obj.toString().replaceAll("\'","\\\'").replaceAll("\n","\\n") + "';\n";
}
else{
StringBuffer sb = getJsObject(jsName, obj);
result = sb.toString();
}
return result;
}
private static StringBuffer getJsObject(String jsName, Object obj) {
Class aClass = obj.getClass();
StringBuffer sb = new StringBuffer("\n" + jsName + " = new Array();");
Field[] fields = aClass.getFields();
for (int f = 0; f < fields.length; f++) {
Field field = fields[f];
if(!Modifier.isPublic(field.getModifiers()) || field.getDeclaringClass().getName().equals("java.lang.Object")) continue;
try {
String jScript = getJavaScriptObject(jsName+"['" + field.getName() + "']", field.get(obj));
sb.append(jScript);
} catch (IllegalAccessException e) { }
}
Method[] methods = aClass.getMethods();
for (int m = 0; m < methods.length; m++) {
Method method = methods[m];
if(!Modifier.isPublic(method.getModifiers())
|| method.getReturnType().equals("void")
|| method.getParameterTypes().length > 0
|| method.getDeclaringClass().getName().equals("java.lang.Object")) continue;
String jScript = null;
try {
Object methodCallResult = method.invoke(obj,null);
jScript = getJavaScriptObject(jsName+"['" + method.getName() + "']", methodCallResult);
sb.append(jScript);
} catch (IllegalAccessException e) {} catch (InvocationTargetException e) {}
}
return sb;
}
}
 
ss kumar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Yuriy, Thanks a lot for the code and your time. Let me see if I can make use of this logic.
cheers,
shoban
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic