中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

網(wǎng)站建設(shè)規(guī)劃北京今日重大新聞

網(wǎng)站建設(shè)規(guī)劃,北京今日重大新聞,色塊網(wǎng)站,網(wǎng)站內(nèi)部資源推廣Spring與Web環(huán)境的集成 1.ApplicationContext應(yīng)用上下文的獲取方式 分析 之前獲取應(yīng)用上下文對象每次都是從容器中獲取,編寫時(shí)都需要new ClasspathXmlApplicationContext(Spring配置文件),這樣的弊端就是配置加載多次應(yīng)用上下文就創(chuàng)建多次。 在Web項(xiàng)目…

Spring與Web環(huán)境的集成

1.ApplicationContext應(yīng)用上下文的獲取方式

分析

之前獲取應(yīng)用上下文對象每次都是從容器中獲取,編寫時(shí)都需要new ClasspathXmlApplicationContext(Spring配置文件),這樣的弊端就是配置加載多次應(yīng)用上下文就創(chuàng)建多次。

在Web項(xiàng)目中,可以使用ServletContextListener監(jiān)聽Web應(yīng)用的啟動,我們可以在Web應(yīng)用啟動時(shí),就加
載Spring的配置文件,創(chuàng)建應(yīng)用上下文對象ApplicationContext,在將其存儲到最大的域servletContext
中,這樣就可以在任意位置從域中獲得應(yīng)用上下文ApplicationContext對象了。

2.Spring提供的獲取應(yīng)用上下文的工具
Spring提供了一個(gè)監(jiān)聽器ContextLoaderListener就是對上述功能的封裝,該監(jiān)
聽器內(nèi)部加載Spring配置文件,創(chuàng)建應(yīng)用上下文對象,并存儲到ServletContext域中,提供了一個(gè)客戶端工
WebApplicationContextUtils供使用者獲得應(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í)行流程的描述

1.用戶發(fā)送請求至前端控制器DispatcherServlet。
2. DispatcherServlet收到請求調(diào)用HandlerMapping處理器映射器。
3. 處理器映射器找到具體的處理器(可以根據(jù)xml配置、注解進(jìn)行查找),生成處理器對象及處理器攔截器(如果 有則生成)一并返回給DispatcherServlet。
4. DispatcherServlet調(diào)用HandlerAdapter處理器適配器。
5.HandlerAdapter經(jīng)過適配調(diào)用具體的處理器(Controller,也叫后端控制器)。
6. Controller執(zhí)行完成返回ModelAndView。
7 HandlerAdapter將controller執(zhí)行結(jié)果ModelAndView返回給DispatcherServlet。
8. DispatcherServlet將ModelAndView傳給ViewReslover視圖解析器。
9.ViewReslover解析后返回具體View。
10. DispatcherServlet根據(jù)View進(jìn)行渲染視圖(即將模型數(shù)據(jù)填充至視圖中)。DispatcherServlet響應(yīng)用戶。

主要的組件解析

1. 前端控制器:DispatcherServlet
用戶請求到達(dá)前端控制器,它就相當(dāng)于 MVC 模式中的 C,DispatcherServlet 是整個(gè)流程控制的中心,由
它調(diào)用其它組件處理用戶的請求,DispatcherServlet 的存在降低了組件之間的耦合性。
2. 處理器映射器:HandlerMapping
HandlerMapping 負(fù)責(zé)根據(jù)用戶請求找到 Handler 即處理器,SpringMVC 提供了不同的映射器實(shí)現(xiàn)不同的
映射方式,例如:配置文件方式,實(shí)現(xiàn)接口方式,注解方式等。
3. 處理器適配器:HandlerAdapter
通過 HandlerAdapter 對處理器進(jìn)行執(zhí)行,這是適配器模式的應(yīng)用,通過擴(kuò)展適配器可以對更多類型的處理
器進(jìn)行執(zhí)行。
4. 處理器:Handler
它就是我們開發(fā)中要編寫的具體業(yè)務(wù)控制器。由 DispatcherServlet 把用戶請求轉(zhuǎn)發(fā)到 Handler。由
Handler 對具體的用戶請求進(jìn)行處理。
5. 視圖解析器:View Resolver
View Resolver 負(fù)責(zé)將處理結(jié)果生成 View 視圖,View Resolver 首先根據(jù)邏輯視圖名解析成物理視圖名,即
具體的頁面地址,再生成 View 視圖對象,最后對 View 進(jìn)行渲染將處理結(jié)果通過頁面展示給用戶。
6. 視圖:View
SpringMVC 框架提供了很多的 View 視圖類型的支持,包括:jstlView、freemarkerView、pdfView等。最
常用的視圖就是 jsp。一般情況下需要通過頁面標(biāo)簽或頁面模版技術(shù)將模型數(shù)據(jù)通過頁面展示給用戶,需要由程
序員根據(jù)業(yè)務(wù)需求開發(fā)具體的頁面

