This post explains how to implement a simple web application with Spring 3.0 on Tomcat.
I used the following tools:
- Java 1.6.0_22
- Tomcat 6.0.20
- Spring 3.0.4
- NetBeans 6.9
-
- In your IDE create a project called Hello World
-
- Add the following libraries:
- commons-logging.jar
- org.springframework.aop-3.0.4.RELEASE.jar
- org.springframework.asm-3.0.4.RELEASE.jar
- org.springframework.aspects-3.0.4.RELEASE.jar
- org.springframework.beans-3.0.4.RELEASE.jar
- org.springframework.context-3.0.4.RELEASE.jar
- org.springframework.context.support-3.0.4.RELEASE.jar
- org.springframework.core-3.0.4.RELEASE.jar
- org.springframework.expression-3.0.4.RELEASE.jar
- org.springframework.instrument-3.0.4.RELEASE.jar
- org.springframework.instrument.tomcat-3.0.4.RELEASE.jar
- org.springframework.jdbc-3.0.4.RELEASE.jar
- org.springframework.jms-3.0.4.RELEASE.jar
- org.springframework.orm-3.0.4.RELEASE.jar
- org.springframework.oxm-3.0.4.RELEASE.jar
- org.springframework.test-3.0.4.RELEASE.jar
- org.springframework.transaction-3.0.4.RELEASE.jar
- org.springframework.web-3.0.4.RELEASE.jar
- org.springframework.web.portlet-3.0.4.RELEASE.jar
- org.springframework.web.servlet-3.0.4.RELEASE.jar
- org.springframework.web.struts-3.0.4.RELEASE.jar
- Add the following libraries:
-
- Create the file /WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>HelloWorld</display-name> <servlet> <servlet-name>HelloWorld</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>HelloWorld</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
- Create the file /WEB-INF/web.xml
-
- Create the file /WEB-INF/HelloWorld-servlet.xml
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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-3.0.xsd"> <context:component-scan base-package="it.helloworld.controller" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/views/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans>
- Create the file /WEB-INF/HelloWorld-servlet.xml
-
- Create the file index.jsp in the application root
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>Welcome Page</title> </head> <body> <jsp:forward page="helloWorld.html" /> </body> </html>
- Create the file index.jsp in the application root
-
- Create the directory /WEB-INF/views and the file /WEB-INF/views/helloWorld.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!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=UTF-8"> <title>JSP Page</title> </head> <body> <h1>Hello World!</h1> </body> </html>
- Create the directory /WEB-INF/views and the file /WEB-INF/views/helloWorld.jsp
-
- Create the file /WEB-INF/classes/it/helloworld/controller/HelloWorldController.java
package it.helloworld.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class HelloWorldController { @RequestMapping(value={"/index", "/helloWorld"}) public ModelAndView helloWorld() { return new ModelAndView("helloWorld"); } }
- Create the file /WEB-INF/classes/it/helloworld/controller/HelloWorldController.java
- Deploy and launch the application
Leave a Reply