CHAPTER 5: Building Java Web Applications with Spring Web MVC 243
Lines 25 to 28: This bean declaration tells the framework how to find physical
JSP files according to logical view names returned by the controllers, by
attaching the prefix and the suffix to a view name. For example, if a controller’s
method returns home as logical view name, then the framework will find a
physical file home.jsp under the /WEB-INF/views directory.
Line 30: <context:component-scan .../> tells the framework which packages
to be scanned when using an annotations-based strategy. Here the framework
will scan all classes under the package com.apress.helloworld. When the
application grows, you can add more configurations for business beans, DAOs,
transactions, and so on.
Now that we have the infrastructure in place to detect the controller that will handle the request,
it’s time to look at the controller.
Note Prior to Spring 2.5, one of the interface-based controllers was used. As of Spring 3.0, the interface-based
controllers have been deprecated in favor of annotated classes.
Listing 5-38 illustrates the code of the controller class HomeController generated by STS.
Listing 5-38. HomeController of the Hello World Application
- package com.apress.helloworld;
- import java.text.DateFormat;
- import java.util.Date;
- import java.util.Locale;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- /**
- Handles requests for the application home page.
- */
- @Controller
- public class HomeController {
- private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
- /**
- Simply selects the home view to render by returning its name.
- */
- @RequestMapping(value = "/", method = RequestMethod.GET)
- public String home(Locale locale, Model model) {