1 / 32

Spring Web MVC

Spring Web MVC. Part 1. Ievads. T īmekļa lietojumprogrammas ir iespējams programmēt pa tiešo izmantojot Java Servlet un JSP tehnoloģijas Eksistē vairāki tīmekļa lietojumprogrammu izstrādes ietvari ( web application frameworks ), kuri atbalsta dinamisko tīmekļa lietojumprogrammu izstrādi

necia
Download Presentation

Spring Web MVC

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Spring Web MVC Part 1

  2. Ievads • Tīmekļa lietojumprogrammas ir iespējams programmēt pa tiešo izmantojot Java Servlet un JSP tehnoloģijas • Eksistē vairāki tīmekļa lietojumprogrammu izstrādes ietvari (web application frameworks), kuri atbalsta dinamisko tīmekļa lietojumprogrammu izstrādi • Piemēri: Spring MVC , JSF, Play!, Lift, Struts, Tapestry, Stripes, Wicket un daudzi citi

  3. Spring • Spring iratklāta koda Java/JavaEE lietojumprogrammu izstrādes ietvars • Viens no interesantākiem komponentiem ir Spring Web MVC http://docs.spring.io/spring/docs/3.2.5.RELEASE/spring-framework-reference/htmlsingle/#overview

  4. Tīmekļa lietojumprogrammu uzdevumi • Tipiskie un svarīgie uzdevumi, kuri ir jāatrisina izstrādājot tīmekļa lietojumprogrammu : • Stāvokļu pārvaldība (state management) • Darba plūsma (workflow) • Validācija • Spring MVC ietvars ir uzprojektēts, lai palīdzēt izstrādātājiem risināt šīs problēmas

  5. Model-View-Controller Pattern • Model-View-Controller (MVC) ir arhitektūras šablons (aprakstīts 1979. gadā, Smalltalk valodā) • Pamata mērķis: atdalīt datus (Model) no lietotāja interfeisa (View) • MVC atrisina problēmu ieviešot starpnieka komponenti - kontrolieri (Controller)

  6. Model-View-Controller Pattern • Model - Represents enterprise data and the business rules. • View - Renders the contents of a model. Accesses enterprise data through the model and specifies how that data should be presented. • Controller - Translates interactions with the view (button clicks, menu selections) into actions to be performed by the model (activating processes, changing the state of the model).

  7. Spring MVC application architecture • Spring MVC applications are broken down into a series of layers • A layer is a discrete, orthogonal area of concern within an application General, high-level layers in a web application

  8. Spring MVC application layers • Typical Spring MVC applications have at least five layers of abstraction • Isolating problem domains into separate layers creates a flexible and testable application • The interface is a contract for a layer, making it easy to keep implementations and their details hiddenwhile enforcing correct layer usage

  9. Spring MVC application layers • The user interface layer (also known as the view) is responsible for rendering output for the client • The web layer manages the user’s navigation through the site • The service layer provides a stateless, coarse-grained interface for clients to use for system interaction • The domain model is the collection of nouns in the system, it also contains the business logic of the system • The data access layer is responsible for interfacing with the persistence mechanism to store and retrieve instances of the object model

  10. Spring MVC komponenti Spring MVC sastāv no vairākiem komponentiem, starp kuriem notiek sadarbība apstrādājot klienta pieprasījumu

  11. Pieprasījuma dzīves cikls (1) • Uz serveri atnāk HTTP pieprasījumus, kurš tiek nodots DispatcherServletkomponentam • Visi pieprasījumi vienmēr tiek nodoti vienam servletam, kuru sauc par priekšējo kontrolieri (front controller)

  12. Front Controller • At the heart of Spring MVC is DispatcherServlet, a servlet that functions as a front controller • A front controller is a common web-application pattern where a single servlet delegates responsibility for a request to other components of an application to perform the actual processing

  13. Pieprasījuma dzīves cikls (2) • Komponents, kurš atbild par pieprasījuma apstrādi ir Controller • Lai nolemt kuram kontrolierim atdot pieprasījumu, DispatcherController izmanto HandlerMapping komponentus • HandlerMapping: URL piesaistīšana kontrolieriem

  14. Pieprasījuma dzīves cikls (3-4) • Kad DispatcherServlet ir izvēlējies kontroliera objektu, tad parsūta tam pieprasījumu, lai kontrolieris izpilda biznesa loģiku • Pēc darba pabeigšanas Controller atgriež ModelAndView objektu • ModelAndView satur View objektu vai skata loģisko vārdu, un modeli

  15. ModelAndView • Objekts ModelAndViewsatur: • Skata objektu(View) vai skata loģisko nosaukumu • piemērs - JSP lapa • Modeli – atribūtu kopa (key-value) public class ModelAndView { /** View instance or view name String */ private Object view; /** Model Map */ private ModelMap model; . . .

  16. Pieprasījuma dzīves cikls (5) • Ja ModelAndView objekts satur skata loģisko vārdu, tad DispatcherServlet izmanto ViewResolverkomponenti • ViewResolver atrod atbilstošu View objektu, lai attēlot atbildi klientam public boolean isReference() { return (this.view instanceof String); }

  17. Pieprasījuma dzīves cikls (6) • Visbeidzot, DispatcherServlet atdod pieprasījumu tam Viewobjektam, uz kuru norāda ModelAndView • Viewobjekts ir atbildīgs par pieprasījuma atbildes atgriešanu un parādīšanu klientam (rendering)

  18. Spring MVC sequence diagram

  19. Processing workflow http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html

  20. Vienkāršas Web lapas izveidošana Nepieciešamie soļi, lai izveidot Web lapu ar Spring MVC: • Uzrakstīt kontroliera klasi, kas izpildīs biznesa loģiku • Piereģistrēt DispatcherServlet un kontrolieri • Iekonfigurēt view resolver, kurš piesaistīs kontrolieri pie JSP • Uzrakstīt JSP, kas attēlos lapu klientam

  21. Spring MVC bibliotēkas Maven konfigurācija (Spring 3.2.5): • <dependency> • <groupId>org.springframework</groupId> • <artifactId>spring-webmvc</artifactId> • <version>3.2.5.RELEASE</version> • </dependency> • <dependency> • <groupId>org.springframework</groupId> • <artifactId>spring-context</artifactId> • <version>3.2.5.RELEASE</version> • </dependency> • <dependency> • <groupId>javax.servlet</groupId> • <artifactId>javax.servlet-api</artifactId> • <version>3.1.0</version> • </dependency> Neaizmirst pievienot arī Spring Context и Servlet API!

  22. Kontroliera izveidošana @Controller public class HomeController { private String greeting = “Hello!”; @RequestMapping("/home") public ModelAndView helloWorld() { ModelAndView mav = new ModelAndView(); mav.setViewName("home"); mav.addObject("message", greeting); return mav; } public void setGreeting(String greeting) { this.greeting = greeting; } }

  23. DispatcherServletreģistrācija Divi veidi: • web.xml (klasika) • Java konfigurācija (Spring annotation based) <web-app> <servlet> <servlet-name>spring</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

  24. DispatcherServletcontext • WebApplicationContext tiek ielādēts no faila: • spring-servlet.xml • kurš atrodas direktorijā WEB-INF (blakus web.xml failam) • Ir iespējams arī sadalīt • konfigurāciju starp • vairākiem failiem un/vai • izmantot Java konfigurāciju

  25. Context hierarchy in Spring Web MVC http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html

  26. Konfigurācijas faila piemērs \spring-app\src\main\webapp\WEB-INF\spring-servlet.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- the application context definition for the DispatcherServlet --> <context:component-scan base-package="ru.kfu.itis.web.controller"/> </beans> Fails satur bean definitions, kurus izmanto DispatcherServlet

  27. View Resolver deklarēšana • View resolver darbs ir pēc skata loģiska vārda (kurš ir atgriezts ModelAndView objektā) atrast pašu skatu • Viens no variantiem ir izmantot skata vārdu kā JSP faila vārdu (jāieraksta spring-servlet.xml): <bean id="viewResolver" class="org.springframework.web. servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> “home”  /WEB-INF/jsp/home.jsp

  28. JSP izveidošana • Pēdējais solis – izveidot JSP lapu <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head><title>Spring Training, Inc.</title></head> <body> <h2><c:out value="${message}"/></h2> </body> </html> • Failu ir jānosauc “home.jsp” un ir jāieliek /WEB-INF/jsp/direktorijā

  29. JSTL konfigurēšana Lai izmantot JSTL (c:out tagu) JSP lapā, ir nepieciešamas papildus bibliotēkas <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.1.2</version> </dependency>

  30. Rezultāts • Palika izveidot tīmekļa lietojumprogrammas WAR failu (mvn package) un palaist to uz servera • Pārlūkprogrammā uzrakstīt URL: http://localhost:8080/spring-app/home

  31. Kopsavilkums

  32. Resursi • SpringFramework http://projects.spring.io/spring-framework/ • Web MVC Framework documentation http://docs.spring.io/spring/docs/3.2.4.RELEASE/spring-framework-reference/html/mvc.html • Tutorial "Designing and Implementing a Web Application with Spring" http://spring.io/guides/tutorials/web/ • Spring Pet Clinic demo project http://spring.io/blog/2013/03/21/spring-petclinic-is-on-github

More Related