Thursday, February 18, 2010

Vaadin-Spring Integration

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.

  • 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.

2 comments:

  1. Hi Dhaval, Thank you for posing this article. Need your help ,I am not able to execute this application.What I am missing in below url? or there is any other way to run it?

    http://localhost:8080/TestVaadin/testApplication

    ReplyDelete