1介紹
mvc框架是什么
mvc全名是model view controller,是模型(model)-視圖(view)-控制器(controller)的縮寫(xiě),一種軟件設(shè)計(jì)典范,用一種業(yè)務(wù)邏輯、數(shù)據(jù)、界面顯示分離的方法組織代碼,將業(yè)務(wù)邏輯聚集到一個(gè)部件里面,在改進(jìn)和個(gè)性化定制界面及用戶交互的同時(shí),不需要重新編寫(xiě)業(yè)務(wù)邏輯。mvc被獨(dú)特的發(fā)展起來(lái)用于映射傳統(tǒng)的輸入、處理和輸出功能在一個(gè)邏輯的圖形化用戶界面的結(jié)構(gòu)中。
模型-視圖-控制器(mvc)是一個(gè)眾所周知的以設(shè)計(jì)界面應(yīng)用程序?yàn)榛A(chǔ)的設(shè)計(jì)模式。它主要通過(guò)分離模型、視圖及控制器在應(yīng)用程序中的角色將業(yè)務(wù)邏輯從界面中解耦。通常,模型負(fù)責(zé)封裝應(yīng)用程序數(shù)據(jù)在視圖層展示。視圖僅僅只是展示這些數(shù)據(jù),不包含任何業(yè)務(wù)邏輯。控制器負(fù)責(zé)接收來(lái)自用戶的請(qǐng)求,并調(diào)用后臺(tái)服務(wù)(manager或者dao)來(lái)處理業(yè)務(wù)邏輯。處理后,后臺(tái)業(yè)務(wù)層可能會(huì)返回了一些數(shù)據(jù)在視圖層展示。控制器收集這些數(shù)據(jù)及準(zhǔn)備模型在視圖層展示。mvc模式的核心思想是將業(yè)務(wù)邏輯從界面中分離出來(lái),允許它們單獨(dú)改變而不會(huì)相互影響。
在springmvc應(yīng)用程序中,模型通常由pojo對(duì)象組成,它在業(yè)務(wù)層中被處理,在持久層中被持久化。視圖通常是用jsp標(biāo)準(zhǔn)標(biāo)簽庫(kù)(jstl)編寫(xiě)的jsp模板。控制器部分是由dispatcherservlet負(fù)責(zé),在本教程中我們將會(huì)了解更多它的相關(guān)細(xì)節(jié)。
一些開(kāi)發(fā)人員認(rèn)為業(yè)務(wù)層和dao層類是mvc模型組件的一部分。我對(duì)此持有不同的意見(jiàn)。我不認(rèn)為業(yè)務(wù)層及dao層類為mvc框架的一部分。通常一個(gè)web應(yīng)用是3層架構(gòu),即數(shù)據(jù)-業(yè)務(wù)-表示。mvc實(shí)際上是表示層的一部分。
dispatcher servlet(spring控制器)
在最簡(jiǎn)單的spring mvc應(yīng)用程序中,控制器是唯一的你需要在java web部署描述文件(即web.xml文件)中配置的servlet。spring mvc控制器 ——通常稱作dispatcher servlet,實(shí)現(xiàn)了前端控制器設(shè)計(jì)模式。并且每個(gè)web請(qǐng)求必須通過(guò)它以便它能夠管理整個(gè)請(qǐng)求的生命周期。
當(dāng)一個(gè)web請(qǐng)求發(fā)送到spring mvc應(yīng)用程序,dispatcher servlet首先接收請(qǐng)求。然后它組織那些在spring web應(yīng)用程序上下文配置的(例如實(shí)際請(qǐng)求處理控制器和視圖解析器)或者使用注解配置的組件,所有的這些都需要處理該請(qǐng)求。
在spring3.0中定義一個(gè)控制器類,這個(gè)類必須標(biāo)有@controller注解。當(dāng)有@controller注解的控制器收到一個(gè)請(qǐng)求時(shí),它會(huì)尋找一個(gè)合適的handler方法去處理這個(gè)請(qǐng)求。這就需要控制器通過(guò)一個(gè)或多個(gè)handler映射去把每個(gè)請(qǐng)求映射到handler方法。為了這樣做,一個(gè)控制器類的方法需要被@requestmapping注解裝飾,使它們成為handler方法。
handler方法處理完請(qǐng)求后,它把控制權(quán)委托給視圖名與handler方法返回值相同的視圖。為了提供一個(gè)靈活的方法,一個(gè)handler方法的返回值并不代表一個(gè)視圖的實(shí)現(xiàn)而是一個(gè)邏輯視圖,即沒(méi)有任何文件擴(kuò)展名。你可以將這些邏輯視圖映射到正確的實(shí)現(xiàn),并將這些實(shí)現(xiàn)寫(xiě)入到上下文文件,這樣你就可以輕松的更改視圖層代碼甚至不用修改請(qǐng)求handler類的代碼。
為一個(gè)邏輯名稱匹配正確的文件是視圖解析器的責(zé)任。一旦控制器類已將一個(gè)視圖名稱解析到一個(gè)視圖實(shí)現(xiàn)。它會(huì)根據(jù)視圖實(shí)現(xiàn)的設(shè)計(jì)來(lái)渲染對(duì)應(yīng)對(duì)象。
2導(dǎo)入jar包
至少應(yīng)該有這些.
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在初始化后會(huì)直接在/web-inf/下找springmvc-servlet.xml文件, servlet-name標(biāo)簽的參數(shù)定義要和xml文件對(duì)應(yīng) --> <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
這個(gè)文件的名字是由web.xml里面配置的dispatcherservlet的<servlet-name></servlet-name>決定的,路徑在上下文/web-inf/里面,主要是配置控制器返回的邏輯視圖名和物理視圖的對(duì)應(yīng)關(guān)系
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"> <!-- 自動(dòng)掃描的包 --> <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 返回的一個(gè)邏輯視圖名經(jīng)過(guò)前后綴的處理返回物理視圖 --> <property name= "prefix" value= "/web-inf/jsp/" /> <property name= "suffix" value= ".jsp" /> </bean> </beans> |
4 編寫(xiě)一個(gè)domain類
用來(lái)封裝一些提交數(shù)據(jù)
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 編寫(xiě)controller
這個(gè)是mvc中的控制器,和struts2不一樣的是他是方法級(jí)的攔截,struts2是類級(jí)的攔截.
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相當(dāng)于struts2里的一個(gè)action //返回一個(gè)字符串給視圖 @requestmapping ( "/hello" ) public modelandview sayhello() { //modelandview的構(gòu)造方法的第一個(gè)參數(shù)相當(dāng)于struts2里的一個(gè)result的name modelandview modelandview = new modelandview( "helloworld" , "msg" , "helloworld!!!" ); return modelandview; } //返回一個(gè)對(duì)象給視圖 //@modelattribute("obj")的作用相當(dāng)于struts2的action類里面的一個(gè)field, //用于表單提交的數(shù)據(jù)放進(jìn)一個(gè)對(duì)象里面 //這里和struts2的區(qū)別: //struts2處理表單提交的方式是:<input name="obj.data"/> 提交的數(shù)據(jù)封裝在obj對(duì)象的data里面 //springmvc的方式是:<input name="data"/> 提交的數(shù)據(jù)封裝在obj對(duì)象的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" 會(huì)自動(dòng)對(duì)應(yīng)上對(duì)象的filed --> <input type= "text" name= "data" size= "30" /><br/> <input type= "submit" value= "submit" /> </form> <hr/> ${obj.data} </body> </html> |
7 目錄結(jié)構(gòu)
總結(jié)
以上就是本文關(guān)于springmvc入門(mén)實(shí)例的全部?jī)?nèi)容,希望對(duì)大家有所幫助。如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持。
原文鏈接:http://www.open-open.com/code/view/1454080281355