
Java | Spring框架学习笔记--(4)MVC框架整合
发布日期:2021-05-07 23:15:27
浏览次数:18
分类:原创文章
本文共 2453 字,大约阅读时间需要 8 分钟。
MVC框架整合
为什么要整合MVC框架?
- MVC框架提供了控制器(Controller)调用Service
- 请求响应的处理
- 接受请求参数 request.getParameter(" ")
- 控制程序的运行流程
- 视图解析(JSP、JSON、Freemarker、Thyemeleaf)
环境搭建
-
新建一个Maven的项目/模块,骨架选择列表里的webapp
-
导入依赖
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.3.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.1</version> </dependency>
Spring整合MVC框架核心思路
-
如何准备工厂?
//使用的是WebXmlApplicationContext而不再是ClassPathApplicationContextApplicationContext applicationContext = new WebXmlApplicationContext("/applicationContext.xml")
-
如何保证工厂唯一、共用?
共用:把ApplicationContext存放在ServletContext域中
唯一:在ServletContext对象创建的同时创建ApplicationContext,因为ServletContext只会创建一次,所以ApplicationContext也就只有一个。使用ServletContextListener,就可以监听ServletContext的创建,把创建工厂的代码放在里面。
Spring已经封装了一个ContextLoaderListener,作用是为我们创建工厂并存放在ServletContext中。
ContextLoaderListener的使用方式:
由于他是一个监听器,所以在web.xml里配置即可。同时还要指定配置文件具体存放的位置!
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value></context-param>
在Controller中获取ApplicationContext:
@RequestMapping("/hello")public String hello(HttpServletRequest request){ ServletContext context = request.getServletContext(); ApplicationContext applicationContext = (ApplicationContext) context.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); System.out.println(applicationContext); return "success";}
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2025年04月01日 14时27分34秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
16 最接近的三数之和(排序、双指针)
2021-05-07
279 完全平方数(bfs)
2021-05-07
875 爱吃香蕉的珂珂(二分查找)
2021-05-07
桌面图标的自动排列图标
2021-05-07
第十一届蓝桥杯python组第二场省赛-数字三角形
2021-05-07
Jquery使用需要下载的文件
2021-05-07
BST中某一层的所有节点(宽度优先搜索)
2021-05-07
广度优先搜索
2021-05-07
Dijkstra算法的总结
2021-05-07
C语言的运算符和表达式
2021-05-07
Vue实现选项卡功能
2021-05-07
uni-app请求头中携带token
2021-05-07
vue中接收后台的图片验证码并显示
2021-05-07
Vue入门学习笔记(1)
2021-05-07
趣谈win10常用快捷键
2021-05-07
数学建模(NO.18灰色预测)
2021-05-07
数学建模更新12(数学线性规划模型1)
2021-05-07
Android,SharedPreferences的使用
2021-05-07
JPEG压缩技术
2021-05-07