JAVA6.0新特性_脚本js

<script>function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script>

/*

*下面是代码:拷贝然后参考上面图建立项目,可以运行,本人测试通过

*/

//PersonMain 类

package com.bobo;

public class PersonMain {
public static void main(String[] args) throws Exception {

Person person = new Person(18, 0);
String info = PersonJsEngineProcess.getInstance().getPersonaInfo(person);
System.out.printf(info);
}
}

//

package com.bobo;

import javax.script.*;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class PersonJsEngineProcess {

private static final String JS_NAME = "Person.js";

private static final String SCRIPT_NAME = "js";

private static final String FUNC_CAL_FROM_JS = "personInfo";

private ScriptEngine engine = null;

private Invocable invocableEngine = null;

public static PersonJsEngineProcess getInstance()
throws FileNotFoundException, ScriptException {
PersonJsEngineProcess pJsEgPr = new PersonJsEngineProcess();
pJsEgPr.init();
return pJsEgPr;
}
/*
* 初始化工作
* 构造一个JS执行Engine用来管理JS
*/
public void init() throws FileNotFoundException, ScriptException {
ScriptEngineManager engineMgr = new ScriptEngineManager();
engine = engineMgr.getEngineByName(SCRIPT_NAME);
try {
eval(engine, JS_NAME);
} catch (Exception e) {
e.printStackTrace();
}
invocableEngine = (Invocable) engine;
}
/*
* 根据JS的名称装在到JSengine
*/
private static void eval(ScriptEngine engine, String name) throws Exception {
InputStream is = PersonJsEngineProcess.class.getResourceAsStream(name);
engine.put(ScriptEngine.NAME, name);
engine.eval(new InputStreamReader(is));
}
/*
* 执行JS的FUNCTION
*/
public Object JsExcute(String jsFunctionName, Object o) throws Exception {
return invocableEngine.invokeFunction(FUNC_CAL_FROM_JS, o);
}

public String getPersonaInfo(Person person) throws Exception {
Object ret = JsExcute(FUNC_CAL_FROM_JS, person);
return (String) ret;
}

}
//

package com.bobo;

public class Person {
private int age;

private int sex;

public Person(int age, int sex) {
this.age = age;
this.sex = sex;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public int getSex() {
return sex;
}

public void setSex(int sex) {
this.sex = sex;
}

}
//Person.js


function personInfo(person) {
age = person.age;
sex = person.sex;
var returnString = "defalut";
if (age <= 18) {
if (sex == 1) {
returnString = "young boy";
} else {
returnString = "young girl";
}
} else {
if (sex == 1) {
returnString = "handsome an";
} else {
returnString = "beauty girl";
}
}
return returnString;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值