Developing a Spring Framework MVC application(一)
This is a step-by-step account of how to develop a web application from scratch using the Spring Framework.
Prerequisites:
-
Java SDK (I am currently using version 1.4.2)
-
Ant (using version 1.6.2)
-
Apache Tomcat (using version 5.0.28)
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.
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 'projects' directory that I already had in my home directory so the full path to my directory is '/Users/trisberg/projects/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.
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 |
<html> |
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 |
<?xml version="1.0" encoding="UTF-8"?> |
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. A separate build script contains the app server specific tasks There are also tasks for controlling the application under Tomcat.
springapp/build.xml |
<?xml version="1.0"?> |
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 |
# Ant properties for building the springapp |
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.
[trisberg@localhost springapp]$ ant Buildfile: build.xml usage: [echo] springapp build file [echo] ----------------------------------- [echo] Available targets are: [echo] build --> Build the application [echo] deploy --> Deploy application as directory [echo] deploywar --> Deploy application as a WAR file [echo] install --> Install application in Tomcat [echo] reload --> Reload application in Tomcat [echo] start --> Start Tomcat application [echo] stop --> Stop Tomcat application [echo] list --> List Tomcat applications 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.
[trisberg@localhost springapp]$ ant deploy Buildfile: build.xml build: |
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.
[trisberg@localhost springapp]$ ant list Buildfile: build.xml list: |
If it is not listed, use the 'install' task to get the application installed in Tomcat.
[trisberg@localhost springapp]$ ant install Buildfile: build.xml install: [install] OK - Installed application at context path /springapp BUILD SUCCESSFUL Total time: 2 seconds |
Now open a browser and browse to http://localhost:8080/springapp/index.jsp.
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.2-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.
This completes the setup of the environment that is necessary, and now we can start actually developing our Spring Framework MVC application.
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 |
<?xml version="1.0" encoding="UTF-8"?> |
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 |
<?xml version="1.0" encoding="UTF-8"?> |
Step 7 – Copy jars to WEB-INF/lib
First create a 'lib' directory in the 'war/WEB-INF' directory. Then, from the Spring distribution, copy spring.jar (spring-framework-1.2/dist/spring.jar) to the new war/WEB-INF/lib directory. Also copy commons-logging jars to the war/WEB-INF/lib directory (spring-framework-1.2/lib/jakarta-commons/commons-logging.jar). We are also going to need a log4j jar. Copy log4j-1.2.9.jar to the war/WEB-INF/lib directory (spring-framework-1.2/lib/log4j/log4j-1.2.9.jar). These jars will be deployed to the server and they are also used during the build process.
Step 8 – Create your Controller
Create your Controller – I named mine SpringappController.java and placed it in the springapp/src directory.
springapp/src/SpringappController.java |
import org.springframework.web.servlet.mvc.Controller; |
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.
Step 9 – Build the Application
Run the 'build' task of the build.xml. Hopefully the code compiles OK.
[trisberg@localhost springapp]$ ant build Buildfile: build.xml build: [javac] Compiling 1 source file to /Users/trisberg/projects/springapp/war/WEB-INF/classes BUILD SUCCESSFUL Total time: 2 seconds |
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.2/samples/petclinic/war/WEB-INF/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 |
# For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml! |
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. (/Users/trisberg/jakarta-tomcat-5.0.28/logs/springapp.log)
2005-04-24 14:58:18,112 INFO [org.springframework.web.servlet.DispatcherServlet] - Initializing servlet 'springapp' |
Step 12 – Create a View
Now it is time to create our first view. I will use a JSP page that I decided to name hello.jsp. I'll put it in the war directory to begin with.
springapp/war/hello.jsp |
<html> |
Nothing fancy here, but it will do for now. Next we have to modify the SpringappController to forward to this view.
springapp/src/SpringappController.java |
import org.springframework.web.servlet.mvc.Controller; |
While I was modifying this class, I also added a logger so we can verify that we actually got here. Changes are highlighted in red. The model that this class returns is actually resolved via a ViewResolver. Since we have not specified a specific one, we are going to get a default one that just forwards to a url matching the name of the view specified. We will modify this later on.
Now compile and deploy the application. After instructing Tomcat to stop and then start the application, everything should get reloaded.
Let's try it in a browser – enter the url http://localhost:8080/springapp/hello.htm and we should see the following:
We can also check the log – I'm only showing the last entries, but we can see that the controller did get invoked and that it forwarded to the hello view. (/Users/trisberg/jakarta-tomcat-5.0.28/logs/springapp.log)
2005-04-24 15:01:56,217 INFO [org.springframework.web.servlet.DispatcherServlet] - FrameworkServlet 'springapp': initialization completed in 372 ms |
Summary
Let's take quick look at the parts of our application that we have created so far.
-
An introduction page index.jsp that does not do anything useful. It was just used to test our setup. We will later change this to actually provide a link into our application.
-
A DispatcherServlet with a corresponding springapp-servlet.xml configuration file.
-
A controller springappController.java with limited functionality – it just forwards a ModelAndView to the ViewResolver. Actually, we only have an empty model so far, but we will fix this later.
-
A view hello.jsp that again is extremely basic. But the whole setup works and we are now ready to add more functionality.
posted on 2006-08-09 23:21 topquan 閱讀(489) 評(píng)論(0) 編輯 收藏 所屬分類: Spring