SprigMVC—理解MVC及快速入門
傳統MVC——>JSPModel2——>Front Controller + Application Controller + Page Controller + Context 三種模式是JavaWeb設計模式的逐漸適配和演進。本文分別講述了這三種模式,作爲了解即可。SpringMVC正是採用了第三種設計模式。
傳統MVC:
MVC是Xerox PARC在20世紀80年代爲編程語言SmallTalk發明的一種軟件設計模式。
MVC是一種設計模式,它強制性地把應用程序的數據展示、數據處理和流程控制分開。
MVC將應用程序分成3個核心模塊:模型、視圖、控制器,他們相互聯結又分別擔當不同的任務,如圖示所示。
JSPModel2:
在早期的Web應用中,JSP負責處理業務邏輯、控制網頁流程並創建HTML頁面。基本上JSP包攬的所有的模塊,這無疑造成了不少的困擾: 比如:
☐可維護性差 ☐調試困難 ☐ HTML與Java程序代碼強耦合在一起 ☐ 內嵌的流程控制邏輯,要理解應用程序的邏輯必須瀏覽所有的JSP頁面。
爲了解決這些問題,SUN公司先後制定了兩種設計模式,分別爲Model1和Model2。雖然Model1在一定程序上實現了MVC中的視圖和模型,但是他的運用並不理想,知道Model2的出現才改變了這種局面。
Model2中用JSP技術實現了視圖的功能、用Servlet技術實現了控制器的功能、用JavaBean技術實現了模型的功能。
Front Controller + Application Controller + Page Controller + Context:
前端控制器+應用控制器+頁面控制器(也有稱其爲動作)+上下文,也是一種WebMVC模型,只是責任更加明確,SpringMVC正是這種模式。
職責 Front Controller:前端控制器,負責爲表現層提供統一訪問點,從而避免Model2中出現的重複的控制邏輯(由前端控制器統一回調相應的功能方法,如前邊的根據submitFlag=login轉調login方法);並且可以爲多個請求提供共用的邏輯(如準備上下文等等),將選擇具體視圖和具體的功能處理(如login裏邊封裝請求參數到模型,並調用業務邏輯對象)分離。 Application Controller:應用控制器,前端控制器分離選擇具體視圖和具體的功能處理之後,需要有人來管理,應用控制器就是用來選擇具體視圖技術(視圖的管理)和具體的功能處理(頁面控制器/命令對象/動作管理),一種策略設計模式的應用,可以很容易的切換視圖/頁面控制器,相互不產生影響。 Page Controller(Command):頁面控制器/動作/處理器:功能處理代碼,收集參數、封裝參數到模型,轉調業務對象處理模型,返回邏輯視圖名交給前端控制器(和具體的視圖技術解耦),由前端控制器委託給應用控制器選擇具體的視圖來展示,可以是命令設計模式的實現。頁面控制器也被稱爲處理器或動作。
Context:上下文,還記得Model2中爲視圖準備要展示的模型數據嗎,我們直接放在request中(Servlet API相關),有了上下文之後,我們就可以將相關數據放置在上下文,從而與協議無關(如Servlet API)的訪問/設置模型數據,一般通過ThreadLocal模式實現。
實例:
0.項目結構
1.加入Jar包(Maven)
<dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-webmvcartifactId> <version>4.3.14.RELEASEversion> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-webartifactId> <version>4.3.14.RELEASEversion> dependency>
2.配置Web.xml文件
xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>dispatcherServletservlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class> <init-param> <param-name>contextConfigLocationparam-name> <param-value>classpath:dispatcherServlet-servlet.xmlparam-value> init-param> <load-on-startup>1load-on-startup> servlet> <servlet-mapping> <servlet-name>dispatcherServletservlet-name> <url-pattern>/url-pattern> servlet-mapping>web-app>
請求處理流程:
3.配置dispatcherServlet-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" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:mac="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.ray"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> bean>beans>
4.創建請求處理器類(helloworld.class)
package com.ray; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * Created by Ray on 2018/4/17 0017. */ @Controller public class helloworld { /** * 1. 使用 @RequestMapping 註解來映射請求的URL(相當於web.xml中的servlet-mapping元素的url-pattern) * 2. 返回值會通過視圖解析器解析爲實際的物理視圖,具體看第三步中的InternalResourceViewResolver配置信息 */ @RequestMapping("/helloworld") public String hello(){ System.out.println("控制檯輸出:hello world"); return "success"; } }
說明
這裏我們使用了註解@Controller ,可以表明了該類作爲一個控制器,而不需要你擴展任何控制器基類或者引用ServletAPI。當然,如果需要還是可以引用特定Servlet功能。註解@Controller的基本目標是擔任所註解的類的原型的角色,指明它的職責。
所註解的控制器Bean可以被顯示定義。@Controller也允許自動偵測。要實現隊所註解的控制器的自動偵測,必須要像配置中加入組件掃描的部分的語句。
<context:component-scan base-package="com.ray"/>
5.編寫JSP頁面(success.jsp)
/WEB-INF/views/success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %><% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %><html><head> <base href="<%=basePath%>"> <title>SpringMVC:學習筆記(1)——理解MVC及快速入門title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page">head><body> Hello,worldbody>html>
6.簡單測試
http://localhost:8080/StringMVC/helloworld
控制器return返回了視圖的名稱,我們在SpringMVC 中設置瞭如何解析處理器返回值爲視圖。 所以當我們訪問/helloworld後我們會被處理器跳轉到success.jsp頁面。
出處:http://blog.csdn.net/q343509740/article/details/79977620
覺得有用就轉發分享一下吧