1. 程式人生 > 其它 >關於SpringMVC的配置簡介

關於SpringMVC的配置簡介

技術標籤:javaspringmvcjava

引言

java開源框架,Spring Framework的一個獨立模組。

MVC框架,在專案中開闢MVC層次架構

對控制器中的功能 包裝 簡化 擴充套件踐行工廠模式,功能架構在工廠之上

MVC架構
概念
名稱職責
Model模型:即業務模型,負責完成業務中的資料通訊處理,對應專案中的 service和dao
View檢視:渲染資料,生成頁面。對應專案中的Jsp
Controller控制器:直接對接請求,控制MVC流程,排程模型,選擇檢視。對應專案中的Servlet
好處
  • MVC是現下軟體開發中的最流行的程式碼結構形態;

  • 人們根據負責的不同邏輯,將專案中的程式碼分成 M V C 3個層次;

  • 層次內部職責單一,層次之間耦合度低;

  • 符合低耦合 高內聚的設計理念。也實際有利於專案的長期維護。

開發流程:

1,配置環境,在專案pom.xml檔案中匯入依賴-------spring-webmvc

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.1.6.RELEASE</version>
</dependency>

2,配置核心(前端)控制器

​ 作為一個MVC框架,首先要解決的問題當然是:如何能夠接收到請求!!!

所以MVC框架大都會設計一款前端控制器,選型在Servlet或Filter兩者之一,在框架最前沿率先工作,接

收所有請求。

​ 控制器在接收到請求之後,還會負責springMVC的核心的排程管理,所以既是前端又是核心。

在web.xml中配置前端控制器(尋找控制層對映路徑的servlet)

<servlet>
    <servlet-name>mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</
servlet-class
>
<!-- 區域性引數:宣告配置檔案位置 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:mvc.xml</param-value> </init-param> <!-- Servlet啟動時刻:可選 --> <load-on-startup>1</load-on-startup> </servlet> <!-- 確定對映路徑: / --> <servlet-mapping> <servlet-name>mvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>

3,控制層操作

等價於之前定義的servlet

@Controller //宣告這是一個控制器
@RequestMapping("/hello")   //指定當前類的對映路徑。訪問路徑  等價於url-pattern
public class HelloController {
	@RequestMapping("/test1")  //指定方法的對映路徑
	public String hello1(){
		System.out.println("----------hello world----------");
		//返回字串,但通過檢視解析後可以返回出指定名叫ok檢視
        return "index"; //跳轉:/index.jsp 
	}
	@RequestMapping("/test2") //訪問路徑
	public String hello2(){
		System.out.println("**********hello world**********");
		return "views/users";//  跳轉:views檔案下的user.jsp頁面
	}
}

4,mvc的配置檔案設定

  • 預設名稱:核心控制器名-servet.xml 預設位置:WEB-INF

  • 隨意名稱:mvc.xml 隨意位置:resources 但需要配置在核心控制器中

​ 指定掃描註解包

​ 開啟註解包

​ 配置檢視解析器—配置返回的資訊是否加字首和後置

<beans 	xmlns="http://www.springframework.org/schema/beans"
		xmlns:context="http://www.springframework.org/schema/context"
		xmlns:mvc="http://www.springframework.org/schema/mvc" 
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
		xsi:schemaLocation="http://www.springframework.org/schema/beans
							http://www.springframework.org/schema/beans/spring-beans.xsd
							http://www.springframework.org/schema/context
							http://www.springframework.org/schema/context/spring-context.xsd
							http://www.springframework.org/schema/mvc
							http://www.springframework.org/schema/mvc/spring-mvc.xsd">

	<!-- 告知springmvc  哪些包中 存在 被註解的類 -->
	<context:component-scan base-package="com.qf.controller"></context:component-scan>
	<!-- 註冊註解開發驅動 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	<!-- 檢視解析器
	     作用:1.捕獲後端控制器的返回值="index"
	          2.解析: 在返回值的前後 拼接 ==> "/index.jsp"
	 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 字首 -->
		<property name="prefix" value="/"></property>
		<!-- 字尾 -->
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>

5,最後配置一個tomcat再發布專案就完事了

	瀏覽器訪問路徑:	
			http://localhost:8989/hello/test1
			http://localhost:8989/hello/test2

在這裡插入圖片描述