En el próximo post vamos a hablar sobre el proyecto Spring Mobile deSpringsource. Este proyecto pretende facilitar la realización de aplicaciones web sobre dispositivos móviles utilizando los controladores de Spring MVC.
Archivo de la etiqueta: Spring MVC
Groovy integration inside a Spring project
In this post we are going to talk a little bit about how to integrate Groovy (or another Scripting language like JRuby or BeanShell) within a Spring project. Achiving that is not really complicated, the only thing you need to do is add your Groovy scripts with a special schema within your application context. The example I am going to show you is uploaded to my Github account. You can find it right here.
And what are we going to do?. We are create a simple sample, based in the official Spring documentation you may find here or here too. We are going to write our Spring MVC controller, but thanks to Groovy we are going to be able to redefine our controller redirection with no need to compile our code again.
The example is pretty simple, imaging you have a controller related to a specified url (say home.htm). What we want is changing the page the controller eventually render. So we need to write our code using Groovy.
The application structure looks like as follows:

We have two controller classes:
- HomeController: It’s a java class that implements the Controller class imported from Spring MVC. The class itself do nothing, another Controller that we code in Groovy is injected within the class. This Groovy controller will be responsible to redirect the request. So we have to call its handleRequest method inside the handleRequest method of our HomeController.
package com.hopcroft.examples.controller; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class HelloController implements Controller { private Controller helloGroovyController; public Controller getHelloGroovyController() { return helloGroovyController; } public void setHelloGroovyController(Controller helloGroovyController) { this.helloGroovyController = helloGroovyController; } public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { return helloGroovyController.handleRequest(arg0, arg1); } } - HomeGroovyController: this Groovy class is code in Groovy and as we previously mentioned, do the redirect job. It seems like another Java class but we save it as a Groovy script/file.
package com.hopcroft.examples.controller; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class HelloGroovyController implements Controller { protected final Log logger = LogFactory.getLog(getClass()); public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { logger.info("Returning index view"); return new ModelAndView("index.jsp"); } }
Wiring the controller in the Spring context.
To define the controller within our context we need to use the tag. Take a look at the context definition below.
<pre class="brush: xml; gutter: true; first-line: 1"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:soap="http://cxf.apache.org/bindings/soap"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd"
default-autowire="byName">
<lang:groovy id="helloGroovyController" script-source="classpath:groovy/HelloGroovyController.groovy"></lang:groovy>
<bean name="/helloGroovy.htm" id="controller" class="com.hopcroft.examples.controller.HelloController">
<property name="helloGroovyController" ref="helloGroovyController" />
</bean>
</beans></pre>
As you may notice, it’s not really difficult to understand. We have two bean definition, one is a Groovy file where you add the script-source property to define the location of the Groovy Controller. The other one is a common bean to which we’ll inject the previous Groovy bean as a property.
And that’s all, now you can deploy your web application to you favourite web container and see how it’s working. You can change your Groovy controller in runtime and notice the changes with no reploying.
Another important thing to bold is the pom file. In order to compile our Groovy file (to be sure that isn’t going to fail once the server is up) we have add the build-helper-maven plugin to our project.
Don’t forget you can read the post in my new blog here
Selenium , testing framework for acceptance tests
Selenium is a testing framework that allows you automatizing your acceptance tests. Selenium gives us the opportunity to test the interaction between our application pages and the users. Basically Selenium framework allows us to do two different things:
Read the rest of this entry »
Spring MVC basic example with Maven
In the following post we are going to talk about the Spring MVC project. But before that I have been thinking about writing some other posts about the Spring framework. I’m going to talk about some SpringSource projects such as Spring MVC, Spring Web Flow, Spring Security (O Auth) or Spring Faces to name a few. Moreover I’m planning to start writing about continuous integration and testing with (Hudson / Jenkins, Sonar, Cobertura, Selenium, Checkstyles, PMD, …). I’d also like write about Cloud Computing , as a novice developer in this particular subject, analyzing some frameworks and tools such as the Google App Engine (GAE) or Micro Cloud Foundry. But there is much left to all I have just mentioned so …. it is time to start with Spring MVC.
Selenium, framework para pruebas de aceptación
Selenium es un framework de pruebas que permite automatizar los test de aceptación en nuestras aplicaciones web. Selenium permite probar el correcto funcionamiento de la interacción de usuario con los elementos de nuestras páginas web. Básicamente el framework Selenium nos permite dos cosas:
Read the rest of this entry »
Ejemplo básico de Spring MVC con Maven
En el siguiente post vamos a hablar un poco de Spring MVC. Pero antes comentar que tengo pensado hacer una serie de post (el primero es este) sobre Spring. En concreto intentaré hablar un poco de proyecto de SpringSource como Spring MVC, Spring Web Flow, Spring Security (O Auth) ó Spring Faces. Además voy a hablar un poco de desarrollo e integración continua y pruebas (Hudson / Jenkins, Sonar, Cobertura, Selenium, Checkstyles, PMD, …). También me gustaría tratar temas de Cloud Computing , siempre como absoluto novato en la materia, analizando herramientas como Google App Engine (GAE) ó Micro Cloud Foundry. Pero para ello todavía queda …. así que empezaré con Spring MVC.
Spring Android: Breve Introducción.
En este post voy a hablar sobre Spring Android ya que fue la opción más votada de la anterior entrada, gracias a la única persona que votó
.
Dentro del grupo de proyectos en los que está involucrado Spring podemos ver que hay dos que están enfocados al desarrollo de aplicaciones sobre dispositivos móviles, Spring Mobile y Spring Android. Mientras que Spring Mobile extiende Spring MVC para desarrollar aplicaciones web en dispositivos móbiles, Spring Android se centra en el desarrollo de aplicaciones Android nativas.
Por el momento vamos a explicar un poco como funciona Spring Android y dar algunos ejemplos, Spring Mobile quedará para otro post. Para las pruebas voy a utilizar la versión Spring Android 1.0.0.M2. El componente principal de Spring Android es el cliente REST llamado RestTemplate. Al crear una nueva instancia de RestTemplate el constructor establece una serie de objetos que hacen posible la funcionalidad para dicho cliente REST. Los objetos son los siguientes:
- HttpComponents HttpClient 4.x. Es el cliente http nativo para Android. El cliente está disponible a partir de la factoria establecida por defecto HttpComponentsClientHttpRequestFactory.
- JSON Marshalling. La funcionalidad requerida para realizar JSON marshalling viene dada por la clase MappingJacksonHttpMessageConverter de la librería externa Jackson JSON Proccesor. Hay que tener en cuenta que sólo tendremos acceso a la clase MappingJacksonHttpMessageConverter si previamente hemos añadido las dependencias a Jackson JSON Proccesor en nuestro classpath.
- XML Marshalling. La funcionalidad de XML marshalling viene dada por otra librería externa que hay que añadir como dependencia en nuestro classpath. Utilizamos la librería Simple XML Serializer y la clase SimpleXmlHttpMessageConverter para ello.
- RSS and Atom feed support. Al igual que los dos anteriores, necesitamos una librería externa. En este caso Android RomE Feed Reader junto a las clases SyndFeedHttpMessageConverter, RssChannelHttpMessageConverter y AtomFeedHttpMessageConverter.
Tras esta breve introducción tenemos que preparar nuestro entorno para desarrollar con Spring Android. Lo veremos en la siguiente entrada.

