Chinaunix首页 | 论坛 | 博客
  • 博客访问: 470872
  • 博文数量: 66
  • 博客积分: 2125
  • 博客等级: 大尉
  • 技术积分: 704
  • 用 户 组: 普通用户
  • 注册时间: 2005-03-08 13:42
个人简介

空气中的尘埃

文章分类

全部博文(66)

文章存档

2019年(1)

2017年(4)

2016年(2)

2015年(4)

2014年(4)

2013年(2)

2012年(5)

2011年(6)

2010年(9)

2009年(8)

2008年(12)

2007年(6)

2006年(2)

2005年(1)

我的朋友

分类: Java

2008-05-08 16:56:15

字符串批量处理中,情况多种多样,要在编译代码中预测所有可能的情况比较困难. 采用filter,plugin 的方式可以大大简化代码编写量,同时提供方便的扩展性.
这里主要讨论java 中如何掉用javascript 以及二者之间的变量传递关系,有于时间关系只看以下String 类型的数据.
bsf
rhino 
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Administrator
 */
import org.apache.bsf.*;
import org.apache.bsf.util.*;
public class TestU {
    public static void main(String args[]){
  
        String sourcehead = "function dealparm(){" +
                " str = bsf.lookupBean('strparm');" +
                "return str+\"javascript return;\";}";
      try{
        BSFManager mgr = new BSFManager ();
        //设置    
        BSFEngine rhinoEngine = mgr.loadScriptingEngine ("javascript");       
        //rhinoEngine.eval(parm, arg1, arg2, args)
        rhinoEngine.eval ("javascript", 0, 0, sourcehead);
        mgr.registerBean("strparm", "testparm1");
        Object result = rhinoEngine.eval("javascript", 0, 0, "dealparm();");
        //输出显示
        System.out.println(result);
        mgr.registerBean("strparm", "testparm2");
        result = rhinoEngine.eval("javascript", 0, 0, "dealparm();");
        //输出显示
        System.out.println(result);
      }catch(Exception e){
        e.printStackTrace();
      }
     
    }
}
输出结果:
testparm1javascript return;
testparm2javascript return;
 
 
 
BSFEngine.eval 说明
public java.lang.Object eval(java.lang.String source,
                             int lineNo,
                             int columnNo,
                             java.lang.Object expr)
                      throws 
This is used by an application to evaluate an expression. The expression may be string or some other type, depending on the language. (For example, for BML it'll be an org.w3c.dom.Element object.)

Parameters:
source - (context info) the source of this expression (e.g., filename)
lineNo - (context info) the line number in source for expr
columnNo - (context info) the column number in source for expr
expr - the expression to evaluate
Throws:
- if anything goes wrong while eval'ing a BSFException is thrown. The reason indicates the problem.
 
阅读(1939) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~