Je découvre en ce moment un nouveau framework de présentation
Echo2 qui m'a l'air de permettre de réaliser des interfaces web sympas. Comme pas mal d'outils qui sortent en ce moment, y a de l'Ajax par dessous...
Et comme il m'arrive souvent d'utiliser
Spring dans mes applis, je me suis créé une petite servlet toute simple permettant d'intégrer Spring et Echo2. La voici en espérant qu'elle puisse être utile à d'autres personnes:package com.fha.spring.integration.echo;
import nextapp.echo2.app.ApplicationInstance;
import nextapp.echo2.webcontainer.WebContainerServlet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
/**
* Servlet to be used to integrate Echo with Spring.
*
* It uses the Spring application context to create the Echo application.
* Declare the servlet in your web.xml file.
*
* Be sure that your Echo ApplicationInstance is defined in your application
* context and that it is not a singleton.
*
* @author Fouad Hamdi
*/
public class EchoSpringAwareServlet extends WebContainerServlet {
private Log log = LogFactory.getLog(EchoSpringAwareServlet.class);
private ApplicationContext context;
private String applicationName;
/**
* Return the application context if an inherited servlet is used
* instead of this one.
*/
protected ApplicationContext getApplicationContext() {
return context;
}
/**
* Returns the ApplicationInstance object defined within the
* Spring context.
*/
public ApplicationInstance newApplicationInstance() {
initApplicationContextIfNeeded();
retrieveApplicationNameIfNeeded();
return (ApplicationInstance) context.getBean(applicationName);
}
/**
* Initializes the Spring application context.
*/
private void initApplicationContextIfNeeded() {
if (context == null) {
context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
}
}
/**
* Gets the name of the Echo application in the Spring
* context.
*/
private void retrieveApplicationNameIfNeeded() {
if (applicationName == null) {
String[] beanNames = context.getBeanNamesForType(ApplicationInstance.class);
if (beanNames == null || beanNames.length == 0) {
throw new IllegalArgumentException("A bean of type " +
"nextapp.echo2.app.ApplicationInstance must be declared " +
"in the application context.");
}
if (beanNames.length > 1) {
throw new IllegalArgumentException("There must only be one " +
"bean of type nextapp.echo2.app.ApplicationInstance declared " +
"in the application context.");
}
this.applicationName = beanNames[0];
}
}
}
Je déclare cette servlet dans mon fichier web.xml ainsi que le listener Spring:<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>EchoSpringAwareServlet</servlet-name>
<servlet-class>com.fha.spring.integration.echo.EchoSpringAwareServlet</servlet-class>
</servlet>
Et enfin, j'ajoute l'application de démarrage d'Echo dans un fichier de configuration Spring. Par exemple:<bean id="app" class="org.echo.test.HelloWorldApp" singleton="false">
<property name="service" ref="service" />
</bean>
<bean id="service" class="org.echo.test.ServiceImpl" />
Je suis obligé de définir le bean d'application comme étant un prototype (singleton="false") car Echo2 doit retourner une instance différente pour chaque nouvel utilisateur (potentiellement la cause de surcharge de mémoire en cas de connexions simultanées d'un grand nombre d'utilisateurs ? A vérifier...).
Comme je débute encore sur Echo2, je ne sais pas si c'est la meilleure solution pour intégrer les deux frameworks mais ça m'a l'air simple comme méthode.