专注于Jsp开发,为Jsp开发提供源动力 VM主机| 海外空间| 郑州网站建设| 郑州网络公司| 洛阳网站建设
jsp空间

Java调用javascript

添加时间:[2010-8-30 17:46:23] 

 package co.test;

  import java.io.FileReader;

  import java.io.LineNumberReader;

  import org.mozilla.javascript.Context;

  import org.mozilla.javascript.Function;

  import org.mozilla.javascript.Scriptable;

  public class JSExploration

  {

  private Context cx;

  private Scriptable scope;

  public JSExploration()

  {

  this.cx = Context.enter();

  this.scope = cx.initStandardObjects();

  }

  public Object runJavaScript(String filename)

  {

  String jsContent = this.getJsContent(filename);

  Object result = cx.evaluateString(scope, jsContent, filename, 1, null);

  return result;

  }

  private String getJsContent(String filename)

  {

  LineNumberReader reader;

  try

  {

  reader = new LineNumberReader(new FileReader(filename));

  String s = null;

  StringBuffer sb = new StringBuffer();

  while ((s = reader.readLine()) != null)

  {

  sb.append(s).append("\n");

  }

  return sb.toString();

  }

  catch (Exception e)

  {

  // TODO Auto-generated catch block

  e.printStackTrace();

  return null;

  }

  }

  public Scriptable getScope()

  {

  return scope;

  }

  public static void main(String[] args)

  {

  String filename = System.getProperty("user.dir") + "/jsmap.js";

  JSExploration jsExploration = new JSExploration();

  Object result = jsExploration.runJavaScript(filename);

  Scriptable scope = jsExploration.getScope();

  Function sum = (Function) scope.get("sum", scope);

  Function isPrime = (Function)sum.call(Context.getCurrentContext(), scope, sum, new Object[] {2,8});

  Object ss = isPrime.call(Context.getCurrentContext(), sum, isPrime, new Object[] {2,8});

  System.out.println(Context.toString(ss));

  }

  }

  试验了一个java 调用 javascript 的例子,如果把jsmap.js中的与this 有关的代码注销的话程序就可以正常运行。不住销掉的话就会报个运行时错误。。。

  js 代码如下(有关this 的代码已注销):

  function  sum(x, y) {

  // this.formulaeObject = null;

  // this.formulaeObject["vager"] = function (c, d) {

  // return (c + d)/2;

  // };

  var vager = 1000;

  return function (x,y){return x + y + vager;} ;

  }

    栏目导航:
    最近更新:
    点击排行:
Java调用javascript
作者无:   加入时间:2010-8-30 17:46:23   点击次数:401

 package co.test;

  import java.io.FileReader;

  import java.io.LineNumberReader;

  import org.mozilla.javascript.Context;

  import org.mozilla.javascript.Function;

  import org.mozilla.javascript.Scriptable;

  public class JSExploration

  {

  private Context cx;

  private Scriptable scope;

  public JSExploration()

  {

  this.cx = Context.enter();

  this.scope = cx.initStandardObjects();

  }

  public Object runJavaScript(String filename)

  {

  String jsContent = this.getJsContent(filename);

  Object result = cx.evaluateString(scope, jsContent, filename, 1, null);

  return result;

  }

  private String getJsContent(String filename)

  {

  LineNumberReader reader;

  try

  {

  reader = new LineNumberReader(new FileReader(filename));

  String s = null;

  StringBuffer sb = new StringBuffer();

  while ((s = reader.readLine()) != null)

  {

  sb.append(s).append("\n");

  }

  return sb.toString();

  }

  catch (Exception e)

  {

  // TODO Auto-generated catch block

  e.printStackTrace();

  return null;

  }

  }

  public Scriptable getScope()

  {

  return scope;

  }

  public static void main(String[] args)

  {

  String filename = System.getProperty("user.dir") + "/jsmap.js";

  JSExploration jsExploration = new JSExploration();

  Object result = jsExploration.runJavaScript(filename);

  Scriptable scope = jsExploration.getScope();

  Function sum = (Function) scope.get("sum", scope);

  Function isPrime = (Function)sum.call(Context.getCurrentContext(), scope, sum, new Object[] {2,8});

  Object ss = isPrime.call(Context.getCurrentContext(), sum, isPrime, new Object[] {2,8});

  System.out.println(Context.toString(ss));

  }

  }

  试验了一个java 调用 javascript 的例子,如果把jsmap.js中的与this 有关的代码注销的话程序就可以正常运行。不住销掉的话就会报个运行时错误。。。

  js 代码如下(有关this 的代码已注销):

  function  sum(x, y) {

  // this.formulaeObject = null;

  // this.formulaeObject["vager"] = function (c, d) {

  // return (c + d)/2;

  // };

  var vager = 1000;

  return function (x,y){return x + y + vager;} ;

  }



上一篇:

关于我们 | 付款方式 | 客户管理 | 网站导航 | 友情连接


版权所有 2008 三易网络(洛阳)科技开发有限公司 京ICP备06012028号

服务热线:0371-63653120 63658758(郑州) 0379-63921200   63265368(洛阳)

QQ在线客服: JSP空间咨询   JSP空间咨询    Email:web@suneasy.cn

郑州网络公司 郑州网站建设 洛阳网站建设

总部地址:纱厂南路41号中泰新城泰福苑803室 郑州分公司地址:金水区圣菲城