传值从Java的JavaScript [英] Pass a value to javascript from Java
问题描述
我有以下的Java小程序。它得到了一个用户选择的文件的绝对文件路径:
I have the following Java applet. It gets the absolute file path of a file that a user has selected:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.awt.Color;
/*
<OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
width=150 height=100
codebase="http://java.sun.com/products/plugin/1.3/jinstall-13-win32.cab#Version=1,3,0,0">
<PARAM NAME="code" value="FileApplet.class">
</OBJECT>
*/
public class fileabs extends JApplet
{
private JTextField tfCount;
final JFileChooser fc = new JFileChooser();
public void init() {
setBackground(Color.WHITE);
JPanel p = new JPanel( new FlowLayout(FlowLayout.CENTER, 15, 15));
p.add(new JLabel("Select File: "));
tfCount = new JTextField(50);
tfCount.setEditable(false);
p.add(tfCount);
JButton b2 = new JButton("Browse...");
p.add(b2);
b2.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent ae) {
tfCount.setText("dsds");
int returnVal = fc.showOpenDialog(fileabs.this);
tfCount.setText(fc.getSelectedFile().getAbsolutePath());
}
} );
// p.add(label);
add(p);
}
}
不过,我需要能够在另一种语言(蟒蛇/ Django的)发送给表单。我怎么会抢abolsute路径的价值,所以我可以在JavaScript解析呢?现在,我没有访问路径,即使它显示了小程序中。
However, I need to be able to send this to a form in another language (python/django). How would I grab the value of the abolsute path so I can parse it in javascript? Right now, I don't have access to the path, even if it shows within the applet.
推荐答案
您应该考虑使用 LiveConnect的,它允许小程序在页面中使用JavaScript交互,反之亦然。
You should look into using LiveConnect, which allows an applet to interact with JavaScript in a page, and vice versa.
以下code调用全球的JavaScript函数的setpath
在页面与一个参数:
The following code calls the global JavaScript function setPath
in the page with a single argument:
netscape.javascript.JSObject.getWindow(appletInstance)
.call("setPath", new Object[] { path });
小程序必须与参数启动 MAYSCRIPT
设置为真正
或 MAYSCRIPT
来被允许使用LiveConnect的。
The applet must be started with the parameter mayscript
set to either true
or mayscript
to be allowed to use LiveConnect.
这篇关于传值从Java的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!