Recently our marketing decided that they urgently need an application which will retrieve certain statistics from our database and return them as excel files. As long as our main application historicly does not really use any web framework (thus evolving into a framework on its own), I've decided that it is a really good time to continue introduction of Spring framework to our codebase. Spring framework is quite clean and useful dependency injection framework which also has a lot of stuff (like JdbcTemplate class and whole data access package) which really make a life of a developer easier. Dependency injection here basicly means that we can write special configuration file which allow us to create instances of classes we need with all the necessary parameters set via set methodsor constructor parameters - really simple and cool.
So the two main things which made this application not absolutely trivial were 1) the application should be integrated with JNDI DataSource deployed in the JBoss application server 2) it should return statistics as excel file. But before starting doing that we need to create a trivial Spring application which will be a base for further development. To start working with Spring in a web application we should add necessary spring libraries (in our case they are - spring-core.jar, spring-web.jar, spring-webmvc.jar and spring.jar) and add the following code to the web.xml configuration file:
<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet> <servlet-name>dispatch</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatch</servlet-name> <url-pattern>/get/*</url-pattern> </servlet-mapping>The whole concept behind that is quite trivial: we added a listener class which is being called when application is initialized, loads bean configuration from configuration file (applicationContext.xml by default) and loads the [servletname]-servlet.xml configuration files for the servlets with [servletname] defined in web.xml. To make it work properly we also need to create an aplicationContext.xml file (which we will leave empty) and dispatch-servlet.xml file which configures the main servlet of the application:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/*">dispatchController</prop>
</props>
</property>
</bean>
<bean id="dispatchController"
class="com.goaiss.internal.stats.web.DispatchController">
</bean>
</beans>
And again nothing really magical happens here, we define a view resolver - a class which responsible for locating a resulting view for a request - in the most simple case it is a jsp file. Than we tell application that all the urls coming to this servlet will be handled by a controller named dispatchController which we define in the last bean declaration. After doing that we just need to create a simple jsp file e.g. test.jsp, DispatchController class (yeah, that's true you need to occasionally write java code while dealing with Spring) which will be super tiny:
public class DispatchController extends MultiActionController {
public ModelAndView actionTest(HttpServletRequest request,
HttpServletResponse response)
throws Exception {
return new ModelAndView("test");
}
}
and now we are ready to put a link to index.jsp file e.g. <a href="get/actionTest">Hello world!</a> and say "Hello world" in the most simple Spring application :)
Eclipse project for the this application can be found here



