The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Design
> JavaScript Development
|
Java Classes to Javascript Object
Discuss Java Classes to Javascript Object in the JavaScript Development forum on Dev Shed. Java Classes to Javascript Object JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 26th, 2012, 09:28 PM
|
|
Contributing User
|
|
Join Date: Nov 2003
Posts: 850
 
Time spent in forums: 1 Week 6 h 16 m 50 sec
Reputation Power: 10
|
|
|
Java Classes to Javascript Object
I have the following class hierarchy
Abstract class
Code:
public abstract class AbstractClass
{
private long timestamp;
private String candidateId;
private String regId;
}
And I have multiple classes extending above abstract class as below in my java project
Code:
public class QuestionClass extends AbstractClass
{
private boolean questionanswered;
private boolean valid;
public QuestionClass(boolean questionanswered)
{
this.questionanswered = questionanswered;
}
public boolean isQuestionanswered()
{
return questionanswered;
}
}
Now, I need to send from the browser the similar javascript objects(as json) to server (and on server I convert json to java object)
For instance:
{"candidateid":"1243","regId":"78","valid":"true","timestamp":"0","questionanswered":"true"}
Is there anyway to dynamically create the javascript objects from java and get all the parent classes fields too
If I can get javascript object from server java, then I can directly convert it to JSON while sending it back to server
Right now, I have to manually code the similar javascript object
Thanks
__________________
Contact info:
Primary email: advanced.programmer@gmail.com
MSN/email: superprg@hotmail.com
AIM: superprg
|

October 29th, 2012, 09:56 AM
|
|
Contributing User
|
|
Join Date: Nov 2003
Posts: 850
 
Time spent in forums: 1 Week 6 h 16 m 50 sec
Reputation Power: 10
|
|
|
Anyone?
|

October 29th, 2012, 12:17 PM
|
|
|
|
There are solutions to your question but this seems to be a very peculiar way of going about things. Why are you using this method to deploy Javascript?
|

October 29th, 2012, 04:12 PM
|
|
Contributing User
|
|
Join Date: Nov 2003
Posts: 850
 
Time spent in forums: 1 Week 6 h 16 m 50 sec
Reputation Power: 10
|
|
|
The reason is as I mentioned, I am using the same object at javascript end to monitor and get my related data in browser
|

October 29th, 2012, 06:12 PM
|
|
|
|
I understand why you are using the same Javascript as Java. I am asking why you are deploying it via a JSON object. To put it another way, why are you not simply printing the Javascript into the document?
|

November 12th, 2012, 06:55 AM
|
|
Contributing User
|
|
Join Date: Nov 2003
Posts: 850
 
Time spent in forums: 1 Week 6 h 16 m 50 sec
Reputation Power: 10
|
|
|
Sorry for the late response because of Sandy and other things..
Let me explain more
I do have the javascript objects in the client code(which I created by manually making it similar to the java classes)
Json is coming in picture only while sending the data from browser to server(I can send in xml or json format and I preferred json since at server it was easy to convert it to java object)
My question is, given 80 such java classes, is there any way to convert these classes to javascript objects?(More importantly, it would be good if this is possible at runtime too)
|

November 15th, 2012, 06:47 AM
|
|
|
Ok, I'll start off by saying that yes you can execute data as code by using the eval function. It is however extremely dangerous to allow that level of functionality on a client-side application.
Another option is to create a file with the generated Javascript in and then insert the file into the document, as an active Javascript source.
I'm still not clear on this. Is the code dynamic and therefore requires generation? Or is it only the data that is dynamic? If it is only the data then there is no need to send over the code also, at least not after the document has loaded.
Can you explain the flow of this application and the requirement for transferring functionality after document load?
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|