java调用请求页面js方法有哪些
2个回答
展开全部
其实网上有很多方法
/** 加载脚本引擎,并在java中调用js方法 */
public void test2(){
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
try {
String str="2&1";
Double d = (Double) engine.eval(str);
Integer i=d.intValue();
System.out.println(i);
} catch (ScriptException ex) {
ex.printStackTrace();
} }
/*
* 在java中调用js,jdk1.6中有加载js引擎类,然后由它来调用js方法。
* 并通过JDK平台给script的方法中的形参赋值
*/
public void test(String name)
{
ScriptEngineManager sem = new ScriptEngineManager();
/**sem.getEngineByExtension(String extension)参数为js
sem.getEngineByMimeType(String mimeType) 参数为application/javascript 或者text/javascript
sem.getEngineByName(String shortName)参数为js或javascript或JavaScript
*/
ScriptEngine se = sem.getEngineByName("js");
try
{
String script = "function say(){ return 'hello,'"+name+"; }";
se.eval(script);
Invocable inv2 = (Invocable) se;
String res=(String)inv2.invokeFunction("say",name);
System.out.println(res);
}
catch(Exception e)
{
e.printStackTrace();
}
}
/** 演示如何将java中对象作为js中全局变量,同时演示将file类赋给脚本语言,并获得其属性。
*/
public void testScriptVariables()
{
ScriptEngineManager sem=new ScriptEngineManager();
ScriptEngine engine=sem.getEngineByName("js");
File file=new File("c:\\1.txt");
engine.put("f", file);
try {
engine.eval("println('path:'+f.getPath())");//无法使用alert方法
} catch (ScriptException e) {
e.printStackTrace();
}
}
/*
* 演示如何在java中如何通过线程来启动一个js方法
*/
public void testScriptInterface() throws ScriptException
{
ScriptEngineManager sem=new ScriptEngineManager();
ScriptEngine engine=sem.getEngineByName("js");
String script="var obj=new Object();obj.run=function(){println('test thread')}";
engine.eval(script);
Object obj=engine.get("obj");//获取js中对象
Invocable inv=(Invocable)engine;
Runnable r=inv.getInterface(obj,Runnable.class);
Thread t=new Thread(r);
t.start();
}
/** 加载脚本引擎,并在java中调用js方法 */
public void test2(){
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
try {
String str="2&1";
Double d = (Double) engine.eval(str);
Integer i=d.intValue();
System.out.println(i);
} catch (ScriptException ex) {
ex.printStackTrace();
} }
/*
* 在java中调用js,jdk1.6中有加载js引擎类,然后由它来调用js方法。
* 并通过JDK平台给script的方法中的形参赋值
*/
public void test(String name)
{
ScriptEngineManager sem = new ScriptEngineManager();
/**sem.getEngineByExtension(String extension)参数为js
sem.getEngineByMimeType(String mimeType) 参数为application/javascript 或者text/javascript
sem.getEngineByName(String shortName)参数为js或javascript或JavaScript
*/
ScriptEngine se = sem.getEngineByName("js");
try
{
String script = "function say(){ return 'hello,'"+name+"; }";
se.eval(script);
Invocable inv2 = (Invocable) se;
String res=(String)inv2.invokeFunction("say",name);
System.out.println(res);
}
catch(Exception e)
{
e.printStackTrace();
}
}
/** 演示如何将java中对象作为js中全局变量,同时演示将file类赋给脚本语言,并获得其属性。
*/
public void testScriptVariables()
{
ScriptEngineManager sem=new ScriptEngineManager();
ScriptEngine engine=sem.getEngineByName("js");
File file=new File("c:\\1.txt");
engine.put("f", file);
try {
engine.eval("println('path:'+f.getPath())");//无法使用alert方法
} catch (ScriptException e) {
e.printStackTrace();
}
}
/*
* 演示如何在java中如何通过线程来启动一个js方法
*/
public void testScriptInterface() throws ScriptException
{
ScriptEngineManager sem=new ScriptEngineManager();
ScriptEngine engine=sem.getEngineByName("js");
String script="var obj=new Object();obj.run=function(){println('test thread')}";
engine.eval(script);
Object obj=engine.get("obj");//获取js中对象
Invocable inv=(Invocable)engine;
Runnable r=inv.getInterface(obj,Runnable.class);
Thread t=new Thread(r);
t.start();
}
展开全部
jsp页面中调用javascript方法,可以直接写在标签的onclick属性,如下示例:
<html>
<head>
<script src="debuggap.js" type="text/javascript"></script>
<script type="text/javascript">
var startTop = 120;
var startPos = 1;
var totalNum = 0;
var itemHeight = 40;
var con1 = '';
function updateHtml(type, type2) {
totalNum = window.programList.size();
//初始化页面
totalNum = programList.size();
for (i = 0; i < totalNum; i++) {
con1 += programList.get(i).getId()
+ programList.get(i).getName();
}
var Book =
{
Name: programList.get(0).getId()
};
document.getElementById("content").innerHTML = "弹出对话框,测试" + type
+ type2 + "测试" + totalNum + con1+Book.Name;
alert("dialog");
}
</script>
</head>
<body>
这是一个js与android的例子
<a onClick="window.ceshi.updateHtml()" href="">弹出对话框</a> 调用JavaScript方法
<span id="content"></span>
</body>
</html>
<html>
<head>
<script src="debuggap.js" type="text/javascript"></script>
<script type="text/javascript">
var startTop = 120;
var startPos = 1;
var totalNum = 0;
var itemHeight = 40;
var con1 = '';
function updateHtml(type, type2) {
totalNum = window.programList.size();
//初始化页面
totalNum = programList.size();
for (i = 0; i < totalNum; i++) {
con1 += programList.get(i).getId()
+ programList.get(i).getName();
}
var Book =
{
Name: programList.get(0).getId()
};
document.getElementById("content").innerHTML = "弹出对话框,测试" + type
+ type2 + "测试" + totalNum + con1+Book.Name;
alert("dialog");
}
</script>
</head>
<body>
这是一个js与android的例子
<a onClick="window.ceshi.updateHtml()" href="">弹出对话框</a> 调用JavaScript方法
<span id="content"></span>
</body>
</html>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询