简体   繁体   English

在Vaadin Flow + Spring Boot项目中添加dropzone.js

[英]Adding dropzone.js on Vaadin Flow + Spring Boot project

I want to add dropzone.js version 5 to my Vaadin Flow 12.0.7 & Spring Boot 2.1.3.RELEASE project but without any success. 我想将dropzone.js版本5添加到我的Vaadin Flow 12.0.7和Spring Boot 2.1.3.RELEASE项目中但没有任何成功。

I've tried both methods adding dropzone on the page: default one, described here: https://www.dropzonejs.com/#usage And programmatically described here: https://www.dropzonejs.com/#create-dropzones-programmatically 我已尝试在页面上添加dropzone的两种方法:默认一种,在此处描述: https ://www.dropzonejs.com/#usage并按此处编程描述: https//www.dropzonejs.com/#create-dropzones-编程

I've added dropzone.js to static resources in my project: 我已将dropzone.js添加到项目中的静态资源中:

\src\main\resources\static\frontend\js

and imported it by 并通过它导入

@JavaScript("/frontend/js/dropzone.js")

Then I've added a new form and div on my view: 然后我在我的视图中添加了一个新表单和div:

@Route
@JavaScript("/frontend/js/dropzone.js")
public class MainView extends VerticalLayout {

    private final Div dropZoneDiv;

    private boolean dropZoneAttached = false;

    private MainView() {
        add(new H1("Vaadin Spring Dropzone"));

        add(new H2("Default adding method"));
        Element form = new Element("form");
        form.setAttribute("action", "file/post");
        form.setAttribute("class", "dropzone");
        form.setAttribute("id", "my-awesome-dropzone");
        getElement().appendChild(form);

        add(new H2("Create dropzone programmatically"));
        dropZoneDiv = new Div();
        dropZoneDiv.setId("dzDiv");
        add(dropZoneDiv);
    }

    @Override
    protected void onAttach(AttachEvent attachEvent) {
        super.onAttach(attachEvent);
        if (dropZoneDiv!=null && !dropZoneAttached) {
            UI.getCurrent().getPage().executeJavaScript(
                    "var myDropzone = new Dropzone(\"div#dzDiv\", { url: \"/file/post\"});\n" //+
            );
            dropZoneAttached = true;
        }
    }
}

I expected to see at least one dropzone on the page, but actually the div and form I'm attaching dropzone to are empty in Chrome or Firefox. 我希望在页面上看到至少一个dropzone,但实际上我在Chrome或Firefox中附加dropzone的div和form是空的。

I've got an answer thank to my friend and colleague. 我得到了答案,谢谢我的朋友和同事。

Here is the working solution: 这是工作解决方案:

@Route
@JavaScript("/frontend/js/dropzone.js")
@StyleSheet("/frontend/js/dropzone.css")
public class MainView extends VerticalLayout {

    private MainView() {

        add(new H1("Vaadin Spring Dropzone"));

        add(new H2("Default adding method"));
        Element form = new Element("form");
        form.setAttribute("action", "uploadwsdl");
        form.setAttribute("class", "dropzone dz-clickable");
        form.setAttribute("id", "wsdlUpload");
        getElement().appendChild(form);
    }

    @Override
    protected void onAttach(AttachEvent attachEvent) {
        super.onAttach(attachEvent);
        UI.getCurrent().getPage().executeJavaScript("Dropzone._autoDiscoverFunction()");
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM