Adam Bien's Weblog

Tuesday Apr 30, 2013

Essential Vaadin 7 / Java EE Maven 3 POM

With an additional Maven 3 dependency and three dependencies in total, you can integrate Vaadin 7 with your Java EE 6/7 backend in an efficient way. No XML configuration or any other configuration is needed.

With the cdi-integration you can not only inject EJBs and CDI managed beans directly to the UIs and views, but also expose the view with a single annotation:


import com.vaadin.cdi.CDIUI;
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.UI;
import javax.inject.Inject;

@CDIUI
public class WelcomePage extends UI {

    @Inject
    ReceptionService service;


    @Override
    protected void init(VaadinRequest request) {
        setSizeFull();
        String message = service.welcome();
        Label label = new Label(message);
        setContent(new HorizontalLayout(label));
    }
}

@Stateless
public class ReceptionService {

    public String welcome() {
        return "Hello, Developer! No XML, No Configuration, and it works!";
    }
}


The pom.xml is based on a minimalistic Java EE template enhanced with three additional dependencies to Vaadin mentioned before:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.airhacks</groupId>
    <artifactId>vaadin-with-javaee-pom</artifactId>
    <version>1.0-SNAPSHOT</version>
    <url>http://airhacks.com</url>
    <packaging>war</packaging>
    <name>vaadin-with-javaee-pom</name>
    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-cdi</artifactId>
            <version>1.0.0.alpha1</version>
        </dependency>     
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-themes</artifactId>
            <version>7.0.4</version>
        </dependency>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-client-compiled</artifactId>
            <version>7.0.4</version>
        </dependency>           

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

A deployable Java EE project was pushed into https://github.com/AdamBien/vaadin-with-javaee-pom/

See you at Java EE Workshops at MUC Airport (particularly at JavaEE UI workshop)!


NEW workshop: Microservices with Java EE 7 and Java 8, January 26th, 2015, Airport Munich

A book about rethinking Java EE Patterns

Comments:

Any reference where to put @WebServlet annotation with this? Thanks.

Posted by Angga on May 13, 2013 at 03:15 PM CEST #

@Angga,

you don't need any reference to a @Servlet. It should just work out-of-the-box,

thanks!,

adam

Posted by Adam Bien on May 14, 2013 at 11:25 AM CEST #

Hi Adam,

Yap I understand, lets say I want to configure url pattern or maybe JEE security, etc through @WebServlet. Should I just use the web.xml then? Thanks.

Posted by Angga on May 14, 2013 at 09:08 PM CEST #

Hey Adam

I love your minimalistic style! :)

The snippet above didn't work on my glassfish 3.1, unless i've put an empty beans.xml in webapp/WEB-INF/ directory. Then it works great :)

I guess for JEE-7 we'd also need the bean-discovery-mode=all flag in the beans.xml

Posted by Raffi on October 07, 2013 at 05:08 PM CEST #

Hi,

Vaadin-CDI is pulling vaadin-* 7.0.2. I can get it to work with 7.1.6 by manually specifying the dependencies, but the beauty is lost.

Is the only way to wait for a non-alpha vaadin-cdi?

Thanks!

Posted by zemiak on October 08, 2013 at 08:59 PM CEST #

Hi Adam,

I would like know your opinion on Vaadin vs. JavaFX. I am especially backhand programmer and I would like to learn one of these frontend technologies. Can you tell me how you feel the comparison of these technologies? Thanks and I appreciate your work on this blog :)

Ciao Petr

Posted by PBouda on November 02, 2013 at 10:58 AM CET #

When I checkout, build and deploy this in Wildfly 8, i get the error "failed to load bootstrap javascript VAADIN/vaadinBootstrap.js"

Any idea what I could be doing wrong?

Posted by Edem on February 25, 2014 at 06:57 PM CET #

Hi,

Adam, thanks, everything work just fine!

Edem, you just need to specify session tracking mode for vaadin app through @WebListener:

@WebListener
public class ServletContextListenerImpl implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
sce.getServletContext().setSessionTrackingModes(EnumSet.of(SessionTrackingMode.COOKIE));
}

@Override
public void contextDestroyed(ServletContextEvent sce) { }
}

or web.xml:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<session-config>
<tracking-mode>COOKIE</tracking-mode>
</session-config>
</web-app>

Posted by Bogdan on July 20, 2014 at 07:41 PM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
realworldpatterns.com
...the last 150 posts
...the last 10 comments
License