Vaadin And Spring Integration

This is my documentation for the steps that i have followed to integrate the Spring with the Vaadin.

  • web.xml changes
So first we need to add the Spring Context listener to load the bean configuration file.
<listener>
  <listener-class>
    org.springframework.web.context.ContextLoaderListener
  </listener-class>
</listener>

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

Add the Spring Servlet to intercept Vaadin requests
 
<servlet>
  <servlet-name>VaadinSpringServlet</servlet-name>
  <servlet-class>com.dhaval.web.vaadin.spring.servlet.SpringApplicationServlet</servlet-class>
  <init-param>
    <param-name>applicationBean</param-name>
    <param-value>testApplication</param-value>
  </init-param>
</servlet>

<servlet-mapping>
  <servlet-name>VaadinSpringServlet</servlet-name>
  <url-pattern>/vaadin/*</url-pattern>
</context-param> 

  • Spring Servlet
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>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  <property>name="dataSource" ref="dataSource"/>
</bean>

<bean id="dao.user" class="com.dhaval.web.vaadin.spring.dao.UserDaoImpl">
  <constructor-arg>ref="jdbcTemplate"/>
</bean>

<bean id="testApplication" class="com.dhaval.web.vaadin.spring.MyApplication">
  <constructor-arg>ref="dao.user"/>
</bean>
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){
        this.userDao = userDao;
    }

    public void init() {
        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());
                if(result){
                    lblCheckMsg.setValue("Available");
                }else{
                    lblCheckMsg.setValue("Not Available");
                }
            }
        });

        mainWindow.addComponent(txtUser);
        mainWindow.addComponent(btnCheckUser);
        mainWindow.addComponent(lblCheckMsg);

        setMainWindow(mainWindow);
   }
}

The full source code is available at vaadin-spring-integration in download section.

 

Comments

Popular posts from this blog

PostgreSQL bytea and oid

MySQL as Hive metadata store

Big Memory Go.