初步认识spring mvc
spring mvc 01
Spring Web MVC是一種基于Java的實(shí)現(xiàn)了Web MVC設(shè)計(jì)模式的請(qǐng)求驅(qū)動(dòng)類型的輕量級(jí)Web框架,即使用了MVC架構(gòu)模式的思想,將web層進(jìn)行職責(zé)解耦,基于請(qǐng)求驅(qū)動(dòng)指的就是使用請(qǐng)求-響應(yīng)模型,框架的目的就是幫助我們簡化開發(fā),Spring Web MVC也是要簡化我們?nèi)粘eb開發(fā)的。
另外還有一種基于組件的、事件驅(qū)動(dòng)的Web框架在此就不介紹了,如Tapestry、JSF等。
Spring Web MVC也是服務(wù)到工作者模式的實(shí)現(xiàn),但進(jìn)行可優(yōu)化。前端控制器是DispatcherServlet;應(yīng)用控制器其實(shí)拆為處理器映射器(Handler Mapping)進(jìn)行處理器管理和視圖解析器(View Resolver)進(jìn)行視圖管理;頁面控制器/動(dòng)作/處理器為Controller接口(僅包含ModelAndView handleRequest(request, response) 方法)的實(shí)現(xiàn)(也可以是任何的POJO類);支持本地化(Locale)解析、主題(Theme)解析及文件上傳等;提供了非常靈活的數(shù)據(jù)驗(yàn)證、格式化和數(shù)據(jù)綁定機(jī)制;提供了強(qiáng)大的約定大于配置(慣例優(yōu)先原則)的契約式編程支持。
2.2、Spring Web MVC能幫我們做什么
√讓我們能非常簡單的設(shè)計(jì)出干凈的Web層和薄薄的Web層;
√進(jìn)行更簡潔的Web層的開發(fā);
√天生與Spring框架集成(如IoC容器、AOP等);
√提供強(qiáng)大的約定大于配置的契約式編程支持;
√能簡單的進(jìn)行Web層的單元測(cè)試;
√支持靈活的URL到頁面控制器的映射;
√非常容易與其他視圖技術(shù)集成,如Velocity、FreeMarker等等,因?yàn)槟P蛿?shù)據(jù)不放在特定的API里,而是放在一個(gè)Model里(Map數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn),因此很容易被其他框架使用);
√非常靈活的數(shù)據(jù)驗(yàn)證、格式化和數(shù)據(jù)綁定機(jī)制,能使用任何對(duì)象進(jìn)行數(shù)據(jù)綁定,不必實(shí)現(xiàn)特定框架的API;
√提供一套強(qiáng)大的JSP標(biāo)簽庫,簡化JSP開發(fā);
√支持靈活的本地化、主題等解析;
√更加簡單的異常處理;
√對(duì)靜態(tài)資源的支持;
√支持Restful風(fēng)格。
2.3、Spring Web MVC架構(gòu)
Spring Web MVC框架也是一個(gè)基于請(qǐng)求驅(qū)動(dòng)的Web框架,并且也使用了前端控制器模式來進(jìn)行設(shè)計(jì),再根據(jù)請(qǐng)求映射規(guī)則分發(fā)給相應(yīng)的頁面控制器(動(dòng)作/處理器)進(jìn)行處理。首先讓我們整體看一下Spring Web MVC處理請(qǐng)求的流程:
見圖表
1、 首先用戶發(fā)送請(qǐng)求————>前端控制器,前端控制器根據(jù)請(qǐng)求信息(如URL)來決定選擇哪一個(gè)頁面控制器進(jìn)行處理并把請(qǐng)求委托給它,即以前的控制器的控制邏輯部分;圖2-1中的1、2步驟;
2、 頁面控制器接收到請(qǐng)求后,進(jìn)行功能處理,首先需要收集和綁定請(qǐng)求參數(shù)到一個(gè)對(duì)象,這個(gè)對(duì)象在Spring Web MVC中叫命令對(duì)象,并進(jìn)行驗(yàn)證,然后將命令對(duì)象委托給業(yè)務(wù)對(duì)象進(jìn)行處理;處理完畢后返回一個(gè)ModelAndView(模型數(shù)據(jù)和邏輯視圖名);圖2-1中的3、4、5步驟;
3、 前端控制器收回控制權(quán),然后根據(jù)返回的邏輯視圖名,選擇相應(yīng)的視圖進(jìn)行渲染,并把模型數(shù)據(jù)傳入以便視圖渲染;圖2-1中的步驟6、7;
4、 前端控制器再次收回控制權(quán),將響應(yīng)返回給用戶,圖2-1中的步驟8;至此整個(gè)結(jié)束。
Spring 框架提供了構(gòu)建 Web 應(yīng)用程序的全功能 MVC 模塊。使用 Spring 可插入的 MVC 架構(gòu),可以選擇是使用內(nèi)置的 Spring Web 框架還是 Struts 這樣的 Web 框架。通過策略接口,Spring 框架是高度可配置的,而且包含多種視圖技術(shù),例如 JavaServer Pages(JSP)技術(shù)、Velocity、Tiles、iText 和 POI。Spring MVC 框架并不知道使用的視圖,所以不會(huì)強(qiáng)迫您只使用 JSP 技術(shù)。Spring MVC 分離了控制器、模型對(duì)象、分派器以及處理程序?qū)ο蟮慕巧?#xff0c;這種分離讓它們更容易進(jìn)行定制
常用開發(fā)性能對(duì)比
Jsp+servlet>struts1>spring mvc>struts2+freemarker>>struts2,ognl,值棧。
案例01:
我們首先導(dǎo)入springmvc所需要的jar包
這里我們導(dǎo)入spring最基礎(chǔ)的jar包以后的新功能我們?cè)谔砑?br />所需jar包在案例01lib下
配置web.xml文件
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/springmvc-servlet.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.spring</url-pattern>
</servlet-mapping>
它是基于基礎(chǔ)的servlet的。
注意這里的spring配置文件默認(rèn)引入的是/WEB-INF/[servlet-name]-servlet.xml
我們可以通過上邊的方式修改成我們需要的
在xml文件中配置并且完成我們的第一個(gè)案例
注意這一段配置文件
<bean id="login" class="com.wode.controller.LoginController"></bean>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="login.spring">login</prop>
</props>
</property>
</bean>
HandleMapping用于根據(jù)請(qǐng)求匹配應(yīng)用控制器---這里我們就把login.spring匹配到了我們注冊(cè)的login這個(gè)bean中去了。而loginbena繼承了我們spring mvc
?
?
<?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.2.xsd"><bean id="login" class="com.lovo.controller.LoginController"></bean><bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"><property name="mappings"><props><prop key="login.spring">login</prop></props></property></bean><bean name="/login.spring" class="com.lovo.controller.LoginController"></bean><bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/><bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"><property name="viewClass"value="org.springframework.web.servlet.view.JstlView"></property><property name="prefix" value="/"></property><property name="suffix" value=".jsp"></property></bean></beans>?
spring mvc方式2:對(duì)應(yīng)文件springmvc-servlet02.xml
<bean
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
</bean>
這種方式作用和之前的方式一樣的效果不同點(diǎn)是
它利用url名字直接尋找相應(yīng)類名的控制器 如:login.spring會(huì)找到LoginController
?
<?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.2.xsd"><bean id="login" class="com.lovo.controller.LoginController"></bean><beanclass="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean><bean name="/login.spring" class="com.lovo.controller.LoginController"></bean><bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/><bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"><property name="viewClass"value="org.springframework.web.servlet.view.JstlView"></property><property name="prefix" value="/"></property><property name="suffix" value=".jsp"></property></bean></beans>?
?
方式3: 對(duì)應(yīng)文件對(duì)應(yīng)文件springmvc-servlet03.xml
<bean name="/login.spring" class="com.wode.controller.LoginController"></bean>
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
這種方式利用url作為控制器在spring容器中的name屬性進(jìn)行匹配 如loing.spring會(huì)找name="/login.spring"的容器bean。springMVC默認(rèn)使用這種HandlerMapping
?
<?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.2.xsd"><bean id="login" class="com.lovo.controller.LoginController"></bean><bean name="/login.spring" class="com.lovo.controller.LoginController"></bean><bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/><bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"><property name="viewClass"value="org.springframework.web.servlet.view.JstlView"></property><property name="prefix" value="/"></property><property name="suffix" value=".jsp"></property></bean></beans>?
方式4:自己寫我們的控制器在哪里注意和方式3的區(qū)別
<bean name="/login.spring" class="com.wode.controller.LoginController"></bean>
?
<?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.2.xsd"><bean id="login" class="com.lovo.controller.LoginController"></bean><bean name="/login.spring" class="com.lovo.controller.LoginController"></bean><bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"><property name="viewClass"value="org.springframework.web.servlet.view.JstlView"></property><property name="prefix" value="/"></property><property name="suffix" value=".jsp"></property></bean></beans>?
方式5:適用于多個(gè)表單各自提交,但處理同一業(yè)務(wù)對(duì)象的不同業(yè)務(wù)行為
相當(dāng)于在一個(gè)controller中處理多個(gè)請(qǐng)求
這里需要注意的是他需要extends MultiActionController Multi---這個(gè)單詞什么意思?
具體案例看文件
?
<?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.2.xsd"><bean id="login" class="com.lovo.controller.LoginController"></bean><bean id="myRegist" class="com.lovo.controller.TestController"><!-- 注入ParameterMethodNameResolver 實(shí)現(xiàn)根據(jù)特定表單提交項(xiàng)的值作為業(yè)務(wù)方法名,進(jìn)行調(diào)用 --><property name="methodNameResolver"><beanclass="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"><property name="paramName" value="methodName"></property></bean></property></bean><bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"><property name="mappings"><props><prop key="login.spring">login</prop><prop key="regist.spring">myRegist</prop></props></property></bean><bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"><property name="viewClass"value="org.springframework.web.servlet.view.JstlView"></property><property name="prefix" value="/"></property><property name="suffix" value=".jsp"></property></bean></beans>?
方式6:如果我們一個(gè)表單中涉及到多處提交,例如注冊(cè)中需要上傳頭像,這其實(shí)也是一次提交表單,那么怎么辦呢?和前邊的案例差不多
案例05---
?
轉(zhuǎn)載于:https://www.cnblogs.com/loongss/p/5816117.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的初步认识spring mvc的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mac与Phy组成原理的简单分析
- 下一篇: ▲数据结构 笛卡尔树【2011】五2 C