2017-04-13 84 views
0

我刚开始学习Vaadin。我已经完成了基于这个视频系列的所有教程: https://www.youtube.com/watch?v=o93ofXBIkf8&list=PLcRrh9hGNalkIgvImLRO9u3D0YpmEWuqo 当我从Eclipse创建Vaadin 7/8项目时,我可以使用视频中使用的所有功能。 像textfield.addTextChangeListener(e - > ...) filterText.setInputPrompt(“filter by name”);Vaadin在Spring Boot中缺少一些功能

但是如果我使用SPRING INITIALIZR(https://start.spring.io/),那么我会使用我的UI,这些功能都会丢失。 (可能有更多的功能缺失,我只注意到了这些。)

这里是我的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> 

,并从我的春天启动我的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(); 
    } 
} 

回答

相关问题