SpringMVC入門實(shí)例
1介紹
MVC框架是什么
MVC全名是Model View Controller,是模型(model)-視圖(view)-控制器(controller)的縮寫,一種軟件設(shè)計(jì)典范,用一種業(yè)務(wù)邏輯、數(shù)據(jù)、界面顯示分離的方法組織代碼,將業(yè)務(wù)邏輯聚集到一個(gè)部件里面,在改進(jìn)和個(gè)性化定制界面及用戶交互的同時(shí),不需要重新編寫業(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)編寫的JSP模板。控制器部分是由dispatcherservlet負(fù)責(zé),在本教程中我們將會(huì)了解更多它的相關(guān)細(xì)節(jié)。
一些開發(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)寫入到上下文文件,這樣你就可以輕松的更改視圖層代碼甚至不用修改請(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
<?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)系
<?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 編寫一個(gè)domain類
用來(lái)封裝一些提交數(shù)據(jù)
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
這個(gè)是MVC中的控制器,和struts2不一樣的是他是方法級(jí)的攔截,struts2是類級(jí)的攔截.
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 視圖
<%@ 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入門實(shí)例的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:
Java編程實(shí)現(xiàn)springMVC簡(jiǎn)單登錄實(shí)例
SpringMVC編程使用Controller接口實(shí)現(xiàn)控制器實(shí)例代碼
SpringMVC攔截器實(shí)現(xiàn)監(jiān)聽(tīng)session是否過(guò)期詳解
如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持。
相關(guān)文章
Mybatis-plus設(shè)置某個(gè)字段值為null的方法總結(jié)
mybatis-plus以下簡(jiǎn)稱mp,目前應(yīng)該也算是主流的一款數(shù)據(jù)訪問(wèn)層應(yīng)用框架,下面這篇文章主要給大家介紹了關(guān)于Mybatis-plus設(shè)置某個(gè)字段值為null的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07springcloud整合gateway實(shí)現(xiàn)網(wǎng)關(guān)的示例代碼
本文主要介紹了springcloud整合gateway實(shí)現(xiàn)網(wǎng)關(guān)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01詳解mybatis插入數(shù)據(jù)后返回自增主鍵ID的問(wèn)題
這篇文章主要介紹了mybatis插入數(shù)據(jù)后返回自增主鍵ID詳解,本文通過(guò)場(chǎng)景分析示例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-07-07詳解Java編程規(guī)約(命名風(fēng)格、常量定義、代碼格式)
這篇文章主要介紹了詳解Java編程規(guī)約(命名風(fēng)格、常量定義、代碼格式),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-10-10Java?MapStruct優(yōu)雅地實(shí)現(xiàn)對(duì)象轉(zhuǎn)換
MapSturct?是一個(gè)生成類型安全,高性能且無(wú)依賴的?JavaBean?映射代碼的注解處理器,用它可以輕松實(shí)現(xiàn)對(duì)象轉(zhuǎn)換,下面就來(lái)和大家聊聊具體操作吧2023-06-06Springboot創(chuàng)建子父工程過(guò)程圖解
這篇文章主要介紹了Springboot創(chuàng)建子父工程過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02vue3實(shí)現(xiàn)一個(gè)todo-list
這篇文章主要為大家詳細(xì)介紹了基于vuejs實(shí)現(xiàn)一個(gè)todolist項(xiàng)目,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能給你帶來(lái)幫助2021-08-08詳解Spring Boot最新版優(yōu)雅停機(jī)的方法
這篇文章主要介紹了Spring Boot最新版優(yōu)雅停機(jī)的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10