Thursday, March 11, 2010

Setting up Spring MVC 3 Web Application

Now its time to code a bit in Skyphoto. So launch Eclipe and create a dynamic web project ready to be deployed on Tomcat 6. I am assuming that readers are atleast familiar with this. Next we will do a bit of configuration and throw in some Spring 3 jar files into the WEB-INF/lib folder.

Step 1 - Configure Spring Servlets and log4j in the web application deployment descriptor.


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="SkyPhotoWeb" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>SkyPhotoWeb</display-name>

<!-- Log4j config location -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/config/log4j.xml</param-value>
</context-param>
<!-- Log4j listener -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

<!-- Spring MVC Frontcontroller servlet -->
<servlet>
<servlet-name>skyphotoservlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>

/WEB-INF/config/spring-web.xml

</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>skyphotoservlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>



</web-app>



Step 2 - log4j.xml
The log4j configuration will be very useful later to see all framework and application messages. I have set it to debug level to help understand the framework better.

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

<!-- ====================================================================== -->
<!-- A P P E N D E R S -->
<!-- ====================================================================== -->

<!-- console -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd.MM.yyyy HH:mm:ss} *%-5p* %c{1}: %M - %m - (%F, line %L)%n"/>
</layout>
</appender>


<appender name="file" class="org.apache.log4j.FileAppender">
<param name="File" value="C:/logs/skyphotos.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd.MM.yyyy HH:mm:ss} *%-5p* %c{1}: %m (%F, line %L)%n"/>
</layout>
</appender>

<!-- ====================================================================== -->
<!-- L O G G E R S -->
<!-- ====================================================================== -->

<root>
<level value="debug" />
<appender-ref ref="console"/>
<appender-ref ref="file"/>
</root>

</log4j:configuration>

Step 3 - Setting up a bare minimal spring configuration file Listing 3 - spring-web.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">


<context:component-scan base-package="org.opengarage.skyphoto.web.controller" />


<bean id="jstlviewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

<bean id="annotationMapper"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
</bean>

<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>


</beans>




Step 4 - setup a simple jsp Listing 4 - index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Sky Photos Home</title>
</head>
<body>
<h1>Welcome to Sky Photos</h1>
<a href="#">Login</a>
</body>
</html>


Finally here is a view of the Eclipse project. You can very well make out the jar files you need for the time being.



Note 1 - that I am storing all configuration files under /WEB-INF/config



Note 2 - donot forget to download Spring framework and Security security distributions from their respective websites.


1 comments:

  1. I have been visiting various blogs for my term papers writing research. I have found your blog to be quite useful. Keep updating your blog with valuable information... Regards

    ReplyDelete