Spring Setup for Time Expression


Now that I have provided you some fundamental concepts for Spring MVC, let's begin setting it up for development of Time Expression screens.

We need a couple of components to get Spring up and running for us. Figure 7.1 showed the Time Expression high-level architecture we established early in this book. As you can see, we need a servlet container that Spring can run within for our web application. So let's start with the installation of a Servlet container first, and then we will download and install the Spring Framework.

Installing a Servlet Container (Apache Tomcat)

I have chosen to use Apache Tomcat (http://tomcat.apache.org/) as the Servlet container for the Time Expression application. However, you can use any other product you want; this can be a servlet-container-only product, such as Tomcat, or a full-blown application server, such as JBoss Application Server, BEA WebLogic, or IBM Websphere.

Note

If you have been following along the examples in this book, you will recall the timex/local.properties file used by our Ant build.xml file (both files are provided in this book's code zip file). Note the deploy.dir property in the timex/local.properties file; this can be adjusted to point to your servlet container's deployment directory. For example, in my case, the deploy.dir property is set up as shown here:

deploy.dir=/apache-tomcat-5.5.15/webapps



Now we can run the ant deploy from a command line using our build.xml file.

By running this ant command, a fresh new timex.war web archive file will be built and deployed to the specified directory (in deploy.dir).

Hot Deploying WAR Files and HTTP Mock Style Testing

In 2001, I wrote an article titled "How Many Times Do You Restart Your Server During Development?" (http://www.javaworld.com/javaworld/jw-04-2001/jw-0406-soapbox.html). Although various servlet containers or application servers handle reloading of applications differently, restarting the server every time you make a change to your application can become a waste of time. Much of this has to do with the way Java's class loading works, but it still doesn't make it any less frustrating.

If your server doesn't (hot) redeploy your war files successfully, you could consider tweaking your style of coding and testing. One good alternative (discussed in this chapter) is to use Spring's mock classes to simulate a HTTP request and use JUnit to unit test the code instead of relying completely on the web application server for your testing.

Incidentally, I recently came across an option for Apache Tomcat that will enable to us to avoid restarts when deploying our application. This can be activated by setting the following attributes in the conf/context.xml file found under the Tomcat install directory, <Context antiJARLocking="true" antiResourceLocking="true">.

Documentation on these attributes can be found at http://tomcat.apache.org/tomcat-5.5-doc/config/context.html#Standard%20Implementation.

Alternatively, we could use the Tomcat Ant deploy tasks; however, I wanted to keep our build.xml generic for most web servers. Nevertheless, documentation on these tasks can be found at the tomcat.apache.org website.


Installing the Spring Framework

By now, you should have a thorough understanding of what Spring can do for you. Next, it is time to download Spring, install it, and begin using it!

The Spring Framework can be downloaded from http://springframework.org. We will now follow the instructions provided on the website to download and install it.

The following are one-time setup steps we will need to follow to get Spring set up for our environment. From here, you might add external jars for added Spring functionality as needed to the timex/lib/ directory. (In Chapter 10, "Beyond the Basics," we will add OpenSymphony's quartz.jar file to our directory.)

  • Spring Copy spring.jar to the timex/lib/ directory of Time Expression, based on the directory structure we established in Chapter 3, "XP and AMDD-Based Architecture and Design Modeling," and shown here in Figure 7.4.

    Figure 7.4. Development directory structure for Time Expression.

  • JSTL We also need to obtain JavaServer Pages Standard Tag Library (JSTL), which is part of the Jakarta taglibs project and can be downloaded from http://jakarta.apache.org/taglibs/. After downloading this package, copy the jstl.jar and standard.jar files to the timex/lib/ directory. JSTL helps eliminate (or at least significantly reduces) the amount of embedded scriptlet code in our JSP files. For example, JSTL provides tags for iterations/loops (<forEach>, for example), conditional tags (<if>, for example), formatting tags (fmt:formatDate, for example), and several other tags. You will see examples of many of these tags in this chapter.

Running Our SpringTest

Incidentally, the three files we discussed in the previous chapter can now be created in the following paths, and we could run ant springtest (from our timex/ top-level directory) to test that we can use Spring in our code. The complete code for these files can be found in this book's code zip file:

  • src/conf/springtest-applicationcontext.xml

  • src/java/com/visualpatterns/timex/test/SpringTest.java

  • src/java/com/visualpatterns/timex/test/SpringTestMessage.java

Configuring Spring MVC

Now that we have the servlet container and Spring software installed, we need to configure Spring MVC so that we can begin developing and deploying the Time Expression sample application.

Configure DispatcherServlet in web.xml

The very first thing we need to do is to have all incoming HTTP requests (that match a certain pattern) forwarded to Spring MVC, by Tomcat.

The following excerpt from our web.xml file demonstrates how we can configure all requests ending with an .htm extension to be processed by the Spring's org.springframework.web.servlet.DispatcherServlet class:

<servlet>     <servlet-name>timex</servlet-name>     <servlet-class>         org.springframework.web.servlet.DispatcherServlet     </servlet-class>     <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping>     <servlet-name>timex</servlet-name>     <url-pattern>*.htm</url-pattern> </servlet-mapping>


Later on we will see how requests with a .jsp extension are handled by Spring's DispatcherServlet.

Note

Our Spring application context file, timex-servlet.xml, will automatically be searched for and loaded by Spring for us.

This file is stored under timex/src/conf but automatically copied to the timex/build/timex/WEB-INF/ directory by our Ant build.xml file when the build, dist, or deploy targets are used.


Create Spring's Application Context XML File (timex-servlet.xml)

Now we need to create our application context XML file, timex-servlet.xml. We will review various parts of this file throughout the remainder of this chapter. You will see how this file quickly becomes an essential part of working with Spring MVC.

The following excerpt from timex-servlet.xml shows how we configure a Spring view resolver to resolve logical view names to the physical view (JSP) file:

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


Note

By storing our JSP files in the build/timex/WEB-INF/jsp/ directory, we are essentially hiding these files so they cannot be accessed directly from a web browser using their actual filenames (that is, only views ending with .htm are mapped to these files). To access .jsp files directly, they must be placed a couple of levels up, under build/timex/, the same location where our welcome file, index.jsp, will reside.

Hiding files is a security precautionary measure. Appendix D, "Securing Web Applications," provides additional security guidelines.




Agile Java Development with Spring, Hibernate and Eclipse
Agile Java Development with Spring, Hibernate and Eclipse
ISBN: 0672328968
EAN: 2147483647
Year: 2006
Pages: 219

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net