網(wǎng)站建設(shè)規(guī)劃北京今日重大新聞
Spring與Web環(huán)境的集成
1.ApplicationContext應(yīng)用上下文的獲取方式
分析
之前獲取應(yīng)用上下文對象每次都是從容器中獲取,編寫時(shí)都需要new ClasspathXmlApplicationContext(Spring配置文件),這樣的弊端就是配置加載多次應(yīng)用上下文就創(chuàng)建多次。
2.Spring提供的獲取應(yīng)用上下文的工具
實(shí)現(xiàn)
1.在web.xml中導(dǎo)入ContextLoaderListener監(jiān)聽器(導(dǎo)入Spring-web坐標(biāo))
2.使用WebApplicationContextUtils獲得應(yīng)用上下文對象ApplicationContext
? ? ? ? <1>導(dǎo)入Spring集成web的坐標(biāo)
<dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>5.0.5.RELEASE</version></dependency>
? ? ? ? <2>配置ContextLoaderListener監(jiān)聽器
<!--全局參數(shù) 全局參數(shù)主要是為了解耦。-->
<context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--Spring的監(jiān)聽器-->
<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
? ? ? ? <3> 通過工具獲取應(yīng)用上下文對象
? ? ? ? 將app存到servletConxt域中
? ? ? ? ?優(yōu)化的目的為了解耦,減小代碼之間的耦合。
public void contextInitialized(ServletContextEvent servletContextEvent) {/* ServletContext servletContext = servletContextEvent.getServletContext();ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");servletContext.setAttribute("app",app);System.out.println("spring容器創(chuàng)建完畢");*///優(yōu)化后/* ServletContext servletContext = servletContextEvent.getServletContext();//讀取web.xml中全局參數(shù)//getInitParameter返回包含指定上下文范圍初始化參數(shù)值的 String,如果參數(shù)不存在,則返回 nullString contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");ApplicationContext app = new ClassPathXmlApplicationContext(contextConfigLocation);//將Spring的應(yīng)用上下文對象存儲到ServletContext域中servletContext.setAttribute("app",app);System.out.println("spring 容器創(chuàng)建完畢");*/ServletContext servletContext = servletContextEvent.getServletContext();String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");ApplicationContext app = new ClassPathXmlApplicationContext(contextConfigLocation);servletContext.setAttribute("app",app);}
? ? ? ? 然后取出使用
@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {ServletContext servletContext = req.getServletContext();/*ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");*//*ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);*/ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);UserService userService = (UserService) app.getBean("userService");userService.save();}
SpringMvc簡介
MVC是一種軟件架構(gòu)的思想,將軟件按照模型、視圖、控制器來劃分
M:Model,模型層,指工程中的JavaBean,作用是處理數(shù)據(jù)
JavaBean分為兩類:
-
一類稱為實(shí)體類Bean:專門存儲業(yè)務(wù)數(shù)據(jù)的,如 Student、User 等
-
一類稱為業(yè)務(wù)處理 Bean:指 Service 或 Dao 對象,專門用于處理業(yè)務(wù)邏輯和數(shù)據(jù)訪問。
V:View,視圖層,指工程中的html或jsp等頁面,作用是與用戶進(jìn)行交互,展示數(shù)據(jù)
C:Controller,控制層,指工程中的servlet,作用是接收請求和響應(yīng)瀏覽器
MVC的工作流程:用戶通過視圖層發(fā)送請求到服務(wù)器,在服務(wù)器中請求被Controller接收,Controller調(diào)用相應(yīng)的Model層處理請求,處理完畢將結(jié)果返回到Controller,Controller再根據(jù)請求處理的結(jié)果找到相應(yīng)的View視圖,渲染數(shù)據(jù)后最終響應(yīng)給瀏覽器
SpringMvc的開發(fā)步驟
1.導(dǎo)入SpringMvc相關(guān)坐標(biāo)
2.配置SpringMvc核心控制器DispathcerServlet
3.創(chuàng)建Controller類和視圖頁面
4.使用注解配置Controller類中業(yè)務(wù)方法的映射地址
5.配置SpringMvc核心文件spring-mvc.xml
6.客戶端發(fā)起請求測試
? ? ? ? (1)導(dǎo)入Spring和SpringMvc的坐標(biāo)
<!-- spring坐標(biāo)--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.5.RELEASE</version><scope>compile</scope></dependency>
<!-- springmvc坐標(biāo)--><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.0.5.RELEASE</version></dependency>
? ? ?導(dǎo)入jsp和servlet坐標(biāo)
<dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>javax.servlet.jsp-api</artifactId><version>2.2.1</version><scope>provided</scope></dependency>
? ? ? ? (2)在web.xml配置SpringMvc的核心控制器
<servlet><servlet-name>DispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-mvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>DispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
? ? ? ? (3)創(chuàng)建Controller和業(yè)務(wù)方法
public class UserController {public String save(){System.out.println("Controller running ...");return "success";}
}
? ? ? ? 創(chuàng)建試圖頁面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body><h1>success</h1>
</body>
</html>
? ? ? ? (4)配置注解
@Controller
@RequestMapping("/userController")
public class UserController {@RequestMapping("/quick")public String save(){System.out.println("Controller running ...");return "success";}
}
? ? ? ? (5)創(chuàng)建spring-mvc.xml
<?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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!--controller組件掃描--><!-- <context:component-scan base-package="com.lin.controller"/>--><context:component-scan base-package="com.lin"><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan><!--配置內(nèi)部資源解釋器 我們可以通過屬性注入的方式修改視圖的的前后綴--><bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/jsp/"></property><property name="suffix" value=".jsp"></property></bean></beans>
springmvc流程圖示
?SpringMvc的組件解析
1.SpringMvc的執(zhí)行流程
?執(zhí)行流程的描述