好的家伙来自这个帖子Rhino Evaluating a javascript object in Java我试图以不同的方式评估javascript。即没有defineClass,也没有正确评估Person对象。
有人可以帮帮我吗?
工作代码如下,
的Javascript
importPackage(Packages.com.testpkg);
defineClass("com.testpkg.Person");
var parser = new MyParser();
var myObject = new Person();
myObject.string1 = "Hello";
myObject.string2 = "World";
myObject.id = 1;
myObject.string4 = "Testing";
myObject.string3 = "Another Test";
try
{
var returnStr = parser.PrintObj(myObject);
}
catch (e)
{
print("An error occured");
}
人员类
public class Person extends ScriptableObject {
private int id;
private String string1;
private String string2;
private String string3;
public Person() {
System.out.println("Person: Person Constructor");
}
public void jsConstructor() {
System.out.println("Person: jsConstructor");
this.string1 = "";
this.string2 = "";
this.id = 0;
}
public int getID() {
return this.id;
}
public void jsSet_id(int value) {
this.id = value;
}
public int jsGet_id() {
return this.id;
}
public String jsGet_string1() {
return this.string1;
}
public void jsSet_string1(String value) {
this.string1 = value;
}
public String jsGet_string2() {
return this.string2;
}
public void jsSet_string2(String value) {
this.string2 = value;
}
public String jsGet_string3() {
return this.string3;
}
public void jsSet_string3(String value) {
this.string3 = value;
}
@Override
public String toString() {
return id + " " + string1 + " " + string2 + " " + string3;
}
@Override
public String getClassName() {
return "Person";
}
}
MyParser类
public class MyParser {
public MyParser() {
System.out.println("Entered constructor");
}
public String PrintObj(ScriptableObject obj) {
Person pObj = (Person)Context.jsToJava(obj, Person.class);
System.out.println("Printing p: " + pObj);
return null;
}
}
输出是,
Entered constructor
Person: Person Constructor
Person: jsConstructor
Printing p: 1 Hello World Another Test
现在的问题是我摆脱了defineClass。在使用(Rhapsody健康产品)的应用程序中实现的Rhino版本没有定义此方法。因此,我无法使用它。我使用另一种方法来加载类Person。如果我摆脱了defineClass,输出将是,
Entered constructor
Person: Person Constructor
Printing p: 0 null null null
请帮忙!
答案 0 :(得分:0)
按照以下步骤修正了它。
Javascript(TestScript.js)
Person = com.testpkg.Person;
MyParser = com.testpkg.MyParser;
var myObject = Person.getInstance("myObject");
myObject.id = 1;
myObject.string1 = "Hello";
myObject.string2 = "World";
myObject.string3 = "Another Test";
myObject.string4 = "Testing";
try
{
var parser = new MyParser();
var returnStr = parser.PrintObj(myObject);
}
catch (e)
{
print("An error occured");
}
人员类
package com.testpkg;
public class Person extends ScriptableObject {
private int id;
private String string1;
private String string2;
private String string3;
public Person() {
}
public void jsConstructor() {
System.out.println("Person: jsConstructor");
this.id = 0;
this.string1 = "";
this.string2 = "";
this.string3 = "";
}
public int jsGet_id() {
return this.id;
}
public void jsSet_id(int value) {
this.id = value;
}
public String jsGet_string1() {
return this.string1;
}
public void jsSet_string1(String value) {
this.string1 = value;
}
public String jsGet_string2() {
return this.string2;
}
public void jsSet_string2(String value) {
this.string2 = value;
}
public String jsGet_string3() {
return this.string3;
}
public void jsSet_string3(String value) {
this.string3 = value;
}
@Override
public String toString() {
return id + " " + string1 + " " + string2 + " " + string3;
}
@Override
public String getClassName() {
return "Person";
}
public static Scriptable getInstance(String objectName) throws IllegalAccessException, InstantiationException, InvocationTargetException {
Context jsContext = Context.enter();
Scriptable person = null;
try {
ScriptableObject rootScope = jsContext.initStandardObjects();
ScriptableObject.defineClass(rootScope, Person.class);
person = jsContext.newObject(rootScope, "Person", null);
rootScope.put(objectName, rootScope, person);
}
finally {
Context.exit();
}
return person;
}
}
MyParser类
package com.testpkg;
public class MyParser {
public MyParser() {
System.out.println("Entered MyParser constructor");
}
public String PrintObj(ScriptableObject obj) {
System.out.println("Printing person: " + obj);
return null;
}
}
所以当编译执行时如此,
java -cp js.jar;MyParser.jar org.mozilla.javascript.tools.shell.Main TestScript.js
应显示以下输出
Person: jsConstructor
Entered MyParser constructor
Printing person: Person 1 Hello World Another Test
应该注意这里安全地忽略了string4!