java開源框架,Spring Framework的一個獨立模塊。
MVC框架,在項目中開辟MVC層次架構
對控制器中的功能 包裝 簡化 擴展踐行工廠模式,功能架構在工廠之上
名稱 | 職責 |
---|---|
Model | 模型:即業務模型,負責完成業務中的數據通信處理,對應項目中的 service和dao |
View | 視圖:渲染數據,生成頁面。對應項目中的Jsp |
Controller | 控制器:直接對接請求,控制MVC流程,調度模型,選擇視圖。對應項目中的Servlet |
java web start。MVC是現下軟件開發中的最流行的代碼結構形態;
人們根據負責的不同邏輯,將項目中的代碼分成 M V C 3個層次;
層次內部職責單一,層次之間耦合度低;
符合低耦合 高內聚的設計理念。也實際有利于項目的長期維護。
<dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.1.6.RELEASE</version>
</dependency>
作為一個MVC框架,首先要解決的是:如何能夠收到請求!
所以MVC框架大都會設計一款前端控制器,選型在 Servlet 或 Filter兩者之一,在框架最前沿率先工作,接收所有請求。
springmvc框架、此控制器在接收到請求后,還會負責springMVC的核心的調度管理,所以既是前端又是核心。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><!-- SpringMVC前端(核心)控制器1. 前端,接收所有請求2. 啟動SpringMVC工廠 需要使用mvc.xml3. springMVC流程調度--><servlet><servlet-name>mvc_shine</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_shine</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
</web-app>
等價于之前定義的Servlet
package com.qf.web;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;@Controller // 聲明后端控制器
@RequestMapping("/hello") //訪問路徑
public class HelloController {@RequestMapping("/test1") //訪問路徑public String hello1(){// service doGet doPostSystem.out.println("hello1");return "hello"; // // 跳轉:/hello.jsp}@RequestMapping("/test2")public String hello2(){// service doGet doPostSystem.out.println("hello2");return "hello2";}
}
默認名稱:核心控制器名-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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"><!-- 注解掃描 --><!-- 告知springmvc 哪些包中 存在 被注解的類 --><context:component-scan base-package="com.qf.web"/><!-- 注解驅動 --><mvc:annotation-driven></mvc:annotation-driven><!-- 視圖解析器作用:1.捕獲后端控制器的返回值="hello"2.解析: 在返回值的前后 拼接 ==> "/hello.jsp"--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 前綴 --><property name="prefix" value="/"></property><!-- 后綴 --><property name="suffix" value=".jsp"></property></bean>
</beans>
結構如下:
Spring MVC,說明:當wen.xml的servlet去啟動工廠的時候,會讀取其中mvc.xml文件,會看到注解驅動和注解掃描的包位置。
會把后端控制器創建出來,進行請求的處理。
先添加tomcat
http://localhost:8080/hello/test1
http://localhost:8080/hello/test2
控制臺打印出輸出內容即訪問正常。
return后面內容需要靜態文件的路徑,現在加上jsp(在webapp文件下)
并且在mvc.xml中添加視圖解析器的配置。
<!-- 視圖解析器作用:1.捕獲后端控制器的返回值="hello"2.解析: 在返回值的前后 拼接 ==> "/hello.jsp"--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 前綴 --><property name="prefix" value="/"></property><!-- 后綴 --><property name="suffix" value=".jsp"></property></bean>
現在訪問正常
springmvc事務注解。結構:
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态