SpringMVC入門(mén)實(shí)例
1介紹
MVC框架是什么
MVC全名是Model View Controller,是模型(model)-視圖(view)-控制器(controller)的縮寫(xiě),一種軟件設(shè)計(jì)典范,用一種業(yè)務(wù)邏輯、數(shù)據(jù)、界面顯示分離的方法組織代碼,將業(yè)務(wù)邏輯聚集到一個(gè)部件里面,在改進(jìn)和個(gè)性化定制界面及用戶(hù)交互的同時(shí),不需要重新編寫(xiě)業(yè)務(wù)邏輯。MVC被獨(dú)特的發(fā)展起來(lái)用于映射傳統(tǒng)的輸入、處理和輸出功能在一個(gè)邏輯的圖形化用戶(hù)界面的結(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)自用戶(hù)的請(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模板??刂破鞑糠质怯蒬ispatcherservlet負(fù)責(zé),在本教程中我們將會(huì)了解更多它的相關(guān)細(xì)節(jié)。
一些開(kāi)發(fā)人員認(rèn)為業(yè)務(wù)層和DAO層類(lèi)是MVC模型組件的一部分。我對(duì)此持有不同的意見(jiàn)。我不認(rèn)為業(yè)務(wù)層及DAO層類(lèi)為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控制器 ——通常稱(chēng)作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è)控制器類(lèi),這個(gè)類(lèi)必須標(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è)控制器類(lèi)的方法需要被@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類(lèi)的代碼。
為一個(gè)邏輯名稱(chēng)匹配正確的文件是視圖解析器的責(zé)任。一旦控制器類(lèi)已將一個(gè)視圖名稱(chēng)解析到一個(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 編寫(xiě)一個(gè)domain類(lèi)
用來(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 編寫(xiě)controller
這個(gè)是MVC中的控制器,和struts2不一樣的是他是方法級(jí)的攔截,struts2是類(lèi)級(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類(lèi)里面的一個(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入門(mén)實(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基于MapperScan注解的動(dòng)態(tài)代理加載機(jī)制詳解
這篇文章主要介紹了Mybatis基于MapperScan注解的動(dòng)態(tài)代理加載機(jī)制,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-01-01
SpringBoot普通類(lèi)獲取spring容器中bean的操作
這篇文章主要介紹了SpringBoot普通類(lèi)獲取spring容器中bean的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
SpringBoot整合Ip2region獲取IP地址和定位的詳細(xì)過(guò)程
ip2region v2.0 - 是一個(gè)離線IP地址定位庫(kù)和IP定位數(shù)據(jù)管理框架,10微秒級(jí)別的查詢(xún)效率,提供了眾多主流編程語(yǔ)言的 xdb 數(shù)據(jù)生成和查詢(xún)客戶(hù)端實(shí)現(xiàn) ,這篇文章主要介紹了SpringBoot整合Ip2region獲取IP地址和定位,需要的朋友可以參考下2023-06-06
詳解SpringBoot如何刪除引用jar包中的無(wú)用bean
為了趕速度和直接將之前多模塊的maven項(xiàng)目中的部分模塊,直接以jar包的形式引入到新項(xiàng)目中了,雖然省去了不少開(kāi)發(fā)時(shí)間,導(dǎo)致項(xiàng)目臃腫,啟動(dòng)很慢。本文將用@ComponentScan注解去實(shí)現(xiàn)讓項(xiàng)目只加載自己需要的bean,需要的可以參考一下2022-06-06
SpringBoot3.x嵌入MongoDB進(jìn)行測(cè)試的步驟詳解
本文介紹了在?Spring?Boot?應(yīng)用中使用Flapdoodle?Embed?Mongo進(jìn)行?MongoDB?測(cè)試的方法,包括Embed?Mongo?的概念,添加依賴(lài)、配置、運(yùn)行測(cè)試的步驟,還列舉了其優(yōu)勢(shì)如快速啟動(dòng)關(guān)閉、環(huán)境一致、無(wú)需外部依賴(lài)等,以及注意事項(xiàng)和結(jié)論,需要的朋友可以參考下2024-12-12
關(guān)于HashMap 并發(fā)時(shí)會(huì)引起死循環(huán)的問(wèn)題解析
JDK1.8之前采用頭插,即在鏈表結(jié)構(gòu)上每次都把數(shù)據(jù)放在鏈表頭部。JDK1.8采用尾插方法,很多朋友在學(xué)習(xí)Java并發(fā)容器和框架時(shí),看到為什么要使用ConcurrentHashMap時(shí)不知道究其原因,今天小編通過(guò)本文給大家介紹下HashMap 并發(fā)死循環(huán)問(wèn)題,一起看看吧2021-05-05

