版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、<p> 此文檔是畢業(yè)設(shè)計(jì)外文翻譯成品( 含英文原文+中文翻譯),無需調(diào)整復(fù)雜的格式!下載之后直接可用,方便快捷!本文價(jià)格不貴,也就幾十塊錢!</p><p> 外文標(biāo)題:Integrating Spring MVC Framework</p><p> 外文作者:Aleksa Vukotic,James Goodwill</p><p> 文獻(xiàn)出
2、處:Apache Tomcat 7: 199-209</p><p> 英文3426單詞,18995字符,中文5689漢字。</p><p> Integrating Spring MVC Framework</p><p> Aleksa Vukotic,James Goodwill</p><p> Java Servlet AP
3、I provides a rich and customizable framework for the development of web applications. However, in the professional Java development world, that is often not enough. While Java Servlet API provides low-level interfaces a
4、nd classes for interaction with web requests, efficient programming often requires an abstraction layer on top of the core servlet components. We are going to take a look at one such framework in this chapter: Spring MVC
5、. In this chapter we are going to:</p><p> Describe the Model-View-Controller pattern, which is the core part of Spring MVC </p><p> Discuss the Front Controller pattern that Spring MVC also i
6、mplements</p><p> Introduce the Spring framework, of which Spring MVC is part</p><p> Implement and configure a sample Spring MVC application, by adding model, view, and controller components&
7、lt;/p><p> Introducing Spring MVC</p><p> Spring MVC is a Java framework for the development of Java web applications using servlets. It uses the MVC design pattern to organize the structure of w
8、eb applications. It also implements the Front Controller design pattern to create configurable and easy-to-implement web components. Spring MVC is a part of the Spring framework—a popular, multi-purpose, open source Java
9、 framework.</p><p> In this section, we will discuss each of the design patterns implemented by the Spring MVC framework, and introduce the core principles of Spring framework, to which Spring MVC belongs.&
10、lt;/p><p> Spring Framework Overview</p><p> Spring framework is a lightweight, open source Java application framework designed to enable application developers to concentrate on the functional p
11、roblems at hand by providing the infrastructure, or glue-code, for Java enterprise applications. Spring is the brainchild of Rod Johnson, who first introduced in it his book Expert One-on-One J2EE Design and Development
12、(Wrox, 2002) In the book, Johnson takes a critical look at what was, at the time, standard Java enterprise application design and </p><p> The core part of the Spring framework is the dependency injection c
13、ontainer, which is the implementation of the Inversion of Control design pattern. It is designed to help developers and architects compose the complex applications from its contributing parts, or components. Using Spring
14、, you can split your application into loosely coupled components and wire them together using Spring’s dependency-injection container, which manages all your components’ lifecycles. The application components in S</p&
15、gt;<p> In addition to this core functionality, Spring ships with many other components, built with the purpose of helping developers follow best-practices in architecture and implementation of Java applications;
16、 for example, database access and integration with popular Java data access frameworks, aspect-oriented programming support, Java Messaging Service (JMS) integration, the development and design of web applications using
17、servlets and portlets, scheduling, and the like.</p><p> Spring is non-invasive, so application components managed by Spring don’t have any dependency on the Spring framework itself. Non-invasiveness is an
18、important characteristic of the Spring framework; your Java classes do not depend on Spring-specific classes and interfaces at all. That is, if you don’t want them to—Spring has a lot of very useful and convenient compon
19、ents that you will actually want to use in your code, so the choice is all yours.</p><p> Spring MVC represents the part of the Spring framework that is designed to make the development of Java web applicat
20、ions easier. In the next section, we’re going to explore the Spring MVC framework for the development of a web application deployed on Tomcat.</p><p> MVC Pattern</p><p> At the beginning of u
21、ser-oriented architecture and development, the major challenge was how to model the user-interface part of the system architecturally. The requirements were to decouple the presentation part of the application’s user-int
22、erface and the data it displays to the user. For example, different user interface components sometimes display different views of the same data (for example, HTML web pages and PDF files). In addition, the user interfac
23、e is typically platform dependent, whet</p><p> MVC came to life in 1979 by Trygve Reenskaug, a computer scientist at the Oslo University in Norway. It defines three collaborating aspects of the application
24、 from the user interface perspective: model, view, and controller.</p><p> Model represents the data and the behavior of the application’s domain model. It contains the information presented to the user. It
25、 also serves as the container for data additions or updates performed via the user interface (for example, data submitted using HTML forms in web applications).</p><p> View component is responsible for vis
26、ual display of the data. It transforms the information contained in the model to the chosen visual representation, and usually does not contain any logic. Examples of view technologies in web application development are
27、HTML, CSS, and JavaScript. Java Server Pages can be considered view technology if they are used with MVC principles, so that they don’t have any business logic, and are used only for data display and retrieval.</p>
28、<p> Controller is the component responsible for responding to user actions and, based on the input, manipulates and queries the model, and renders the view with data from the model. In a Java web application sen
29、se, servlets can be considered as controller components: when a user clicks on a link or presses the submit button, the action invokes the servlet, which performs the operation on the model (loads or updates data in the
30、database, for example), and renders the HTML view in the user’s browser.</p><p> It is important to follow good practices when implementing the MVC pattern in your web applications. The business logic shoul
31、d be encapsulated within the controller (or, to be more precise, delegated to the transactional service layer from the controller), the model should only contain data, and the view should only have visual markup, without
32、 any logic. In Java web application development, this means that JSP pages should only be used for data rendering and collection, without any embedded Java</p><p> Figure 11-1. Model-View-Controller pattern
33、</p><p> There are two main benefits of using the MVC pattern in user-interface development:</p><p> With the view decoupled from the model, it becomes easy to maintain multiple views while ke
34、eping the model unchanged. In web application design, for example, using the MVC pattern, you are able to develop web applications that can be accessed on standard desktop browsers as well as multiple mobile devices.<
35、/p><p> Adapting to change in application design is easy with MVC. You can change the presentation of the data to the user without changing the way your application responds to user actions or the data itself
36、(for example, you can change the complete look and feel of the web page by changing HTML and CSS code, without making any changes to servlets or to the database schema). In addition, you can make the changes to the model
37、 and controller components without affecting the application visibility to the </p><p> In the Spring MVC framework, the controller component is represented with the Controller interface, the model is repre
38、sented as a Java Map containing the collection of key-value pairs, where values are objects that are stored in the model, which are then accessed in the view using keys. Finally,views can be configured to be any web tech
39、nology you like—HTML, JSP, Freemarker, and so on.In addition to the MVC pattern, Spring MVC employs another important architectural software pattern: the Front Co</p><p> Front Controller Pattern</p>
40、<p> The Front Controller pattern is a popular pattern in web applications architecture. In addition to Spring MVC, many other web frameworks implement this pattern, including Ruby on Rails and PHP frameworks such
41、 as Cake PHP and Drupal. Servlet filters are implementations of the Front Controller pattern as well.</p><p> The main principle of the Front Controller pattern is that all web requests to web applications
42、are handled by a single entry point. This central entry point is called the front controller, and its role is to route requests to the specific web application component that should handle it. After a specific handler ha
43、s completed processing the request, the control is returned to the front controller, which is responsible for sending the web response back to the calling client. In addition to routin</p><p> Figure 11-2 i
44、llustrates the Front Controller pattern architecture.</p><p> Figure 11-2. Architecture of the Front Controller pattern</p><p> In the case of Spring MVC framework, this central entry point is
45、 the Java servlet implemented in class org.springframework.web.servlet.DispatcherServlet. In the next section, we are going to use the Spring MVC framework to develop and configure a sample web application.</p>&l
46、t;p> Spring MVC Web Applications</p><p> Spring MVC framework uses proven MVC and Front Controller patterns to guide developers to use best practices in web application development. Spring MVC contains
47、many components that are configured to work together in order to create a working web applications. Figure 11-3 illustrates the inner workings of the Spring MVC application.</p><p> Figure 11-3. Spring MVC
48、application architecture</p><p> DispatcherServlet (the front controller in Spring MVC implementation) processes all incoming client requests. It then delegates each request for handling by the Spring’s Con
49、troller component (or handler, as it’s sometimes called), based on the handler mapping. During request processing, a controller can collaborate with any of the configured Spring beans. After finishing with request proces
50、sing, a controller returns an instance of ModelAndView to the DispatcherServlet. ModelAndView is the spring</p><p> Most of the components are readily available in Spring MVC, and all we need to do in order
51、 to use them is to add configuration to our web application. The key parts that need to be implemented by developers are views and controllers. Spring MVC supports almost all view technologies available in Java: JSP, Fr
52、eemarker, Velocity, and Flex. In this section, we are going to use JSPs as our view technology. The controllers in Spring MVC are implemented as Plain Old Java Objects (POJOs), with Spring-s</p><p> In the
53、following sections, we are going to guide you through configuration and basic implementation of a Spring MVC web application. Let’s start by configuring the DispatcherServlet.</p><p> Configuring Dispatcher
54、Servlet</p><p> The DispatcherServlet is the only servlet we are going to configure for the Spring MVC web application. It’s configured like any other servlet, in web.xml file. Listing 11-1 shows the exampl
55、e configuration of DispatcherServlet.</p><p> There is nothing new in this configuration: we configured the servlet with the name chapter11 using the <servlet> element, specifying the org.springframew
56、ork.web.servlet.DispatcherServlet as servlet class (#1). We then mapped this servlet to all URLs with .html extensions (#2).</p><p> Let’s now implement and configure all the components used in the MVC patt
57、ern: model, view, and controller. We’re going to start with the view and its model representation.</p><p> Adding Views</p><p> Let’s start by adding a view component to our Spring MVC example
58、. We will use a standard JSP file as a view in our application, which will display the message to the user. The message itself will be our model from the Model-View-Controller pattern. The message is the information that
59、 is displayed in the view— it’s not hard-coded in the view itself, but rather passed to the view from the controller component.</p><p> Listing 11-2 shows the JSP file implementation.</p><p>
60、We are displaying the message in a JSP file using JSP Expression Language (EL), which we introduced in Chapter 3. We used the key message to reference the message object from the model (#1); this object will need to be a
61、dded to the model by the controller.</p><p> We are going to save this JSP file as index.jsp in the /WEB-INF/views/ directory.</p><p> Now that we have the view to display the data from the mo
62、del, we need to implement the controller</p><p> component.</p><p> Implementing Controllers</p><p> The controller component from the MVC pattern is called just like that in the
63、 Spring MVC: controller. It’s a POJO implementation, which does not extend any other class or implement any interface from the framework. To mark the Java class as controller, all we have to do is annotate it with Spring
64、’s @Controller annotation. Let’s take a look at our HelloWorldController implementation in Listing 11-3.</p><p> Listing 11-3. Controller Implementation That Connects Model and View Components</p>&l
65、t;p> The class itself is annotated with @Controller annotation, which will tell the Spring MVC framework to treat it as a controller component (#1). The class is just a POJO, without any super class or implementing i
66、nterface (#2). This approach enables easy out-of-container testing of your controllers, as they don’t have any dependency on the Servlet API at all—for example, we don’t have to extend HttpServlet like we did in earlier
67、servlet examples in this book.</p><p> Each implemented method in the controller class acts as a separate web handler. We use @RequestMapping annotation on the method level to configure mapping for the part
68、icular handler: the URL pattern it will be mapped to and the HTTP method it accepts. In our example, we mapped our method hello() to /helloWorld.html URL, using GET HTTP method (#3).</p><p> The handler met
69、hod itself does not have any arguments, and has a return parameter of type ModelAndView (#4). ModelAndView class is the Spring’s abstraction of the view MVC component and the model that is used in the view. Each Spring M
70、VC handler returns the instance of ModelAndView to the front controller (DispatcherServlet), which in turn sends the response of the view with model to the</p><p> calling client (browser in our case).</
71、p><p> Model itself is nothing more than a Java Map instance—a collection of String keys with Object values (#5). In our sample controller, we are adding the message object to the model using key message (#6),
72、 the same key used in the JSP view to display the message in Listing 11-2.</p><p> Finally we return the constructed instance of ModelAndView (#7). We pass two arguments to the ModelAndView constructor: the
73、 name of the view ("index") and the instance of the model. Spring will translate the view name to the actual view (JSP file) using the view resolver, which we will cover in the next section.</p><p>
74、; So far, we have implemented the view that displays the model information, and the controller that populates the model and passes it to the view for rendering. Let’s now wire all these components together in the Spring
75、 application context.</p><p> Wiring Spring Application Context</p><p> Spring application context is configured using XML in the Spring configuration files. In Spring MVC, the DispatcherServl
76、et configured in the web.xml file will automatically load its Spring context from the /WEB-INF/{servlet-name}-servlet.xml file packaged in the web application, where the {servlet-name} is the configured <servlet-name&
77、gt; element used in the web.xml. Because we specified the servlet name chapter11 in the web.xml (Listing 11-1), the Spring configuration file will be /WEB-INF/chapter</p><p> Spring application context is c
78、onfigured in XML format, using XML schemas defined in the Spring framework. To configure Spring MVC application context, we are using two XML schemas: beans schema to configure Spring components (beans), and mvc schema f
79、or Spring MVC specific configuration (#1).</p><p> We used @Controller annotation to mark Java class used as controller component. Annotations in Java are only used for code markup; the application itself m
80、ust understand Java annotations at runtime. That’s why we have to configure Spring runtime to understand the @Controller annotation, and configure classes annotated with it as controller components in MVC architecture. S
81、pring comes with the convenient XML configuration element <mvc:annotation-driven/> that enables us to do just that (#2). In a</p><p> All spring-managed components must be defined as beans in the Spri
82、ng framework. So our next step is to configure HelloWorldController as a spring-managed bean (#3).</p><p> The final step is to configure Spring to recognize and use view names referenced in the controller,
83、 and map them to the JSP files we created. To do this we configured view resolver (#4). Spring comes with few view resolver implementations, and we are using InternalResourceViewResolver, which is convenient to use with
84、JSP files. We specify three properties for view resolver bean:</p><p> viewClass (#5) is set to org.springframework.web.servlet.view.JstlView, which is the Spring’s implementation of JSP view with support o
85、f JSP Standard Tag Library</p><p><b> (JSTL).</b></p><p> prefix (#6) is set to "/WEB-INF/views" and specifies the prefix path where all our JSP files are located.</p&
86、gt;<p> If you take a look at our controller implementation in Listing 11-3, you will remember that we referenced our view using its name only ("index"). Now we can understand how Spring uses this view
87、name to reference the actual view: the JSP file that we saved to /WEB-INF/views/index.jsp location.</p><p> Spring uses a configured view resolver bean to transform the view name to the fully qualified path
88、 of JSP view file using prefix and suffix. Starting from the configured prefix ("/WEB-INF/views/"), Spring appends view name reference in controller implementation ("index"), and finally appends the c
89、onfigured suffix (".jsp") to complete the full path tour JSP file, relative to the web application root - /WEB- INF/views/index.jsp.</p><p> Make sure you have the view resolver bean defined in yo
90、ur web application context, as it’s responsible for translating view names you use in your controller code to actual views (JSP files for example). If you see error messages in your browser stating that Tomcat cannot fin
91、d a JSP file, make sure your view resolver is configured correctly, as it’s a typical cause of such errors.</p><p> Figure 11-4. Model, view, and controller are combined to display the “Hello, Spring MVC Wo
92、rld” message in the browser.</p><p> And that’s it: we now have a Spring MVC web application deployed and running in Tomcat.</p><p> Spring framework’s vision is to enable developers to write
93、lightweight Java Enterprise applications that are capable of delivering enterprise functionality without the heavy infrastructure usually related to it. Being lightweight as it is, with rich application server functional
94、ity, Tomcat has been a good match for Spring application since the beginning of Spring. Using Tomcat and Spring enables organizations to develop and deploy high-level enterprise Java applications at a low cost (as both T
95、o</p><p><b> Summary</b></p><p> In this chapter we introduced the Spring MVC framework used for web application design and development. We started by introducing Spring framework
96、itself, following with the description of two core design patterns that Spring MVC implements: the Model-View-Controller pattern and the Front Controller pattern.</p><p> In the second part of the chapter w
97、e implemented a sample web application using Spring MVC. We discussed the architecture of Spring MVC framework. Next, we implemented model, view, and controller components, and using Spring MVC. Finally, we configured th
98、e web application so it can be deployed and run on Apache Tomcat server.</p><p><b> 譯文:</b></p><p> 集成Spring MVC框架</p><p> Aleksa Vukotic,James Goodwill</p>&l
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 眾賞文庫(kù)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 114計(jì)算機(jī)專業(yè)相關(guān)有關(guān)畢業(yè)設(shè)計(jì)外文文獻(xiàn)集成spring mvc框架
- 130計(jì)算機(jī)專業(yè)相關(guān)有關(guān)畢業(yè)設(shè)計(jì)外文文獻(xiàn)翻譯成品介紹java web開發(fā)
- 116計(jì)算機(jī)專業(yè)相關(guān)有關(guān)畢業(yè)設(shè)計(jì)外文文獻(xiàn)翻譯成品安裝和配置mysql (最新)
- 117計(jì)算機(jī)專業(yè)相關(guān)有關(guān)畢業(yè)設(shè)計(jì)外文文獻(xiàn)翻譯成品 對(duì)java及其歷史的介紹(最新)
- 126有關(guān)計(jì)算機(jī)專業(yè)相關(guān)畢業(yè)設(shè)計(jì)外文文獻(xiàn)翻譯成品對(duì)delphi的概述
- 04計(jì)算機(jī)專業(yè)相關(guān)有關(guān)外文文獻(xiàn)翻譯成品tmn網(wǎng)絡(luò)管理平臺(tái)的設(shè)計(jì)與實(shí)現(xiàn)
- 18計(jì)算機(jī)專業(yè)相關(guān)有關(guān)外文文獻(xiàn)翻譯成品高校大學(xué)宿舍管理系統(tǒng)研究
- 【精品文檔】114中英文雙語(yǔ)計(jì)算機(jī)專業(yè)畢業(yè)設(shè)計(jì)外文文獻(xiàn)翻譯:集成spring mvc框架 12
- 29計(jì)算機(jī)專業(yè)相關(guān)有關(guān)外文文獻(xiàn)翻譯成品c.c語(yǔ)言的自動(dòng)類型轉(zhuǎn)換
- 128計(jì)算機(jī)專業(yè)相關(guān)有關(guān)畢業(yè)設(shè)計(jì)外文文獻(xiàn)翻譯成品web網(wǎng)站開發(fā)方法 4萬(wàn)字符
- 02計(jì)算機(jī)專業(yè)相關(guān)有關(guān)外文文獻(xiàn)翻譯成品c編程語(yǔ)言在增強(qiáng)故障檢測(cè)方面的擴(kuò)展
- 06計(jì)算機(jī)專業(yè)相關(guān)有關(guān)外文文獻(xiàn)翻譯成品安卓andriod移動(dòng)設(shè)備上醫(yī)療信息服務(wù)的實(shí)現(xiàn)
- 【中英雙語(yǔ)】118有關(guān)計(jì)算機(jī)專業(yè)相關(guān)畢業(yè)設(shè)計(jì)外文文獻(xiàn)翻譯成品:對(duì) asp.net core的介紹(最新)
- 108計(jì)算機(jī)專業(yè)相關(guān)有關(guān)外文文獻(xiàn)翻譯成品從信息門戶到數(shù)字圖書館管理系統(tǒng)案例分析
- 94計(jì)算機(jī)專業(yè)相關(guān)有關(guān)外文文獻(xiàn)翻譯成品分析java ee應(yīng)用程序中的程序依賴性
- 26計(jì)算機(jī)專業(yè)相關(guān)有關(guān)外文文獻(xiàn)翻譯成品基于消費(fèi)者行為建模的網(wǎng)頁(yè)內(nèi)容推薦系統(tǒng)
- 116計(jì)算機(jī)專業(yè)相關(guān)有關(guān)畢業(yè)設(shè)計(jì)外文文獻(xiàn)翻譯安裝和配置mysql (最新)
- 130計(jì)算機(jī)專業(yè)相關(guān)有關(guān)畢業(yè)設(shè)計(jì)外文文獻(xiàn)翻譯介紹java web開發(fā)
- 【中英雙語(yǔ)】124中英文雙語(yǔ)計(jì)算機(jī)專業(yè)畢業(yè)設(shè)計(jì)外文文獻(xiàn)翻譯成品:django框架介紹(最新)
- 128計(jì)算機(jī)專業(yè)相關(guān)有關(guān)畢業(yè)設(shè)計(jì)外文文獻(xiàn)翻譯web網(wǎng)站開發(fā)方法 (英文)
評(píng)論
0/150
提交評(píng)論