Learn Java for Web Development

(Tina Meador) #1
CHAPTER 5: Building Java Web Applications with Spring Web MVC 245







  1. The time on the server is ${serverTime}.










Listing 5-39 looks familiar. This is a simple JSP file that uses an EL expression on line 12 to print the
value of the variable serverTime that is passed by the controller.


As you may have noticed, STS created two Spring configuration files: root-context.xml and
servlet-context.xml. We have not looked at root-context.xml yet because our Hello World
application does not require this file to display the content of home.jsp. This file is empty by default,
as illustrated in Figure 5-19.


Figure 5-19. Generated root-context.xml


This file, as the name suggests, specifies the root configuration for the Spring container.
The root-context.xml file is loaded by the Spring’s ContextLoaderListener upon the application’s
start-up, as you learned in the previous section.


So far, we have gone through all the files generated by the Spring MVC Project template, so you
should be equipped enough to dive deeper, building the bookstore application along the way.


Implementing Spring Web MVC in the Bookstore Application

In this section, you will learn how to develop the bookstore web application using the Spring Web
MVC framework. The code for the application is available as a downloadable archive from the
Apress web site. As mentioned earlier, all incoming requests flow through DispatcherServlet.
Hence, like any other servlet in a Java EE application, the Java EE container needs to be informed
to load this servlet on start-up via web.xml. You have to create a new Spring MVC project first.
Listing 5-40 illustrates web.xml of the bookstore application.


Listing 5-40. web.xml



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

  2. <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi=
    "http://www.w3.org/2001/XMLSchema-instance"

  3. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

  4. version="3.0">

Free download pdf