Spring in Practice

Embed Size (px)

Citation preview

  • 7/27/2019 Spring in Practice

    1/5

    Building web applications with Spring Web MVC

    Spring Web MVC background

    A review of the model-view-controller (MVC) pattern

    Model-view-controller (MVC) refers to the separation-of-concerns architectural pattern in which you

    separate your business services and domain objects (the model) from the UI (the view) and mediate

    their interaction through one or more controllers.

    An HTTP request comes into the controller. The controller accesses the model, possibly getting data,

    possibly updating the model, and possibly both. The controller then uses the view to generate a

    response, passing any relevant data it pulled out of the model. The client receives the generated

    response, and service is complete.

    What is Spring Web MVC?

    An architectural overview of Spring Web MVC

    The center of the Spring Web MVC universe is the DispatcherServlet, a front controller that

    dispatches requests to registered request handlers. The handlers can be UI controllers or endpoints

    for HTTP-based remote services. Each handler performs a service and then specifies a view to which

    the DispatcherServlet passes the request.

  • 7/27/2019 Spring in Practice

    2/5

    The request comes into the DispatcherServlet. Based on the request path, the DispatcherServlet

    figures out which of its registered handlers is the right one to service the request. It then passes the

    HTTP request (or, more abstractly, the user command or form data that the request represents) to ahandler for processing. The handler queries or updates the model (or both), and if appropriate the

    model returns the requested data. The handler then returns both the data and a logical view name

    back to the DispatcherServlet. The DispatcherServlet resolves the view name into an actual view and

    then passes the model data (if any) along to that view so it can be used in generating a response.

    Processing is complete, and the DispatcherServlet has serviced the request.

    Creating your first Spring Web MVC application

    Configuring the application

    1. 2. 3. 8.9. 10. main11. 12. org.springframework.web.servlet.DispatcherServlet13. 14. 15. 16. main17. /main/*

  • 7/27/2019 Spring in Practice

    3/5

    18. 19.

    Listing 3.1 Simple DispatcherServlet configuration in web.xml

    Youve defined a minimal DispatcherServlet, which again is Spring Web MVCs front controller, and

    youve indicated that you want to send /main/* requests to it. You didnt define a

    ContextLoaderListener in web.xml, so you might be wondering where the app context comes from.

    The answer is that each DispatcherServlet instance creates its own local app context using an XML

    configuration we provide, so heres that configuration.

    1. 2. 3. 8.9. 10. 13.

    Listing 3.2 /WEB-INF/main-servlet.xml, the DispatcherServlets local app context

    DispatcherServlet knows where to find this file by using a convention /WEB-INF/[servlet-name]-

    servlet.xml. You define the controller, RosterController, and a ViewResolver, which allows you to

    convert logical view names, such as foo, to views, such as /WEB-INF/jsp/foo.jsp. When dealing with

    JSP views in particular, its a good practice to place them somewhere inside the WEB-INF folder

    (WEB-INF/jsp is the official recommendation) so clients cant access them directly.

    A simple domain object

    1. publicclassMember {2. privateString firstName;3. privateString lastName;4.5. // constructors, getters, setters, etc.6. }

    Listing 3.3 Member.java

    Writing a basic controller

    1. importjava.util.*;2. importorg.springframework.stereotype.Controller;3. import org.springframework.ui.Model;4. importorg.springframework.web.bind.annotation.RequestMapping;5. import org.springframework.web.bind.annotation.RequestParam;6. import com.springinpractice.ch03.model.Member;7.8. @Controller9. publicfinalclassRosterController {10. privateList members = newArrayList();11. publicRosterController() {

  • 7/27/2019 Spring in Practice

    4/5

    12. members.add(newMember("John", "Lennon"));13. members.add(newMember("Paul", "McCartney"));14. members.add(newMember("George", "Harrison"));15. members.add(newMember("Ringo", "Starr"));16. }17.18. @RequestMapping19. publicvoidlist(Model model) {20. model.addAttribute(members);21. }22.23. @RequestMapping24. publicvoidmember(@RequestParam("id") Integer id, Model model) {25. model.addAttribute(members.get(id));26. }27.}

    Listing 3.4 RosterController.java

    You declare a Model parameter, which means Spring will automatically pass you a Model object

    (essentially it functions as a Map, even though it doesnt implement that interface). Anything you

    put on the Model will be available to the JSP as a JSP expression language (EL) variable. Here, youve

    placed the list of members on the Model. When you place objects on the model, theyre stored as

    name/value pairs. Here, you havent explicitly assigned a name to the attribute, so Spring will

    automatically generate the name by convention. Here, because the type is List, the

    generated name is memberList. Youll be able to access it from your JSP using ${memberList}. On line

    25, the name will be member and will be available to the JSP using ${member}.

    By convention, the request paths for the methods are /roster/list[.*] and /roster/ member[.*]

    respectively, where * is any extension. The view names are list and member respectively.

    Implementing the master and details views

    1. 2. 3. 4. Roster5. 6. 7. Roster8. 9. 10. 11. 12. 13. 14. 15. 16. 17.18.

    Listing 3.5 /WEB-INF/jsp/roster/list.jsp

    1. 2. 3. 4. Member: ${member}5.

  • 7/27/2019 Spring in Practice

    5/5

    6. 7. Member: ${member}8.

    Back

    9. 10.

    Listing 3.6 /WEB-INF/jsp/roster/member.jsp

    URL:http://localhost:8080/sip/main/roster/list.do.

    Serving and processing forms

    Using domain objects as form beans

    If possible use domain objects as form beans to avoid duplication.

    http://localhost:8080/sip/main/roster/list.dohttp://localhost:8080/sip/main/roster/list.dohttp://localhost:8080/sip/main/roster/list.dohttp://localhost:8080/sip/main/roster/list.do