Vaadin Programming Cookbook I
Total Page:16
File Type:pdf, Size:1020Kb
Vaadin Programming Cookbook i Vaadin Programming Cookbook Vaadin Programming Cookbook ii Contents 1 Vaadin Architecture Tutorial 1 1.1 The tools.......................................................1 1.2 Introduction......................................................1 1.3 Prerequisites......................................................1 1.4 Set up the project...................................................2 1.5 The Architecture...................................................3 1.5.1 The layers...................................................3 1.5.1.1 Client Widgets...........................................4 1.5.1.2 Web Server Layer.........................................4 1.5.1.3 Persistence Layer.........................................4 1.5.2 Our project explained.............................................5 1.5.2.1 Persistence.............................................5 1.5.2.2 Web Server Layer.........................................5 1.5.2.3 Client Tier.............................................8 1.6 Complete Source Code................................................9 1.7 Running the example................................................. 11 1.8 Results......................................................... 11 1.9 Download the Source Code.............................................. 12 2 Vaadin Example Application 13 2.1 The tools....................................................... 13 2.2 Introduction...................................................... 13 2.3 Prerequisites...................................................... 13 2.4 Set up the project................................................... 14 2.5 Coding the example.................................................. 15 2.6 The complete source code.............................................. 21 2.7 Running the example................................................. 25 2.8 Results......................................................... 26 2.9 Download the Source Code.............................................. 26 Vaadin Programming Cookbook iii 3 Vaadin Best Practices 27 3.1 The tools....................................................... 27 3.2 Introduction...................................................... 27 3.3 Prerequisites...................................................... 27 3.4 Set up the project................................................... 27 3.5 The example...................................................... 29 3.5.1 Make a design................................................ 29 3.5.2 Annotations.................................................. 30 3.5.3 Navigator................................................... 30 3.5.3.1 Layout & content......................................... 30 3.5.3.2 Navigator views.......................................... 31 3.5.3.3 Menu listeners........................................... 31 3.5.3.4 Menu................................................ 31 3.5.3.5 Navigator initial page....................................... 32 3.5.3.6 Welcome page........................................... 32 3.5.4 Validate user input.............................................. 33 3.5.4.1 Input form............................................. 33 3.5.4.2 Name field validator........................................ 33 3.5.4.3 Surname field validator...................................... 33 3.5.4.4 Age field validator......................................... 34 3.5.4.5 Age field validator......................................... 34 3.5.4.6 Validation process......................................... 34 3.5.4.7 Clear fields............................................. 35 3.5.5 Use containers in fields............................................ 35 3.5.5.1 Property sets............................................ 36 3.5.5.2 Field groups............................................ 36 3.5.6 Separate the UI from the data........................................ 36 3.5.7 Deploy on https................................................ 38 3.6 The complete source code.............................................. 38 3.6.1 VaadinbestpracticesUI.java.......................................... 38 3.6.2 WelcomePage.java.............................................. 39 3.6.3 InputPage.java................................................ 40 3.6.4 DataPage.java................................................. 42 3.6.5 DataBean.java................................................ 43 3.7 Running the example................................................. 43 3.8 Results......................................................... 43 3.8.1 Welcome view................................................ 43 3.8.2 Input view.................................................. 44 3.8.3 Input view.................................................. 45 3.9 Download the Source Code.............................................. 46 Vaadin Programming Cookbook iv 4 Vaadin Visual Designer Example 47 4.1 The tools....................................................... 47 4.2 Introduction...................................................... 47 4.3 Prerequisites...................................................... 47 4.4 Set up the project................................................... 47 4.4.1 Get the Visual Designer........................................... 47 4.4.2 Create the project............................................... 49 4.5 Coding the example.................................................. 57 4.6 The complete source code.............................................. 61 4.7 Running the example................................................. 63 4.8 Results......................................................... 64 4.9 Download the Source Code.............................................. 64 5 Vaadin Data Binding Example 65 5.1 The tools....................................................... 65 5.2 Introduction...................................................... 65 5.3 Prerequisites...................................................... 65 5.4 Set up the project................................................... 65 5.5 Coding the example.................................................. 67 5.6 The complete source code.............................................. 72 5.7 Running the example................................................. 75 5.8 Results......................................................... 75 5.9 Download the Source Code.............................................. 77 6 Vaadin Rest Example 78 6.1 Introduction...................................................... 78 6.2 Prerequisites...................................................... 78 6.3 Set up the server project............................................... 78 6.3.1 Download Jersey............................................... 78 6.3.2 Create the server project........................................... 79 6.3.3 Copy Jersey Files............................................... 80 6.4 Coding the server................................................... 81 6.5 The server web.xml.................................................. 82 6.6 Create the client project................................................ 83 6.7 Coding the client project............................................... 84 6.7.1 Client Class to access the server....................................... 84 6.7.2 Vaadin UI................................................... 86 6.8 The complete source code.............................................. 87 6.8.1 The server source code............................................ 87 Vaadin Programming Cookbook v 6.8.2 The client Source Code............................................ 88 6.9 Running the example................................................. 91 6.10 Results......................................................... 92 6.10.1 Get the response............................................... 92 6.10.2 Get the plain text............................................... 93 6.10.3 Get the XML................................................. 94 6.10.4 Get the JSON................................................. 95 6.11 Download the Source Code.............................................. 95 7 Vaadin Container Example 96 7.1 The tools....................................................... 96 7.2 Introduction...................................................... 96 7.3 Prerequisites...................................................... 96 7.4 Set up the project................................................... 97 7.5 Coding the example.................................................. 98 7.5.1 MyBean BeanContainer........................................... 98 7.5.2 MySubBean BeanContainer......................................... 99 7.5.3 The UI.................................................... 100 7.5.3.1 Random name generator...................................... 100 7.5.3.2 The layout............................................. 101 7.5.3.3 First BeanContainer........................................ 101 7.5.3.4 Second BeanContainer...................................... 101 7.5.3.5 Employee Table.......................................... 101 7.5.3.6 Doe’s Table............................................ 102 7.5.3.7 Dynamic input........................................... 102 7.5.3.8 Click listener............................................ 102 7.5.3.9 Find Duplicates.......................................... 103 7.6 The complete source code.............................................. 103 7.7 Running the example................................................