You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

getting-started-first-project.asciidoc 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. ---
  2. title: Creating and Running a Project with Eclipse
  3. order: 5
  4. layout: page
  5. ---
  6. [[getting-started.first-project]]
  7. = Creating and Running a Project with Eclipse
  8. This section gives instructions for creating a new Eclipse project using the
  9. Vaadin Plugin. The task will include the following steps:
  10. . Create a new project
  11. . Write the source code
  12. . Configure and start Tomcat (or some other web server)
  13. . Open a web browser to use the web application
  14. We also show how you can debug the application in the debug mode in Eclipse.
  15. This walkthrough assumes that you have already installed the Vaadin Plugin for
  16. Eclipse and set up your development environment, as instructed in
  17. <<dummy/../../../framework/getting-started/getting-started-environment#getting-started.environment,"Setting
  18. up the Development Environment">>.
  19. [[getting-started.first-project.creation]]
  20. ifdef::web[]
  21. == Creating the Project
  22. endif::web[]
  23. ifdef::web[]
  24. _The following describes the creation of an Ivy project. The upcoming version of the Eclipse plug-in creates Maven projects. For that, see <<getting-started.first-project.maven>>._
  25. endif::web[]
  26. ifdef::web[]
  27. Let us create the first application project with the tools installed in the
  28. previous section. First, launch Eclipse and follow the following steps:
  29. . Start creating a new project by selecting from the menu "File > New > Project...".
  30. . In the [guilabel]#New Project# window that opens, select "Vaadin > Vaadin 7
  31. Project" and click [guibutton]#Next#.
  32. +
  33. image::img/myproject-ivy-new-vaadin.png[width=70%]
  34. . In the [guilabel]#Vaadin Project# step, you need to set the basic web project
  35. settings. You need to give at least the __project name__ and the runtime; the
  36. default values should be good for the other settings.
  37. +
  38. image::img/myproject-ivy-settings.png[width=70%]
  39. [guilabel]#Project name#:: Give the project a name. The name should be a valid identifier usable
  40. cross-platform as a filename and inside a URL, so using only lower-case
  41. alphanumerics, underscore, and minus sign is recommended.
  42. [guilabel]#Use default location#:: Define the directory under which the project is created. The default is under
  43. your workspace folder, and you should normally leave it as it is. You may need
  44. to set the directory, for example, if you are creating an Eclipse project on top
  45. of a version-controlled source tree.
  46. [guilabel]#Target runtime#:: Define the application server to use for deploying the application. The server
  47. that you have installed, for example Apache Tomcat, should be selected
  48. automatically. If not, click [guibutton]#New# to configure a new server under
  49. Eclipse.
  50. [guilabel]#Configuration#:: Select the configuration to use; you should normally use the default
  51. configuration for the application server. If you need to modify the project
  52. facets, click [guibutton]#Modify#. The recommended Servlet 3.0 configuration
  53. uses the @WebServlet deployment, while Servlet 2.4 uses the old
  54. [filename]#web.xml# deployment.
  55. [guilabel]#Deployment configuration#:: This setting defines the environment to which the application will be deployed,
  56. to generate the appropriate project directory layout and configuration files.
  57. The choises are:
  58. *** [guilabel]#Servlet# (default)
  59. *** [guilabel]#Google App Engine Servlet#
  60. *** [guilabel]#Generic Portlet (Portlet 2.0)#
  61. +
  62. The further steps in the New Project Wizard depend on the selected deployment
  63. configuration; the steps listed in this section are for the default servlet
  64. configuration.
  65. ifdef::web[]
  66. See <<dummy/../../../framework/advanced/advanced-gae#advanced.gae,"Google App
  67. Engine Integration">> and <<dummy/../../../framework/portal/portal-overview.asciidoc#portal.overview,"Portal Integration">> for instructions regarding the use of Vaadin in the alternative
  68. environments.
  69. endif::web[]
  70. [guilabel]#Vaadin version#:: Select the Vaadin version to use. The drop-down list shows, by default, the
  71. latest available version of Vaadin. The selection includes nightly
  72. [literal]#++SNAPSHOT++# builds, if you want to keep up with the absolutely
  73. latest unstable versions.
  74. +
  75. You can change the version later in the [filename]#ivy.xml#.
  76. [guilabel]#Create TestBench test#:: When enabled, the application stub will include a test case for testing the UI
  77. with Vaadin TestBench, as described in
  78. <<dummy/../../../testbench/testbench-overview.asciidoc#testbench.overview,"Vaadin TestBench">>.
  79. Vaadin TestBench API library will be included in [filename]#ivy.xml# as a dependency.
  80. Vaadin version 7.3 or later is required to create the stub.
  81. +
  82. You can click [guibutton]#Finish# here to use the defaults for the rest of the
  83. settings, or click [guibutton]#Next#.
  84. . The settings in the [guilabel]#Web Module# step define the basic web application
  85. (WAR) deployment settings and the structure of the web application project. All
  86. the settings are pre-filled, and you should normally accept them as they are.
  87. +
  88. image::img/myproject-ivy-web.png[]
  89. [guilabel]#Context Root#:: The context root (of the application) identifies the application in the URL used
  90. for accessing it. For example, if the project has a [literal]#++myproject++#
  91. context and a single UI at the context root, the URL would be
  92. http://example.com/myproject. The wizard will suggest the project name given in
  93. the first step as the context name. You can change the context root later in the
  94. Eclipse project properties.
  95. [guilabel]#Content Directory#:: The directory containing all the content to be included in the web application
  96. (WAR) that is deployed to the web server. The directory is relative to the root
  97. directory of the project.
  98. +
  99. You can just accept the defaults and click [guibutton]#Next#.
  100. . The [guilabel]#Vaadin project# step page has various Vaadin-specific application settings.
  101. If you are trying out Vaadin for the first time, you should not need
  102. to change anything. You can set most of the settings afterwards, except the
  103. creation of the portlet configuration.
  104. +
  105. image::img/myproject-vaadin.png[]
  106. [guilabel]#Create project template#:: Make the wizard create an UI class stub.
  107. [guilabel]#Application Name#:: A name for the application UI, shown in the title bar of the browser window.
  108. [guilabel]#Base package name#:: The name of the Java package under which the UI class of the application is to
  109. be placed.
  110. [guilabel]#Application/UI class name#:: The name of the UI class for the application, in which the user interface is
  111. developed.
  112. [guilabel]#Portlet version#:: When a portlet version is selected (only Portlet 2.0 is supported), the wizard
  113. will create the files needed for running the application in a portal. See
  114. <<dummy/../../../framework/portal/portal-overview.asciidoc#portal.overview,"Portal
  115. Integration">> for more information on portlets.
  116. +
  117. Finally, click [guibutton]#Finish# to create the project.
  118. [[getting-started.first-project.exploring]]
  119. == Exploring the Project
  120. After the [guilabel]#New Project# wizard exits, it has done all the work for
  121. you: an UI class skeleton has been written to [filename]#src# directory and the
  122. [filename]#WebContent/WEB-INF/web.xml# contains a deployment descriptor. The
  123. project hierarchy shown in the Project Explorer is shown in
  124. <<figure.getting-started.first-project.exploring>>.
  125. [[figure.getting-started.first-project.exploring]]
  126. .A New Vaadin Project
  127. image::img/myproject-ivy-created.png[scaledwidth=60%]
  128. The Vaadin libraries and other dependencies are managed by Ivy. Notice that the
  129. libraries are not stored under the project folder, even though they are listed
  130. in the "Java Resources > Libraries > ivy.xml" virtual folder.
  131. [[getting-started.first-project.exploring.ui]]
  132. === The UI Class
  133. The UI class created by the plugin contains the following code:
  134. [source, java]
  135. ----
  136. package com.example.myproject;
  137. import com.vaadin.ui.UI;
  138. ...
  139. @SuppressWarnings("serial")
  140. @Theme("myproject")
  141. public class MyprojectUI extends UI {
  142. @WebServlet(value = "/*", asyncSupported = true)
  143. @VaadinServletConfiguration(
  144. productionMode = false,
  145. ui = MyprojectUI.class)
  146. public static class Servlet extends VaadinServlet {
  147. }
  148. @Override
  149. protected void init(VaadinRequest request) {
  150. final VerticalLayout layout = new VerticalLayout();
  151. layout.setMargin(true);
  152. setContent(layout);
  153. Button button = new Button("Click Me");
  154. button.addClickListener(new Button.ClickListener() {
  155. public void buttonClick(ClickEvent event) {
  156. layout.addComponent(
  157. new Label("Thank you for clicking"));
  158. }
  159. });
  160. layout.addComponent(button);
  161. }
  162. }
  163. ----
  164. In a Servlet 3.0 project, the deployment is configured with servlet class and a
  165. [literal]#++@WebServlet++# annotation. The stub includes the servlet class as a
  166. static inner class. You may want to refactor it to a separate normal class.
  167. In a Servlet 2.3 project, you would have a [filename]#web.xml# deployment
  168. descriptor.
  169. For a more detailed treatment of the deployment, see
  170. <<dummy/../../../framework/application/application-environment#application.environment.web-xml,"Using a web.xml Deployment Descriptor">>.
  171. endif::web[]
  172. [[getting-started.first-project.maven]]
  173. == Creating a Maven Project
  174. ifdef::web[]
  175. _The following describes project creation in the upcoming version of the Eclipse plug-in, which creates Maven rather than Ivy projects.
  176. To use it, you must have installed the experimental version of the plug-in._
  177. endif::web[]
  178. Let us create the first application project with the tools installed in the previous section.
  179. First, launch Eclipse and follow the following steps:
  180. . Start creating a new project by selecting from the menu "File > New > Project...".
  181. . In the [guilabel]#New Project# window that opens, select "Vaadin > Vaadin 7
  182. Project (Maven)" and click [guibutton]#Next#.
  183. +
  184. image::img/myproject-new-vaadin.png[width=70%]
  185. . In the [guilabel]#Select a Maven archetype# step, you need to select the project type.
  186. To create a simple test project, select the [guilabel]#Single-module Application Project#.
  187. +
  188. image::img/myproject-archetype-selection.png[width=70%]
  189. . In the [guilabel]#Specify archetype parameters# step, you need to give at least the [guilabel]#Group Id# and the [guilabel]#Artifact Id#.
  190. The default values should be good for the other settings.
  191. +
  192. image::img/myproject-settings.png[width=70%]
  193. [guilabel]#Group Id#::
  194. Give the project an organization-level identifier, for example, [packagename]#com.example#.
  195. It is used as a prefix for your Java package names, and hence must be a valid Java package name itself.
  196. [guilabel]#Artifact Id#:: Give the project a name, for example, `myproject`.
  197. The artifact ID must be a valid Java sub-package name.
  198. [guilabel]#Version#:: Give the project a Maven compatible version number, for example, `1.0-SNAPSHOT`.
  199. The version number should typically start with two or more integers separated with dots, and
  200. should not contain spaces.
  201. [guilabel]#Package#:: Give the base package name for the project, for example,
  202. [packagename]#com.example.myproject#.
  203. It is by default generated from the group ID and the artifact ID.
  204. [guilabel]#Properties#:: Enter values for archetype-specific properties that control naming of various elements in the created project, such as the UI class name.
  205. +
  206. You can change the version later in the [filename]#pom.xml#.
  207. +
  208. Finally, click [guibutton]#Finish# to create the project.
  209. [[getting-started.first-project.exploring]]
  210. == Exploring the Project
  211. After the [guilabel]#New Project# wizard exits, it has done all the work for you: a UI class skeleton has been written to the [filename]#src# directory.
  212. The project hierarchy shown in the Project Explorer is shown in <<figure.getting-started.first-project.exploring>>.
  213. [[figure.getting-started.first-project.exploring]]
  214. .A new Vaadin Project
  215. image::img/myproject-created-annotated-hi.png[width=80%]
  216. The Vaadin libraries and other dependencies are managed by Maven.
  217. Notice that the libraries are not stored under the project folder, even though they are listed in the "Java Resources > Libraries > Maven Dependencies" virtual folder.
  218. [[getting-started.first-project.exploring.ui]]
  219. === The UI Class
  220. The UI class created by the plug-in contains the following code:
  221. [source, java]
  222. ----
  223. package com.example.myproject;
  224. import com.vaadin.ui.UI;
  225. ...
  226. @Theme("mytheme")
  227. @Widgetset("com.example.myproject.MyAppWidgetset")
  228. public class MyUI extends UI {
  229. @Override
  230. protected void init(VaadinRequest vaadinRequest) {
  231. final VerticalLayout layout = new VerticalLayout();
  232. final TextField name = new TextField();
  233. name.setCaption("Type your name here:");
  234. Button button = new Button("Click Me");
  235. button.addClickListener( e -> {
  236. layout.addComponent(new Label("Thanks " + name.getValue()
  237. + ", it works!"));
  238. });
  239. layout.addComponents(name, button);
  240. layout.setMargin(true);
  241. layout.setSpacing(true);
  242. setContent(layout);
  243. }
  244. @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
  245. @VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
  246. public static class MyUIServlet extends VaadinServlet {
  247. }
  248. }
  249. ----
  250. [[getting-started.first-project.widgetset]]
  251. == Compiling the Widget Set and Theme
  252. Before running the project for the first time, select [guilabel]#Compile Widgetset and Theme# from the menu shown in <<figure.getting-started.first-project.compilewidgetset>>.
  253. [[figure.getting-started.first-project.compilewidgetset]]
  254. .Compile Widgetset and Theme Menu
  255. image::img/myproject-compilewidgetset.png[width=50%]
  256. [[getting-started.first-project.coding]]
  257. == Coding Tips for Eclipse
  258. One of the most useful features in Eclipse is __code completion__. Pressing
  259. kbd:[Ctrl+Space] in the editor will display a pop-up list of possible class name and
  260. method name completions, as shown in
  261. <<figure.getting-started.first-project.coding.codecompletion>>, depending on the
  262. context of the cursor position.
  263. [[figure.getting-started.first-project.coding.codecompletion]]
  264. .Java Code Completion in Eclipse
  265. image::img/codingtips-codecompletion.png[]
  266. To add an [literal]#++import++# statement for a class, such as
  267. [classname]#Button#, simply press kbd:[Ctrl+Shift+O] or click the red error indicator on
  268. the left side of the editor window. If the class is available in multiple
  269. packages, a list of the alternatives is displayed, as shown in
  270. <<figure.getting-started.first-project.coding.import>>. For server-side
  271. development, you should normally use the classes under the
  272. [package]#com.vaadin.ui# or [package]#com.vaadin.server# packages. You can not
  273. use client-side classes (under [package]#com.vaadin.client#) or GWT classes for
  274. server-side development.
  275. [[figure.getting-started.first-project.coding.import]]
  276. .Importing Classes Automatically
  277. image::img/codingtips-automaticimports.png[scaledwidth=80%]
  278. [[getting-started.first-project.server]]
  279. == Setting Up and Starting the Web Server
  280. Eclipse IDE for Java EE Developers has the Web Standard Tools package installed,
  281. which supports control of various web servers and automatic deployment of web
  282. content to the server when changes are made to a project.
  283. Make sure that Tomcat was installed with user permissions. Configuration of the
  284. web server in Eclipse will fail if the user does not have write permissions to
  285. the configuration and deployment directories under the Tomcat installation
  286. directory.
  287. Follow the following steps:
  288. . Switch to the Servers tab in the lower panel in Eclipse. List of servers should be empty after Eclipse is installed. Right-click on the empty area in the panel and select "New > Server".
  289. +
  290. image::img/tomcat-startserver-1.png[]
  291. . Select "Apache > Tomcat v7.0 Server" and set [guilabel]#Server's host name# as [literal]#++localhost++#, which should be the default. If you have only one Tomcat installed, [guilabel]#Server runtime# has only one choice. Click [guibutton]#Next#.
  292. +
  293. image::img/tomcat-startserver-2.png[]
  294. . Add your project to the server by selecting it on the left and clicking [guibutton]#Add# to add it to the configured projects on the right. Click [guibutton]#Finish#.
  295. +
  296. image::img/tomcat-startserver-3.png[]
  297. . The server and the project are now installed in Eclipse and are shown in the [guilabel]#Servers# tab.
  298. To start the server, right-click on the server and select [guilabel]#Debug#.
  299. To start the server in non-debug mode, select [guilabel]#Start#.
  300. +
  301. image::img/tomcat-startserver-4.png[]
  302. . The server starts and the WebContent directory of the project is published to the server on http://localhost:8080/myproject/.
  303. +
  304. image::img/tomcat-startserver-5.png[]
  305. [[getting-started.first-project.run]]
  306. == Running and Debugging
  307. Starting your application is as easy as selecting [guilabel]#myproject# from the
  308. [guilabel]#Project Explorer# and then "Run > Debug As > Debug on Server".
  309. Eclipse then opens the application in built-in web browser.
  310. .Running a Vaadin Application
  311. image::img/runningMyProject.png[width=60%]
  312. You can insert break points in the Java code by double-clicking on the left
  313. margin bar of the source code window. For example, if you insert a breakpoint in
  314. the [methodname]#buttonClick()# method and click the [guibutton]#What is the
  315. time?# button, Eclipse will ask to switch to the Debug perspective. Debug
  316. perspective will show where the execution stopped at the breakpoint. You can
  317. examine and change the state of the application.
  318. To continue execution, select [guilabel]#Resume# from [guilabel]#Run# menu.
  319. .Debugging a Vaadin Application
  320. image::img/debuggingMyProject.png[]
  321. Above, we described how to debug a server-side application.
  322. Debugging client-side applications and widgets is described in
  323. <<dummy/../../../framework/clientside/clientside-debugging#clientside.debugging,"Debugging Client-Side Code">>.