V7 & V14+ CDI Applications Side-by-Side
With applications that were developed using Vaadin 7 and CDI, you have the option to keep the legacy code and continue developing new pages with V14+. You’re also able to use CDI beans, such as SessionScoped
beans, in both V14+ and Vaadin 7 parts of your application.
This page contains step-by-step instructions on how to adopt this approach with Vaadin 14. It can also be followed to do the same with Vaadin 15+. Use the Vaadin version of your choice.
First, add Vaadin 14 to your Maven dependencies:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-bom</artifactId>
<type>pom</type>
<scope>import</scope>
<version>24.5.8</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-core</artifactId>
</dependency>
</dependencies>
Next, exclude conflicted dependencies between Vaadin 7 and Vaadin 14 which are jsoup
and atmosphere-runtime
from Vaadin 7 in your pom.xml
file. This is shown in the following example:
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-server-mpr-jakarta</artifactId>
<version>${framework.7.version}</version>
<exclusions>
<exclusion>
<groupId>com.vaadin.external.atmosphere</groupId>
<artifactId>atmosphere-runtime</artifactId>
</exclusion>
<exclusion>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
</exclusion>
</exclusions>
</dependency>
Now, remove dependency of vaadin-cdi 1.*
and add a dependency to mpr-cdi-v7 1.0.0
like so:
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>mpr-cdi-v7</artifactId>
<version>1.0.0</version>
</dependency>
Add the vaadin-cdi
dependency. The version isn’t needed since it’s defined by the vaadin-bom
.
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-cdi</artifactId>
</dependency>
Since the root path of the application is managed by Vaadin 7, you need to define the Vaadin 14 servlet manually. Also, set its URL pattern to a value that doesn’t collide with any of the V7 servlets.
@WebServlet(name = "Flow Servlet", urlPatterns = {
MyFlowServlet.FLOW_SERVLET_ROOT + "/*" })
public class MyFlowServlet extends CdiVaadinServlet {
public static final String FLOW_SERVLET_ROOT = "flow";
}
Now you can have both Vaadin 7 and Vaadin 14 parts of your application in one project. To navigate from a Vaadin 7 part to a Vaadin 14 part, you can use the following line of code:
getUI().getPage().setLocation(MyFlowServlet.FLOW_SERVLET_ROOT);
To navigate from a Vaadin 14 part to a Vaadin 7 view, you can use, for example, an Anchor
like the code below:
Anchor anchor = new Anchor("/#!home", "Home");
add(anchor);
To have shared data between Vaadin 14 and Vaadin 7 parts, you can use SessionScoped
beans that are shared for both V7 and V14 applications.
@SessionScoped
public class SecurityContext implements Serializable {
private User currentUser = new User();
public boolean signIn(String username, String password) {
if (username == null || username.isEmpty())
return false;
currentUser.setUsername(username);
return true;
}
}
8F436CEA-E832-44CC-9641-4E6D84D91FED