从javascript错误调用公共Java方法

时间:2012-09-28 12:05:12

标签: java javascript html django

在我的Django网页中,我需要Javascript来调用java applet的公共方法。 我想让java applet将一些数据POST到DJango webserver,但为了做到这一点,我需要cookie来验证会话。 考虑到这一点,我发现我可以从JS调用我的applet的公共方法,所以我尝试了它,但它失败了。 我不知道哪个是问题,或者我错过了什么。

这是代码。

HTML:

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.1.min.js"></script>

<applet name="mapGenerator"
    code="MapGenerator.class"
    archive="{{STATIC_URL}}java/MapGenerator.jar"
    width= 500 height = 300>

        <param name=numero_immagini value="{{floor.count}}">
        <param name=id value="{{building.id}}">
        {% for f in floor %}
            <param name=immagine{{forloop.counter0}} value="{{MEDIA_URL}}{{f.link.name}}">
            <param name=numero_piano{{forloop.counter0}} value="{{f.numero_di_piano}}">
        {%endfor%}
</applet>

<script type="text/javascript">
    function initialize() {
        document.mapGenerator.setCookie(document.cookie);
    }
</script>

<body onload="initialize()">

类MapGenerator:

public class MapGenerator extends JApplet {

    //... variables

    public void init() {
        numero_immagini = Integer.parseInt(this.getParameter(N_IMMAGINI));
        id_building = Integer.parseInt(this.getParameter(ID_BUILDING));
        images = new BufferedImage[numero_immagini];
        floors = new int[numero_immagini];

        for(int i=0; i< numero_immagini; i++) {
            try {
                URL url = new URL(getCodeBase(), this.getParameter(IMMAGINE+i));
                    images[i] = ImageIO.read(url);
                    floors[i] = Integer.parseInt(this.getParameter(PIANO_IMMAGINE+i));
            } catch (IOException ioe) {ioe.printStackTrace();}
        }
    }

    public void setCookie(String cookie) {
        this.cookie = cookie;
        ed.cookie = cookie;
        System.err.println(cookie);
    }

    public void start() {

        ed = new Editor(this.getContentPane(), images, floors, id_building);                     
        this.add(ed.getPanel());
        Toolkit kit = this.getToolkit();
        Dimension dim = kit.getScreenSize();
        this.setBounds(dim.width/4, dim.height/4, dim.width/4, dim.height/4);
        this.setVisible(true);
        this.repaint();
    }
}

当我打开此网页时,这是JS控制台中的错误:

Uncaught TypeError: Object #<HTMLAppletElement> has no method 'setCookie'

谢谢

1 个答案:

答案 0 :(得分:0)

我无法相信这是一件愚蠢的事......

我只需要编写脚本

<script type="text/javascript">
    function initialize() {
        document.mapGenerator.setCookie(document.cookie);
    }
</script>

在applet之前......我要哭了......