Spring MVC Step by Step

  • Published on
    10-Apr-2015

  • View
    3.176

  • Download
    5

Embed Size (px)

Transcript

<p>Developing a Spring Framework MVC application step-by-stepThomas Risberg July, 2003</p> <p>This is a step-by-step account of how to develop a web application from scratch using the Spring Framework. It is divided into a number of parts. You can read them in sequence or skip one if you are already familiar with the concepts covered in it.</p> <p>Part 1 - Basic Application and Environment Setup</p> <p>Part 2 Developing and Configuring the Application</p> <p>Part 3 Adding Unit Tests and a Form to the Application</p> <p>Part 4 Implementing Database Persistence</p> <p>Copyright 2003, Thomas Risberg</p> <p>http://www.springframework.org/docs/MVC-step-by-step/Spring-MVC-step-by-step.html [13/10/2004 9:57:08 PM]</p> <p>Developing a Spring Framework MVC application step-by-stepPart 1 Basic Application and Environment Setup Thomas Risberg July, 2003</p> <p>This is a step-by-step account of how to develop a web application from scratch using the Spring Framework. Prerequisites:r r r</p> <p>Java SDK (I am currently using version 1.4.2) Ant (using version 1.5.3) Apache Tomcat (using version 4.1.24 or 5.0.18)</p> <p>You should also be reasonably comfortable using the above software. I am not going to cover a lot of background information or theory in this document -- there are plenty of books available that covers this in depth. Instead we will dive right into developing the application.</p> <p>Step 1 development directory We are going to need a place to keep all the source and other files we will be creating, so I create a directory that I name 'springapp'. You can place this directory in your home folder or in some other location. I created mine in a 'workspace' directory that I already had in my home directory so the full path to my directory is '/home/trisberg/workspace/springapp'. Inside this directory I create a 'src' directory to hold all Java source files. Then I create another directory that I name 'war'. This directory will hold everything that should go into the WAR file, that we would use to deploy our application. All source files other than Java source, like JSPs and configuration files, belongs in this directory.</p> <p>Step 2 index.jsp I will start by creating a JSP page named 'index.jsp' in the war directory. This is the entry point for our application.springapp/war/index.jsp</p> <p> Example :: Spring Application Example - Spring Application This is my test. Just to have a complete web application, I create a web.xml in a WEB-INF directory that I create under the war directory.springapp/war/WEB-INF/web.xml</p> <p>Step 3 deploying the application to Tomcat Next, I write an Ant build script that we are going to use throughout this document. There are tasks for building and deploying the application. There are also tasks for controlling the application under Tomcat.springapp/build.xml</p> <p>http://www.springframework.org/docs/MVC-step-by-step/Spring-MVC-step-by-step-Part-1.html (1 of 12) [13/10/2004 9:57:10 PM]</p> <p> --&gt; --&gt; --&gt;</p> <p>http://www.springframework.org/docs/MVC-step-by-step/Spring-MVC-step-by-step-Part-1.html (2 of 12) [13/10/2004 9:57:10 PM]</p> <p>http://www.springframework.org/docs/MVC-step-by-step/Spring-MVC-step-by-step-Part-1.html (3 of 12) [13/10/2004 9:57:10 PM]</p> <p> This script now contains all the targets that we are going to need to make our development efforts easier. I am not going to cover this script in detail since most if not all of it is pretty much standard Ant and Tomcat stuff. You can just copy the above build file and put it at the root of your development directory tree. We also need a build.properties file that you should customize to match your server installation. This file belongs in the same directory as the build.xml file.springapp/build.properties</p> <p># Ant properties for building the springapp deploy.path=/home/trisberg/jakarta-tomcat-4.1.24/webapps #deploy.path=c:/Tomcat 4.1/webapps #deploy.path=c:/bea/user_projects/domains/mydomain/applications tomcat.home=/home/trisberg/jakarta-tomcat-4.1.24 #tomcat.home= c:/Tomcat 4.1 tomcat.manager.url=http://localhost:8080/manager tomcat.manager.username=admin tomcat.manager.password=tomcat If you are on a system where you are not the owner of the Tomcat install, then the Tomcat owner must either grant you full access to the webapps directory or the owner must create a new directory named 'springapp' in the 'webapps' directory of the Tomcat installation, and also give you full rights to deploy to this newly created directory. On Linux I run the command chmod a+rwx springapp to give everybody full rights to this directory. If you are using a different web application server, then you can remove the Tomcat specific tasks at the end of the build script. You will have to rely on your server's hot deploy feature, or you will have to stop and start your application manually. Now I run Ant to make sure that everything is working OK. You should have your current directory set to the 'springapp' directory.</p> <p>http://www.springframework.org/docs/MVC-step-by-step/Spring-MVC-step-by-step-Part-1.html (4 of 12) [13/10/2004 9:57:10 PM]</p> <p>[trisberg@localhost springapp]$ ant Buildfile: build.xml usage: [echo] springapp build file [echo] ----------------------------------[echo] Available targets are: [echo] [echo] [echo] [echo] [echo] [echo] [echo] [echo] build deploy deploywar install reload start stop list --&gt; --&gt; --&gt; --&gt; --&gt; --&gt; --&gt; --&gt; Build the application Deploy application as directory Deploy application as a WAR file Install application in Tomcat Reload application in Tomcat Start Tomcat application Stop Tomcat application List Tomcat applications</p> <p>BUILD SUCCESSFUL Total time: 2 seconds Last action here is to do the actual deployment. Just run Ant and specify 'deploy' or 'deploywar' as the target.</p> <p>[trisberg@localhost springapp]$ ant deploy Buildfile: build.xml deploy: [copy] Copying 1 file to /home/trisberg/jakarta-tomcat-4.1.24/webapps/springapp BUILD SUCCESSFUL Total time: 2 seconds</p> <p>Step 4 Test the application Let's just quickly start Tomcat and make sure that we can access the application. Use the 'list' task from our build file to see if Tomcat has picked up the new application.</p> <p>[trisberg@localhost springapp]$ ant list Buildfile: build.xml list: [list] OK - Listed applications for virtual host localhost [list] /admin:running:0:../server/webapps/admin [list] /webdav:running:0:/home/trisberg/jakarta-tomcat-4.1.24/webapps/webdav [list] /springapp:running:0:/home/trisberg/jakarta-tomcat-4.1.24/webapps/springapp/ [list] /examples:running:0:examples [list] /tomcat-docs:running:0:/home/trisberg/jakarta-tomcat-4.1.24/webapps/tomcat-docs</p> <p>http://www.springframework.org/docs/MVC-step-by-step/Spring-MVC-step-by-step-Part-1.html (5 of 12) [13/10/2004 9:57:10 PM]</p> <p>[list] /:running:0:/home/trisberg/jakarta-tomcat-4.1.24/webapps/ROOT [list] /manager:running:0:../server/webapps/manager</p> <p>BUILD SUCCESSFUL Total time: 1 second If it is not listed, use the 'install' task to get the application installed in Tomcat.</p> <p>[trisberg@localhost springapp]$ ant install Buildfile: build.xml install: [install] OK - Installed application at context path /springapp</p> <p>BUILD SUCCESSFUL Total time: 2 seconds</p> <p>Now open a browser and browse to http://localhost:8080/springapp/index.jsp.</p> <p>http://www.springframework.org/docs/MVC-step-by-step/Spring-MVC-step-by-step-Part-1.html (6 of 12) [13/10/2004 9:57:10 PM]</p> <p>Step 5 Download Spring distribution If you have not already downloaded the Spring Framework Release file, now is the time to do so. I am currently using 'spring-framework-1.0-with-dependencies.zip' that can be downloaded from www.springframework.org/download.html. I unzipped this file in my home directory. We are going to use several files from this download later on.</p> <p>This completes the setup of the environment that is necessary, and now we can start actually developing our Spring Framework MVC application.</p> <p>Step 6 Modify web.xml in WEB-INF directory Go to the 'springapp/war/ WEB-INF' directory. Modify the minimal 'web.xml' file that we created earlier. Now we will modify it to suit our needs. We define a DispatcherServlet that is going to control where all our request are routed based on information we will enter at a later point. It also has a standard servlet-mapping entry that maps to the url patterns that we will be using. I have decided to let any url with an '.htm' extension be routed to the 'springapp' dispatcher.springapp/war/WEB-INF/web.xml</p> <p> springapp org.springframework.web.servlet.DispatcherServlet 1 springapp *.htm index.jsp Next, create a file called 'springapp-servlet.xml' in the springapp/war/WEB-INF directory (you can copy an example of this file from the Spring distributions sample/skeletons/webapp-minimal directory). This is the file where definitions used by the DispatcherServlet should be entered. It is named based on the servlet-name from web.xml with '-servlet' appended. This is a standard naming convention used in the Spring Framework. Now, add a bean entry named springappController and make the class SpringappController. This defines the controller that our application will be using. We also need to add a url mapping so the DispatcherServlet knows which controller should be invoked for different url:s.springapp/war/WEB-INF/springapp-servlet.xml</p> <p>http://www.springframework.org/docs/MVC-step-by-step/Spring-MVC-step-by-step-Part-1.html (7 of 12) [13/10/2004 9:57:10 PM]</p> <p> springappController </p> <p>Step 7 Copy jars to WEB-INF/lib From the Spring distribution, copy spring.jar (spring-framework-1.0/dist/spring.jar) to a war/WEB-INF/lib directory. Also copy commons-logging jars to the war/WEB-INF/lib directory (spring-framework-1.0/lib/jakarta-commons/commons-logging.jar). We are also going to need a log4j jar. Copy log4j-1.2.8.jar to the war/WEB-INF/lib directory (spring-framework-1.0/lib/log4j/log4j-1.2.8.jar). These jars will be deployed to the server and they are also used during the build process.</p> <p>Step 8 Create your Controller Create your Controller I named mine SpringappController.java and placed it a new springapp/src directory.springapp/src/SpringappController.java</p> <p>import org.springframework.web.servlet.mvc.Controller; import org.springframework.web.servlet.ModelAndView; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class SpringappController implements Controller { public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { return new ModelAndView(""); } } This is as basic a Controller as you can use. We will be expanding this later on, and we will also later on extend some provided abstract base implementations. The Controller handles the request and returns a ModelAndView. We have not yet defined any Views, so right now there is nothing to do.</p> <p>Step 9 Build the Application Run the 'build' task of the build.xml. Hopefully the code compiles OK.</p> <p>http://www.springframework.org/docs/MVC-step-by-step/Spring-MVC-step-by-step-Part-1.html (8 of 12) [13/10/2004 9:57:10 PM]</p> <p>[trisberg@localhost springapp]$ ant build Buildfile: build.xml build: [javac] Compiling 1 source file to /home/trisberg/workspace/springapp/war/WEB-INF/classes BUILD SUCCESSFUL Total time: 2 seconds</p> <p>Step 10 Copy and modify log4j.properties The Spring Framework uses log4j for logging so we have to create a configuration file for log4j. Copy the log4j.properties from the sample Petclinic application (spring-framework-1.0/samples/petclinic/war/WEB-INF/classes/log4j.properties) to the war/WEB-INF/classes directory (this directory should have been created in the previous step). Now uncomment or modify the log4j.rootCategory property and change the name and location of the logfile that will be written. I decided to have it written to the same directory as all other Tomcat logs.springapp/war/WEB-INF/classes/log4j.properties</p> <p>log4j.rootCategory=INFO, stdout, logfile log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %n log4j.appender.logfile=org.apache.log4j.RollingFileAppender log4j.appender.logfile.File=/home/trisberg/jakarta-tomcat-4.1.24/logs/springapp.log log4j.appender.logfile.MaxFileSize=512KB # Keep three backup files log4j.appender.logfile.MaxBackupIndex=3 log4j.appender.logfile.layout=org.apache.log4j.PatternLayout #Pattern to output : date priority [category] - line_separator log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %n</p> <p>Step 11 Deploy Application Run the 'deploy' task and then the 'stop' and 'start' tasks of the build.xml. This will force a reload of the application. We have to check the Tomcat logs for any deployment errors there could be typos in the above xml files or there could be missing classes or jar files. This is an example of what it should look like. (/home/trisberg/jakarta-tomcat-4.1.24/logs/springapp.log) 2003-07-01 17:34:43,702 INFO [org.springframework.web.servlet.DispatcherServlet] - 2003-07-01 17:34:43,747 INFO [org.springframework.beans.CachedIntrospectionResults] - 2003-07-01 17:34:43,800 INFO [org.springframework.web.servlet.DispatcherServlet] - 2003-07-01 17:34:43,837 INFO [org.springframework.web.context.support.XmlWebApplicationContext] - 2003-07-01 17:34:43,869 INFO [org.springframework.beans.factory.xml.XmlBeanFactory] - 2003-07-01 17:34:43,928 INFO [org.springframework.beans.factory.xml.XmlBeanFactory] - 2003-07-01 17:34:43,929 INFO [org.springframework.beans.CachedIntrospectionResults] - 2003-07-01 17:34:43,947 INFO [org.springframework.web.context.support.XmlWebApplicationContext] - 2003-07-01 17:34:43,947 INFO [org.springframework.web.context.support.XmlWebApplicationContext] - </p> <p>http://www.springframework.org/docs/MVC-step-by-step/Spring-MVC-step-by-step-Part-1.html (9 of 12) [13/10/2004 9:57:10 PM]</p> <p>2003-07-01 17:34:43,948 INFO [org.springframework.web.context.support.XmlWebApplicationContext] - 2003-07-01 17:34:43,948 INFO [org.springframework.web.context.support.XmlWebApplicationContext] - 2003-07-01 17:34:43,949 INFO [org.springframework.web.context.support.XmlWebApplicationContext] - 2003-07-01 17:34:43,951 WARN [org.springframework.web.context.support.XmlWebApplicationContext] - 2003-07-01 17:34:43,963 WARN [org.springframework.ui.context.support.UiApplicationContextUtils] - 2003-07-01 17:34:43,970 INFO [org.springframework.web.servlet.DispatcherServlet] - 2003-07-01 17:34:43,972 INFO [org.springframework.web.servlet.DispatcherServlet] - 2003-07-01 17:34:43,976 INFO [org.springframework.web.servlet.DispatcherServlet] - 2003-07-01 17:34:43,986 INFO [org.springframework.web.servlet.DispatcherServlet] - 2003-07-01 17:34:43,986 INFO [org.springframework.web.servlet.DispatcherServlet] - 2003-07-01 17:34:44,214 INFO [org.springframework.web.servlet.DispatcherS...</p>