Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
book-of-vaadin.pdf
Скачиваний:
88
Добавлен:
24.03.2015
Размер:
13.43 Mб
Скачать

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

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]