注解解析

@RequestMapping
作用:用于建立請求 URL 和處理請求方法之間的對應(yīng)關(guān)系
位置:
?類上,請求URL 的第一級訪問目錄。此處不寫的話,就相當(dāng)于應(yīng)用的根目錄
?方法上,請求 URL 的第二級訪問目錄,與類上的使用@ReqquestMapping標(biāo)注的一級目錄一起組成訪問虛擬路徑
屬性:
?value:用于指定請求的URL。它和path屬性的作用是一樣的
?method:用于指定請求的方式
?params:用于指定限制請求參數(shù)的條件。它支持簡單的表達(dá)式。要求請求參數(shù)的key和value必須和配置的一模一樣
例如:
?params = {"accountName"},表示請求參數(shù)必須有accountName
?params = {"moeny!100"},表示請求參數(shù)中money不能是100
http://www.risenshineclean.com/news/49905.html

相關(guān)文章:

  • 網(wǎng)站開發(fā)采購合同模板下載b2b
  • 做素材網(wǎng)站存儲搜索最多的關(guān)鍵詞的排名
  • 廣東網(wǎng)站建設(shè)微信官網(wǎng)開發(fā)網(wǎng)絡(luò)營銷策劃模板
  • 哪兒提供邢臺做網(wǎng)站windows系統(tǒng)優(yōu)化軟件排行榜
  • 一個(gè)公網(wǎng)ip可以做幾個(gè)網(wǎng)站青島官網(wǎng)seo方法
  • 怎么做網(wǎng)站企業(yè)文化欄目網(wǎng)站推廣途徑和推廣要點(diǎn)
  • 24小時(shí)學(xué)會網(wǎng)站建設(shè) 下載定制網(wǎng)站開發(fā)
  • 聯(lián)通做網(wǎng)站寧波seo網(wǎng)站推廣軟件
  • 網(wǎng)頁制作價(jià)格私活seodao cn
  • 商會網(wǎng)站建設(shè)方案廣告策劃公司
  • 商城類網(wǎng)站功能列表北京seo排名服務(wù)
  • 自己做時(shí)時(shí)彩票網(wǎng)站北京seo排名外包
  • 做網(wǎng)站小編怎么樣做網(wǎng)絡(luò)推廣可以通過哪些渠道推廣
  • 網(wǎng)站建設(shè)優(yōu)化公司cps推廣接單平臺
  • 網(wǎng)站開發(fā)服務(wù)商百度網(wǎng)頁版網(wǎng)址
  • 能查個(gè)人信息的網(wǎng)站百度競價(jià)托管代運(yùn)營公司
  • 營銷型網(wǎng)站建設(shè)定制網(wǎng)絡(luò)營銷的四種模式
  • 怎樣免費(fèi)建立自己網(wǎng)站長春做網(wǎng)站推薦選吉網(wǎng)傳媒好
  • 什么是網(wǎng)站app惠州seo管理
  • wordpress手機(jī)圖片站蘇州百度代理公司
  • 平面設(shè)計(jì)網(wǎng)站大全有哪些成人計(jì)算機(jī)培訓(xùn)機(jī)構(gòu)哪個(gè)最好
  • 一些好用的網(wǎng)站個(gè)人博客網(wǎng)站設(shè)計(jì)畢業(yè)論文
  • 做自媒體的網(wǎng)站谷歌推廣開戶多少費(fèi)用
  • 自己做的網(wǎng)站如何加視頻長春seo排名公司
  • wordpress做的好的網(wǎng)站友情鏈接的形式有哪些
  • 深圳網(wǎng)站建設(shè)公司 交通如何推廣app更高效
  • 珠海網(wǎng)站優(yōu)化百度導(dǎo)航2023年最新版
  • 怎樣查看wordpress用的什么主題網(wǎng)站seo工具
  • 安徽六安郵政編碼seo快速排名軟件app
  • 關(guān)鍵詞的分類和優(yōu)化seo關(guān)鍵詞首頁排名代發(fā)