A typical request handler in Spring MVC is based on the @Controller
and @RequestMapping
annotations. Create a Java class, org.jboss.springmvc.controller.CatalogController
, annotated with the @Controller
annotation, which makes the class auto-detectable for injection by Spring. In the springmvcsrcmainwebappWEB-INFjboss-as-spring-mvc-context.xml
context file, which is listed and discussed later in the chapter, the component scanning for the org.jboss.springmvc.mvc
base package is configured, as follows:
<context:component-scan base-package="org.jboss.springmvc.controller" />
BeanFactory
injects a catalogController
bean when the CatalogController
class is required to handle a request. The handler class creates a model Map
with data and selects a view name to be rendered. The createAndDisplayCatalog
method takes the servlet request sent by DispatcherServlet
and returns an org.springframework.web.servlet.ModelAndView
object, which is a holder for Model
and View
in the Spring MVC framework. Map the servlet request onto the createAndDisplayCatalog
handler method using the @RequestMapping
annotation. Specify the path mapping URI as /catalog
.
Autowire the org.jboss.springmvc.data.CatalogDao
DAO interface to Spring's dependency injection mechanism using the @AutoWire
annotation. A catalogDaoImpl
bean is injected on startup as, by default, the application context pre-instantiates all singleton (the default scope in Spring) beans at startup. The createAndDisplayCatalog
method gets the request parameter values and creates a Catalog
object. The Catalog
object is stored using the autowired DAO object's persist method. Having created a model Map
, the createAndDisplayCatalog
method returns a org.springframework.web.servlet.ModelAndView
object with the View
catalog and the catalogDao
model. The org.springframework.web.servlet.ModelAndView
object is returned to DispatcherServlet
for the servlet to render the response. The view ID is resolved using the view resolver specified in the jboss-as-spring-mvc-context.xml
context file. The Map
model is made available to the view template. The controller class is listed in the following code:
package org.jboss.springmvc.controller; import org.jboss.springmvc.data.*; import org.jboss.springmvc.model.*; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class CatalogController { @Autowired private CatalogDao catalogDao; @RequestMapping(value = "/catalog") public ModelAndView createAndDisplayCatalog(HttpServletRequest request) { // Handle a new catalog (if any): String journal = request.getParameter("journal"); String publisher = request.getParameter("publisher"); String edition = request.getParameter("edition"); String title = request.getParameter("title"); String author = request.getParameter("author"); if (journal != null && publisher != null && edition != null && title != null && author != null) { Catalog catalog = new Catalog(); catalog.setJournal(journal); catalog.setPublisher(publisher); catalog.setEdition(edition); catalog.setTitle(title); catalog.setAuthor(author); catalogDao.persist(catalog); } // Prepare the result view (catalog): return new ModelAndView("catalog", "catalogDao", catalogDao); } }
The model, DAO, and controller classes are shown in Project Explorer in the following screenshot: