25.02.2015 Views

Introducing Spring Framework

Introducing Spring Framework

Introducing Spring Framework

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 10 ■ Showing Your <strong>Spring</strong> Application on the Web<br />

After creating the web application structure (this is the standard structure for a web application) and before you<br />

continue with the code, let’s talk about the MVC design pattern and how the <strong>Spring</strong> MVC extension helps you to use<br />

this pattern effectively.<br />

<strong>Spring</strong> MVC<br />

The <strong>Spring</strong> <strong>Framework</strong> provides the <strong>Spring</strong> MVC extension that supports the MVC pattern and features such as i18n,<br />

support for different view engines, theming, and others. Figure 10-1 shows the <strong>Spring</strong> MVC at a high level.<br />

Incoming<br />

Request<br />

Return<br />

Response<br />

Return<br />

Control<br />

Front Controller<br />

DispatcherServlet<br />

Model<br />

Render<br />

Response<br />

Delegate<br />

Request<br />

Model<br />

Delegate<br />

Rendering of<br />

Response<br />

Handle<br />

Request<br />

Controller<br />

@Controller<br />

Create<br />

Model<br />

View Template<br />

HTML/JSP/JSPX<br />

3rd Party Engines<br />

Figure 10-1. <strong>Spring</strong> MVC<br />

Figure 10-1 shows how the MVC design pattern works; first there is a request from the user to the front controller<br />

(the <strong>Spring</strong>’s DispatcherServlet); then it delegates its request to a controller (your own implementation) that will<br />

create a model; then it will delegate the response to the view, in this case the view template (a JSP/JSPX) that will<br />

return the control to the front controller and return the response to the user.<br />

But let’s start coding, so we can see your <strong>Spring</strong> application on the Web. First, you are going to start creating a<br />

controller that will create the model, and then this controller will delegate the rendering response (with the name of<br />

the view) through the DispatcherServlet. Listing 10-3 shows the delegate.<br />

Listing 10-3. SearchController.java<br />

package com.apress.isf.spring.web;<br />

import org.springframework.beans.factory.annotation.Autowired;<br />

import org.springframework.stereotype.Controller;<br />

import org.springframework.ui.Model;<br />

import org.springframework.web.bind.annotation.RequestMapping;<br />

import org.springframework.web.bind.annotation.RequestMethod;<br />

import com.apress.isf.spring.data.DocumentDAO;<br />

135

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!