【问题标题】:Vaadin missing some function in Spring BootVaadin 在 Spring Boot 中缺少一些功能
【发布时间】:2017-04-13 11:11:47
【问题描述】:

我刚刚开始学习 Vaadin。我已经完成了基于这个视频系列的所有教程: https://www.youtube.com/watch?v=o93ofXBIkf8&list=PLcRrh9hGNalkIgvImLRO9u3D0YpmEWuqo 当我从 Eclipse 创建 Vaadin 7/8 项目时,我可以使用视频中使用的所有功能。 像 textfield.addTextChangeListener(e->...) filterText.setInputPrompt("按名称过滤");

但是如果我使用 SPRING INITIALIZR (https://start.spring.io/) 然后我创建我的 UI,这些功能就丢失了。 (可能还有更多功能缺失,我只是注意到这些。)

这里是 spring boot 应用的 pom.xml。

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>hu.csani</groupId>
<artifactId>vaadin-sql3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>vaadin-sql3</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>com.vaadin</groupId>
        <artifactId>vaadin-spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>net.sf.opencsv</groupId>
        <artifactId>opencsv</artifactId>
        <version>2.3</version>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-bom</artifactId>
            <version>8.0.5</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

以及我的 Spring Boot 应用程序中的 UI:

package hu.csani.presenter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.vaadin.data.Binder;
import com.vaadin.server.VaadinRequest;
import com.vaadin.spring.annotation.SpringUI;
import com.vaadin.ui.Button;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Notification;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.Upload;
import com.vaadin.ui.Upload.SucceededEvent;
import com.vaadin.ui.VerticalLayout;

import hu.csani.model.Customer;
import hu.csani.service.CustomerService;

@SpringUI
public class VaadinUI extends UI implements Upload.Receiver, Upload.SucceededListener {

    @Autowired
    private CustomerService service;

    private Customer customer;
    private Binder<Customer> binder = new Binder<>(Customer.class);

    private Grid<Customer> grid = new Grid(Customer.class);

    private TextField filterText = new TextField();

    private TextField firstName = new TextField("First name");
    private TextField lastName = new TextField("Last name");
    private Button save = new Button("Save", e -> saveCustomer());

    private Upload upload;
    private File tempFile;

    @Override
    protected void init(VaadinRequest request) {
        // service.setUi(this);
        updateGrid();
        grid.setColumns("firstName", "lastName");
        grid.addSelectionListener(e -> updateForm());

        binder.bindInstanceFields(this);
        upload = new Upload("Upload it here", this);
        upload.addSucceededListener(this);
        upload.setImmediateMode(false);


//      filterText.p("filter by name");
        filterText.addValueChangeListener(e->{
            updateGrid();
        });

        VerticalLayout layout = new VerticalLayout(upload, filterText, grid, firstName, lastName, save);
        layout.setMargin(true);
        layout.setSpacing(true);
        setContent(layout);
    }

//  private void updateGrid(String filter) {
//      List<Customer> customers = service.findAll(filter);
//      grid.setItems(customers);
//      setFormVisible(false);
//  }

    private void updateGrid() {
        List<Customer> customers = service.findAll(filterText.getValue());
        grid.setItems(customers);
        setFormVisible(false);
    }

    private void updateForm() {
        if (grid.asSingleSelect().isEmpty()) {
            setFormVisible(false);
        } else {
            customer = grid.asSingleSelect().getValue();
            binder.setBean(customer);
            setFormVisible(true);
        }
    }

    private void setFormVisible(boolean visible) {
        firstName.setVisible(visible);
        lastName.setVisible(visible);
        save.setVisible(visible);
    }

    private void saveCustomer() {
        service.update(customer);
        updateGrid();
    }

    public void showNotification(String title, String message) {
        Notification.show(title, message, Notification.Type.HUMANIZED_MESSAGE);
    }

    @Override
    public OutputStream receiveUpload(String filename, String mimeType) {
        System.out.println("receive");
        try {
            tempFile = File.createTempFile(filename, "");
            // System.out.println(tempFile.getAbsolutePath());
            // tempFile.deleteOnExit();
            return new FileOutputStream(tempFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public void uploadSucceeded(SucceededEvent event) {
        // File destinationFile = new File("d:\\test\\" + event.getFilename());
        // FileUtils.moveFile(tempFile , destinationFile);
        service.processfile(tempFile);
        showNotification("CSV upload", "Success");
        updateGrid();
    }
}

【问题讨论】:

    标签: java spring-boot vaadin


    【解决方案1】:

    教程有点过时了,初始化器使用的 Vaadin 8 已经发生了很多变化。

    对于addTextChangeListener,您可以改用addValueChangeListener,并且 setInputPrompt 将是 setPlaceHolder

    有几个网站也注意到其他变化。例如。 https://vaadin.com/download/prerelease/8.0/8.0.0/8.0.0.alpha10/release-notes.html#incompatible

    【讨论】:

      猜你喜欢
      • 2012-06-12
      • 1970-01-01
      • 2012-06-20
      • 1970-01-01
      • 1970-01-01
      • 2022-10-18
      • 1970-01-01
      • 1970-01-01
      • 2018-05-21
      相关资源
      最近更新 更多