I remember the time when i was trying to integrate Spring with Struts2 and GWT applications. The problem was rather documenting the integration stuff i tried to remember them and as usual tend to forgot them. So this time have decided to document the Spring integration with Vaadin.
So first we need to add the Spring Context listener to load the bean configuration file.
org.springframework.web.context.ContextLoaderListener
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
Add the Spring Servlet to intercept Vaadin requests
<servlet-name>VaadinSpringServlet</servlet-name>
<servlet-class>com.dhaval.web.vaadin.spring.servlet.SpringApplicationServlet</servlet-class>
<param-name>applicationBean</param-name>
<param-value>testApplication</param-value>
<servlet-name>VaadinSpringServlet</servlet-name>
<url-pattern>/vaadin/*</url-pattern>
This servlet connects the Spring with Vaadin. The servlet loads the Vaadin Application class and dispatches all the HTTP requests/responses. The
Servlet is available in the Simple Application provided by Vaadin Team for Spring integration.
- Spring bean configuration
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property>name="driverClassName" value="${db.driver}"/>
<property>name="url" value="${db.url}"/>
<property>name="username" value="${db.user}"/>
<property>name="password" value="${db.pass}"/>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property>name="dataSource" ref="dataSource"/>
<bean id="dao.user" class="com.dhaval.web.vaadin.spring.dao.UserDaoImpl">
<constructor-arg>ref="jdbcTemplate"/>
<bean id="testApplication" class="com.dhaval.web.vaadin.spring.MyApplication">
<constructor-arg>ref="dao.user"/>
testApplication bean id is same as we have given in the web.xml as an init-param to Spring Servlet.
- Vaadin Main Application class
This is my Vaadin Application class
public class MyApplication extends Application{
private UserDaoImpl userDao;
public MyApplication(UserDaoImpl userDao){
Window mainWindow = new Window("Simple Vaadin Spring Integration");
final TextField txtUser = new TextField("Enter username");
Button btnCheckUser = new Button("Check Username");
final Label lblCheckMsg = new Label("");
btnCheckUser.addListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
boolean result = userDao.hasUser(txtUser.getValue().toString());
lblCheckMsg.setValue("Available");
lblCheckMsg.setValue("Not Available");
mainWindow.addComponent(txtUser);
mainWindow.addComponent(btnCheckUser);
mainWindow.addComponent(lblCheckMsg);
setMainWindow(mainWindow);