A Simple Example


Before we dive into all of Spring's web-related features, let's get a simple page up and running that demonstrates some of the main concepts. We'll begin by creating a controller called SimpleController. This object will prepare a model and hand it over to the view (for example, a JSP). Our controller extends AbstractController, one of many built-in controllers predefining specific workflow when dealing with web interfaces. The AbstractController is one of the simpler ones. On receiving a request, SimpleController prepares a ModelAndView object, identifies the view (in this case called welcome), and adds a Date to the model. After returning the ModelAndView object, the controller has completed its work: having chosen a view and provided it with a model, based on information in the request that it can use to render the response.

import org.springframework.web.servlet.mvc.ModelAndView; import org.springframework.web.servlet.mvc.AbstractController;      public SimpleController extends AbstractController {        public ModelAndView handleRequestInternal(HttpServletRequest request,     HttpServletResponse response)    throws Exception {          // create a model-and-view using 'welcome’     ModelAndView mav = new ModelAndView("welcome");     // then, add an object to it and return it     mav.addObject("date", new Date());     return mav;   } }

You will need to wire up the controller in an ApplicationContext, as controllers and other web tier objects are configured using Spring's Dependency Injection. For objects responsible for handling web workflow, the application context must be an instance of the sub interface WebApplicationContext.

Like many other MVC frameworks, Spring's Web MVC framework is built around a generic servlet known as a Dispatcher Servlet or Front Controller. This pattern is explained in more detail by Crupi, Malks, and Alur in Core J2EE Patterns (Pearson Education, 2003). The DispatcherServlet is the endpoint a user connects to with their web browser. For now, assume that you will need to define the servlet in your web.xml file of your web application and create a file called [servlet-name]-servlet.xml in the same directory as the web.xml file (/WEB-INF/). The [servlet-name]-servlet.xml file defines the beans in your WebApplicationContext; hence our SimpleController will be defined there. The WebApplicationContext will contain all our web-related components. Its parent will be the application context containing middle-tier services and necessary supporting objects such as data sources. We will highlight the use of parent and child application contexts later in this chapter.

For our example, the contents of the [servlet-name].xml file (excluding XML headers) will be as follows:

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

The user connects to the web application through the DispatcherServlet, which delegates to the appropriate controller. The following piece of code is an excerpt from our web.xml file declaring the servlet and mapping all requests onto the DispatcherServlet:

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

We need to create the actual JSP to display something to the user. The following JSP uses the Java Standard Tag Library to display the Date that the SimpleController adds to the model:

<%-- welcome.jsp --%> <%@ taglib prefix="c" uri=http://java.sun.com/jstl/core %>     <html>   <head>     <title>Hello world!</title>   </head>   <body>     <h1>Hello world</h1>     <p>       Right now, the time is <c:out value="${date}"/>.     </p>   </body> </html>

As an alternative to JSP, you could equally implement the page using Freemarker or Velocity, or have iText generate a PDF, or POI generate an Excel file based on the model returned from the controller. Switching to Velocity templates, for example, would require changing the view class of the view resolver shown in the previous example to VelocityView and exchanging welcome.jsp for a welcome.vm Velocity template. It would not require any changes to controller or model, as Spring MVC cleanly separates model, view, and controller. We discuss a range of view technologies, including Velocity, JavaServer Pages, and XSLT in Chapter 13.



Professional Java Development with the Spring Framework
Professional Java Development with the Spring Framework
ISBN: 0764574833
EAN: 2147483647
Year: 2003
Pages: 188

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