ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 01.01.2026

Просмотров: 2623

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

Getting Started with Vaadin

3. In the Vaadin Project step, you need to set the basic web project settings. You need to give at least the project name and the runtime; the default values should be good for the other settings.

Project name

Give the project a name.The name should be a valid identifier usable cross-platform as a filename and inside a URL, so using only lower-case alphanumerics, underscore, and minus sign is recommended.

Use default location

Define the directory under which the project is created. You should normally leave it as it is. You may need to set the directory, for example, if you are creating an Eclipse project on top of a version-controlled source tree.

Target runtime

Define the application server to use for deploying the application. The server that you have installed, for example Apache Tomcat, should be selected automatically. If not, click New to configure a new server under Eclipse.

Configuration

Select the configuration to use; you should normally use the default configuration for the application server. If you need to modify the project facets, click Modify.

38

Creating the Project

Getting Started with Vaadin

Deployment configuration

This setting defines the environment to which the application will be deployed, to generate the appropriate project directory layout and configuration files.The choises are:

Servlet (default)

Google App Engine Servlet

Generic Portlet (Portlet 2.0)

The further steps in the New Project Wizard depend on the selected deployment configuration; the steps listed in this section are for the default servlet configuration. See Section 11.7, “Google App Engine Integration” and Chapter 12, Portal Integration for instructions regarding the use of Vaadin in the alternative environments.

Vaadin version

Select the Vaadin version to use. The drop-down list shows, by default, the latest available version of Vaadin. If you want to use another version, click Download. The dialog that opens lists all official releases of Vaadin.

If you want to use a pre-release version or a nightly development build, select Show pre-release versions and nightly builds. Select a version and click OK to download it. It will appear as a choise in the drop-down list.

If you want to change the project to use another version of Vaadin, for example to upgrade to a newer one, you can go to project settings and download and select the other version.

Use latest nightly build

Ticking this option configures Ivy to use the latest (unstable) nightly Vaadin build in the same branch. The builds are downloaded automatically. You can later change the behavior by manually editing the ivy.xml file.

You can click Finish here to use the defaults for the rest of the settings, or click Next.

Creating the Project

39

Getting Started with Vaadin

4.The settings in the Web Module step define the basic web application (WAR) deployment settings and the structure of the web application project. All the settings are pre-filled, and you should normally accept them as they are.

Context Root

The context root (of the application) identifies the application in the URL used for accessing it. For example, if the project has a myproject context and a single UI at the context root, the URL would be http://example.com/myproject. The wizard will suggest the project name given in the first step as the context name. You can change the context root later in the Eclipse project properties.

Content Directory

The directory containing all the content to be included in the web application (WAR) that is deployed to the web server. The directory is relative to the root directory of the project.

You can just accept the defaults and click Next.

5.The Vaadin project step page has various Vaadin-specific application settings. If you are trying out Vaadin for the first time, you should not need to change anything. You can set most of the settings afterwards, except the creation of the portlet configuration.

40

Creating the Project



Getting Started with Vaadin

Create project template

Make the wizard create an application class stub.

Application Name

A name for the servlet or portlet.

Base package name

The name of the Java package under which the UI class of the application is to be placed.

Application/UI class name

The name of the UI class for the application, in which the user interface is developed.

Portlet version

When a portlet version is selected (only Portlet 2.0 is supported), the wizard will create the files needed for running the application in a portal. See Chapter 12, Portal Integration for more information on portlets.

Finally, click Finish to create the project.

2.5.2. Exploring the Project

After the New Project wizard exists, it has done all the work for us: an UI class skeleton has been written to src directory and the WebContent/WEB-INF/web.xml contains a deployment descriptor. The project hierarchy shown in the Project Explorer is shown in Figure 2.3, “A New Vaadin Project”.

The Vaadin libraries and other dependencies are managed by Ivy. Notice that the libraries are not stored under the project folder, even though they are listed in the Java Resources Libraries

ivy.xml virtual folder.

The UI Class

The UI class created by the plugin contains the following code:

package com.example.myproject;

import com.vaadin.server.VaadinRequest; import com.vaadin.ui.Label;

import com.vaadin.ui.UI;

