1介紹
mvc框架是什么
mvc全名是model view controller,是模型(model)-視圖(view)-控制器(controller)的縮寫,一種軟件設計典范,用一種業務邏輯、數據、界面顯示分離的方法組織代碼,將業務邏輯聚集到一個部件里面,在改進和個性化定制界面及用戶交互的同時,不需要重新編寫業務邏輯。mvc被獨特的發展起來用于映射傳統的輸入、處理和輸出功能在一個邏輯的圖形化用戶界面的結構中。
模型-視圖-控制器(mvc)是一個眾所周知的以設計界面應用程序為基礎的設計模式。它主要通過分離模型、視圖及控制器在應用程序中的角色將業務邏輯從界面中解耦。通常,模型負責封裝應用程序數據在視圖層展示。視圖僅僅只是展示這些數據,不包含任何業務邏輯。控制器負責接收來自用戶的請求,并調用后臺服務(manager或者dao)來處理業務邏輯。處理后,后臺業務層可能會返回了一些數據在視圖層展示。控制器收集這些數據及準備模型在視圖層展示。mvc模式的核心思想是將業務邏輯從界面中分離出來,允許它們單獨改變而不會相互影響。
在springmvc應用程序中,模型通常由pojo對象組成,它在業務層中被處理,在持久層中被持久化。視圖通常是用jsp標準標簽庫(jstl)編寫的jsp模板。控制器部分是由dispatcherservlet負責,在本教程中我們將會了解更多它的相關細節。
一些開發人員認為業務層和dao層類是mvc模型組件的一部分。我對此持有不同的意見。我不認為業務層及dao層類為mvc框架的一部分。通常一個web應用是3層架構,即數據-業務-表示。mvc實際上是表示層的一部分。
dispatcher servlet(spring控制器)
在最簡單的spring mvc應用程序中,控制器是唯一的你需要在java web部署描述文件(即web.xml文件)中配置的servlet。spring mvc控制器 ——通常稱作dispatcher servlet,實現了前端控制器設計模式。并且每個web請求必須通過它以便它能夠管理整個請求的生命周期。
當一個web請求發送到spring mvc應用程序,dispatcher servlet首先接收請求。然后它組織那些在spring web應用程序上下文配置的(例如實際請求處理控制器和視圖解析器)或者使用注解配置的組件,所有的這些都需要處理該請求。
在spring3.0中定義一個控制器類,這個類必須標有@controller注解。當有@controller注解的控制器收到一個請求時,它會尋找一個合適的handler方法去處理這個請求。這就需要控制器通過一個或多個handler映射去把每個請求映射到handler方法。為了這樣做,一個控制器類的方法需要被@requestmapping注解裝飾,使它們成為handler方法。
handler方法處理完請求后,它把控制權委托給視圖名與handler方法返回值相同的視圖。為了提供一個靈活的方法,一個handler方法的返回值并不代表一個視圖的實現而是一個邏輯視圖,即沒有任何文件擴展名。你可以將這些邏輯視圖映射到正確的實現,并將這些實現寫入到上下文文件,這樣你就可以輕松的更改視圖層代碼甚至不用修改請求handler類的代碼。
為一個邏輯名稱匹配正確的文件是視圖解析器的責任。一旦控制器類已將一個視圖名稱解析到一個視圖實現。它會根據視圖實現的設計來渲染對應對象。
2導入jar包
至少應該有這些.
3 配置文件
3.1 web.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
<?xml version= "1.0" encoding= "utf-8" ?> <web-app xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xmlns= "http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id= "webapp_id" version= "3.0" > <display-name>springmvc_helloworld</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file> default .html</welcome-file> <welcome-file> default .htm</welcome-file> <welcome-file> default .jsp</welcome-file> </welcome-file-list> <!-- spring mvc 的servlet --> <!-- dispatcherservlet在初始化后會直接在/web-inf/下找springmvc-servlet.xml文件, servlet-name標簽的參數定義要和xml文件對應 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet- class > org.springframework.web.servlet.dispatcherservlet </servlet- class > <load-on-startup> 1 </load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <context-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:applicationcontext.xml</param-value> </context-param> <listener> <listener- class >org.springframework.web.context.contextloaderlistener</listener- class > </listener> </web-app> |
3.2 springmvc-servlet.xml
這個文件的名字是由web.xml里面配置的dispatcherservlet的<servlet-name></servlet-name>決定的,路徑在上下文/web-inf/里面,主要是配置控制器返回的邏輯視圖名和物理視圖的對應關系
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<?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:aop= "http://www.springframework.org/schema/aop" xmlns:tx= "http://www.springframework.org/schema/tx" xmlns:context= "http://www.springframework.org/schema/context" xsi:schemalocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans.xsd http: //www.springframework.org/schema/tx http: //www.springframework.org/schema/tx/spring-tx.xsd http: //www.springframework.org/schema/aop http: //www.springframework.org/schema/aop/spring-aop.xsd http: //www.springframework.org/schema/context http: //www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 自動掃描的包 --> <context:component-scan base- package = "com.lin.helloworld.controller" /> <bean id= "viewresolver" class = "org.springframework.web.servlet.view.urlbasedviewresolver" > <property name= "viewclass" value= "org.springframework.web.servlet.view.jstlview" /> <!-- controller 返回的一個邏輯視圖名經過前后綴的處理返回物理視圖 --> <property name= "prefix" value= "/web-inf/jsp/" /> <property name= "suffix" value= ".jsp" /> </bean> </beans> |
4 編寫一個domain類
用來封裝一些提交數據
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com.lin.helloworld.domain; public class helloworld { private string data; public helloworld() { super (); } public helloworld(string data) { super (); this .data = data; } public string getdata() { return data; } public void setdata(string data) { this .data = data; } @override public string tostring() { return "helloworld [data=" + data + "]" ; } } |
5 編寫controller
這個是mvc中的控制器,和struts2不一樣的是他是方法級的攔截,struts2是類級的攔截.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package com.lin.helloworld.controller; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.modelattribute; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.servlet.modelandview; import com.lin.helloworld.domain.helloworld; @controller public class helloworldcontroller { //這里的/hello相當于struts2里的一個action //返回一個字符串給視圖 @requestmapping ( "/hello" ) public modelandview sayhello() { //modelandview的構造方法的第一個參數相當于struts2里的一個result的name modelandview modelandview = new modelandview( "helloworld" , "msg" , "helloworld!!!" ); return modelandview; } //返回一個對象給視圖 //@modelattribute("obj")的作用相當于struts2的action類里面的一個field, //用于表單提交的數據放進一個對象里面 //這里和struts2的區別: //struts2處理表單提交的方式是:<input name="obj.data"/> 提交的數據封裝在obj對象的data里面 //springmvc的方式是:<input name="data"/> 提交的數據封裝在obj對象的data里面, //前提是要使用@modelattribute注解 @requestmapping ( "/helloobj" ) public modelandview sayhelloworld( @modelattribute ( "obj" ) helloworld obj) { system.out.println(obj.tostring()); modelandview modelandview = new modelandview( "helloworld" , "obj" , obj); return modelandview; } } |
6 視圖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
<%@ page language= "java" import = "java.util.*" pageencoding= "utf-8" %> <% string path = request.getcontextpath(); string basepath = request.getscheme()+ "://" +request.getservername()+ ":" +request.getserverport()+path+ "/" ; %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" > <html> <head> <base href= "<%=basepath%>" rel= "external nofollow" > <title>my jsp 'helloworld.jsp' starting page</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" > <!-- <link rel= "stylesheet" type= "text/css" href= "styles.css" rel= "external nofollow" > --> </head> <body> helloworld! this is a spring mvc framework example.<br> ${msg} <hr/> <form action= "helloobj" method= "post" > <!-- 這里的表單提交和struts2不同的是name= "data" 會自動對應上對象的filed --> <input type= "text" name= "data" size= "30" /><br/> <input type= "submit" value= "submit" /> </form> <hr/> ${obj.data} </body> </html> |
7 目錄結構
總結
以上就是本文關于springmvc入門實例的全部內容,希望對大家有所幫助。如有不足之處,歡迎留言指出。感謝朋友們對本站的支持。
原文鏈接:http://www.open-open.com/code/view/1454080281355