Hi !
I am currently developping an Vaadin web-app using Spring.
I am using a specific ApplicationServlet in order to be able to use the Vaadin components as normal Spring beans.
My web.xml:
<servlet>
<servlet-name>subgeste</servlet-name>
<servlet-class>net.sebpx.txvdn.vaadin.util.WebContextApplicationServlet</servlet-class>
...
</servlet>
and WebContextApplicationServlet.java:
package net.sebpx.txvdn.vaadin.util;
import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.ApplicationServlet;
import java.net.MalformedURLException;
import java.net.URL;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
/**
* {@link ApplicationServlet} that acquires {@link Application} instances from an
* associated Spring {@link WebApplicationContext}.
* This allows a Vaadin application to be configured normally as a Spring bean.
*
* <p>
* For example, annotations such as
* <code>{@link org.springframework.beans.factory.annotation.Autowired @Autowired}</code>,
* <code>{@link org.springframework.beans.factory.annotation.Required @Required}</code>, etc.
* and interfaces such as {@link org.springframework.beans.factory.BeanFactoryAware BeanFactoryAware},
* etc. will work on your {@link Application} instances.
* </p>
*
* <p>
* Important: The application bean must be declared in the associated {@link WebApplicationContext}
* with <code>scope="session"</code>, and it must be the only instance of the configured application class.
* </p>
*
* <p>
* An example of "direct" use of this servlet in conjunction with Spring's
* {@link org.springframework.web.context.ContextLoaderListener ContextLoaderListener}:
* <blockquote><pre>
* <!-- Spring context loader -->
* <listener>
* <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
* </listener>
*
* <!-- Vaadin servlet -->
* <servlet>
* <servlet-name>myapp</servlet-name>
* <servlet-class>com.example.WebContextApplicationServlet</servlet-class>
* <init-param>
* <param-name>application</param-name>
* <param-value>some.spring.configured.Application</param-value>
* </init-param>
* <init-param>
* <param-name>productionMode</param-name>
* <param-value>true</param-value>
* </init-param>
* </servlet>
* <servlet-mapping>
* <servlet-name>myapp</servlet-name>
* <url-pattern>/*</url-pattern>
* </servlet-mapping>
* </pre></blockquote>
* with this application context:
* <blockquote><pre>
* <!-- Activate Spring annotation support -->
* <context:annotation-config/>
*
* <!-- Define Vaadin application bean -->
* <bean class="some.spring.configured.Application" scope="session"/>
*
* <!-- Define other beans... -->
* </pre></blockquote>
* </p>
*
* <p>
* An example that creates a Spring MVC "controller" bean for use with Spring's
* {@link org.springframework.web.servlet.DispatcherServlet DispatcherServlet}:
* <blockquote><pre>
* <!-- Activate Spring annotation support -->
* <context:annotation-config/>
*
* <!-- Define controller bean for Vaadin application -->
* <bean id="applicationController" class="org.springframework.web.servlet.mvc.ServletWrappingController"
* p:servletClass="com.example.WebContextApplicationServlet">
* <property name="initParameters">
* <props>
* <prop key="application">some.spring.configured.Application</prop>
* <prop key="productionMode">true</prop>
* </props>
* </property>
* </bean>
*
* <!-- Define Vaadin application bean -->
* <bean class="some.spring.configured.Application" scope="session"/>
*
* <!-- Define other beans... -->
* </pre></blockquote>
*
* @see org.springframework.web.servlet.mvc.ContextLoaderListener
* @see org.springframework.web.servlet.DispatcherServlet
* @see org.springframework.web.servlet.mvc.ServletWrappingController
*/
public class WebContextApplicationServlet extends ApplicationServlet {
protected final Logger log = Logger.getLogger(getClass());
private WebApplicationContext webApplicationContext;
/**
* Initialize this servlet.
*
* @throws ServletException if there is no {@link WebApplicationContext} associated with this servlet's context
*/
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
log.debug("finding containing WebApplicationContext");
try {
this.webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext());
} catch (IllegalStateException e) {
throw new ServletException("could not locate containing WebApplicationContext");
}
}
/**
* Get the containing Spring {@link WebApplicationContext}.
* This only works after the servlet has been initialized (via {@link #init init()}).
*
* @throws ServletException if the operation fails
*/
protected final WebApplicationContext getWebApplicationContext() throws ServletException {
if (this.webApplicationContext == null)
throw new ServletException("can't retrieve WebApplicationContext before init() is invoked");
return this.webApplicationContext;
}
/**
* Create and configure a new instance of the configured application class.
*
* <p>
* The implementation in {@link WebContextApplicationServlet} invokes
* {@link BeanFactory#createBean(Class) BeanFactory.createBean()}
* on the associated {@link WebApplicationContext} class.
* </p>
*
* @param request the triggering {@link HttpServletRequest}
* @throws ServletException if bean creation fails
*/
@Override
protected Application getNewApplication(HttpServletRequest request) throws ServletException {
Class<? extends Application> applicationClass;
try {
applicationClass = this.getApplicationClass();
} catch (ClassNotFoundException e) {
throw new ServletException("failed to create new instance of application class", e);
}
log.debug("acquiring new instance of " + applicationClass + " from " + this.webApplicationContext);
try {
return this.getWebApplicationContext().getBean(applicationClass);
} catch (BeansException e) {
throw new ServletException("failed to acquire new instance of " + applicationClass, e);
}
}
}
Now, I would like to transform my app into a mobile application thanks to TouchKit.
I saw that I am supposed to change the ApplicationServlet with this :
<servlet-class>
com.vaadin.touchkit.mobileapplication.MobileApplicationServlet
</servlet-class>
I guess that my Spring annotations will no longer work with the MobileApplicationServlet.
Has someone already tryed to use TouchKit and Spring ?
Any help would be appreciated.
Thank you !
Sebastian