将值从 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);
}
}
但是,我需要能够将其发送到另一种语言(python/django)的表单.我将如何获取绝对路径的值,以便我可以在 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.
以下代码使用单个参数调用页面中的全局 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 });
applet 必须在参数 mayscript
设置为 true
或 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屋!