public class MyprojectUI extends UI { @Override

public void init(VaadinRequest request) {

// Create the content root layout for the UI VerticalLayout content = new VerticalLayout(); setContent(content);

Label label = new Label("Hello Vaadin user"); content.addComponent(label);

}

}

Deployment Descriptor (web.xml)

The deployment descriptor WebContent/WEB-INF/web.xml defines Vaadin framework servlet, the application class, and servlet mapping. Below is a deployment descriptor for the Hello World application.

Exploring the Project

41


Getting Started with Vaadin

Figure 2.3. A New Vaadin Project

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

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

<display-name>myproject</display-name>

<context-param>

<description>Vaadin production mode</description> <param-name>productionMode</param-name> <param-value>false</param-value>

</context-param>

<servlet>

<servlet-name>Myproject UI</servlet-name> <servlet-class>com.vaadin.server.VaadinServlet</servlet-class> <init-param>

<description>Vaadin UI class to use</description> <param-name>UI</param-name>

<param-value> com.example.myproject.MyprojectUI

</param-value> </init-param>

</servlet>

<servlet-mapping>

<servlet-name>Myproject UI</servlet-name> <url-pattern>/*</url-pattern>

</servlet-mapping> </web-app>

For a more detailed treatment of the web.xml file, see Section 4.8.3, “Deployment Descriptor web.xml”.

2.5.3. Coding Tips for Eclipse

Let us add a button to the application to make it a bit more interesting. The resulting init() method could look something like:

@Override

public void init(VaadinRequest request) {

42

Coding Tips for Eclipse

Getting Started with Vaadin

//Create the content root layout for the UI VerticalLayout content = new VerticalLayout(); setContent(content);

//Add a simple component

Label label = new Label("Hello Vaadin user"); content.addComponent(label);

// Add a component with user interaction content.addComponent(new Button("What is the time?",

new ClickListener() { public void buttonClick(ClickEvent event) {

Notification.show("The time is " + new Date());

}

}));

}

One of the most useful features in Eclipse is code completion. Pressing Ctrl+Space in the editor will display a popup list of possible class name and method name completions, as shown in Figure 2.4, “Java Code Completion in Eclipse”, depending on the context of the cursor position.

Figure 2.4. Java Code Completion in Eclipse

To add an import statement for a class, such as Button, simply press Ctrl+Shift+O or click the red error indicator on the left side of the editor window. If the class is available in multiple packages, a list of the alternatives is displayed, as shown in Figure 2.5, “Importing Classes Automatically”. For server-side Vaadin development, you should normally use the classes under the com.vaadin.server package.

Figure 2.5. Importing Classes Automatically

Coding Tips for Eclipse

43


Getting Started with Vaadin

2.5.4. Setting Up and Starting the Web Server

Eclipse IDE for Java EE Developers has the Web Standard Tools package installed, which supports control of various web servers and automatic deployment of web content to the server when changes are made to a project.

Make sure that Tomcat was installed with user permissions. Configuration of the web server in Eclipse will fail if the user does not have write permissions to the configuration and deployment directories under the Tomcat installation directory.

Follow the following steps.

1.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.

2. Select Apache Tomcat v7.0 Server and set Server's host name as localhost, which should be the default. If you have only one Tomcat installed, Server runtime has only one choice. Click Next.

3.Add your project to the server by selecting it on the left and clicking Add to add it to the configured projects on the right. Click Finish.

44

Setting Up and Starting the Web Server

Getting Started with Vaadin

4.The server and the project are now installed in Eclipse and are shown in the Servers tab. To start the server, right-click on the server and select Debug. To start the server in non-debug mode, select Start.

5.The server starts and the WebContent directory of the project is published to the server on http://localhost:8080/myproject/.

2.5.5.Running and Debugging

Starting your application is as easy as selecting myproject from the Project Explorer and then Run Debug As Debug on Server. Eclipse then opens the application in built-in web browser.

Figure 2.6. Running a Vaadin Application

You can insert break points in the Java code by double-clicking on the left margin bar of the source code window. For example, if you insert a breakpoint in the buttonClick() method and click the What is the time? button, Eclipse will ask to switch to the Debug perspective. Debug perspective will show where the execution stopped at the breakpoint. You can examine and change the state of the application. To continue execution, select Resume from Run menu.

Running and Debugging

45