2011-09-12 38 views
0

我正在使用Vaadin和Liferay6.0创建一个Portlet项目。 我需要从ui上传一个csv文件并读取文件。 我的班级作如下:如何上传Vaadin ui Portlet中的文件?

@SuppressWarnings("serial") 
public void init() { 
    Window window = new Window("Vaadin Portlet Application"); 
    setMainWindow(window); 
    window.addComponent(new Label("Hello Vaadin user!")); 
    window.addComponent(new Label("We are here")); 
    final TextField tf = new TextField("Device Name:"); 

    // Create the Upload component. 
    final Upload upload = 
      new Upload("Upload the file here", null); 

    // Use a custom button caption instead of plain "Upload". 
    upload.setButtonCaption("Upload Now"); 



    try{ 

     System.out.println("I am here only"); 
     final DeviceManager dManager = new DeviceManager(); 
     final DeviceSoap[] devices = dManager.getAll(); 
     //getDeviceByName("207.20.47.137"); 
     for (DeviceSoap deviceSoap : devices) { 
      window.addComponent(new Label("Device Name! "+deviceSoap.getName())); 

     } 


     window.addComponent(tf); 
     Button submitBttn = new Button("Add Device"); 
     Button updateBttn = new Button("Update Device"); 
     window.addComponent(submitBttn);    

     // Handle button clicks 
     submitBttn.addListener(new Button.ClickListener() { 
      @Override 
      public void buttonClick(ClickEvent event) { 
       // If the field value is bad, set its error. 
       // (Allow only alphanumeric characters.) 
       DeviceSoap d = new DeviceSoap(); 
       d.setName(tf.getValue().toString()); 
       try { 
        dManager.createDevice(d); 
       } catch (RemoteException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 

     }); 


     updateBttn.addListener(new Button.ClickListener() { 
      @Override 
      public void buttonClick(ClickEvent event) { 
       // If the field value is bad, set its error. 
       // (Allow only alphanumeric characters.) 
       DeviceSoap d = devices[1]; 
       d.setName(tf.getValue().toString()); 
       try { 
        dManager.createDevice(d); 
       } catch (RemoteException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 

     });   


     upload.addListener((Upload.SucceededListener) this); 
     upload.addListener((Upload.FailedListener) this); 
     window.addComponent(upload); 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 
} 

感谢 Hiamsnhu

回答

1

我在做同样的事情,并在阅读你的代码,我看,你忘了做:

window.addComponent(updateBttn); 

希望这工作。