SpringMvc入门
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
Spring与Web环境的集成
1.ApplicationContext应用上下文的获取方式
分析
之前获取应用上下文对象每次都是从容器中获取编写时都需要new ClasspathXmlApplicationContext(Spring配置文件)这样的弊端就是配置加载多次应用上下文就创建多次。
2.Spring提供的获取应用上下文的工具
实现
1.在web.xml中导入ContextLoaderListener监听器导入Spring-web坐标
2.使用WebApplicationContextUtils获得应用上下文对象ApplicationContext
<1>导入Spring集成web的坐标
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<2>配置ContextLoaderListener监听器
<!--全局参数 全局参数主要是为了解耦。-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--Spring的监听器-->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<3> 通过工具获取应用上下文对象
将app存到servletConxt域中
优化的目的为了解耦减小代码之间的耦合。
public void contextInitialized(ServletContextEvent servletContextEvent) {
/* ServletContext servletContext = servletContextEvent.getServletContext();
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
servletContext.setAttribute("app",app);
System.out.println("spring容器创建完毕");*/
//优化后
/* ServletContext servletContext = servletContextEvent.getServletContext();
//读取web.xml中全局参数
//getInitParameter返回包含指定上下文范围初始化参数值的 String如果参数不存在则返回 null
String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");
ApplicationContext app = new ClassPathXmlApplicationContext(contextConfigLocation);
//将Spring的应用上下文对象存储到ServletContext域中
servletContext.setAttribute("app",app);
System.out.println("spring 容器创建完毕");*/
ServletContext servletContext = servletContextEvent.getServletContext();
String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");
ApplicationContext app = new ClassPathXmlApplicationContext(contextConfigLocation);
servletContext.setAttribute("app",app);
}
然后取出使用
@Override
protected 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是一种软件架构的思想将软件按照模型、视图、控制器来划分
MModel模型层指工程中的JavaBean作用是处理数据
JavaBean分为两类
-
一类称为实体类Bean专门存储业务数据的如 Student、User 等
-
一类称为业务处理 Bean指 Service 或 Dao 对象专门用于处理业务逻辑和数据访问。
VView视图层指工程中的html或jsp等页面作用是与用户进行交互展示数据
CController控制层指工程中的servlet作用是接收请求和响应浏览器
MVC的工作流程用户通过视图层发送请求到服务器在服务器中请求被Controller接收Controller调用相应的Model层处理请求处理完毕将结果返回到ControllerController再根据请求处理的结果找到相应的View视图渲染数据后最终响应给浏览器
SpringMvc的开发步骤
1.导入SpringMvc相关坐标
2.配置SpringMvc核心控制器DispathcerServlet
3.创建Controller类和视图页面
4.使用注解配置Controller类中业务方法的映射地址
5.配置SpringMvc核心文件spring-mvc.xml
6.客户端发起请求测试
(1)导入Spring和SpringMvc的坐标
<!-- spring坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
<scope>compile</scope>
</dependency>
<!-- springmvc坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
导入jsp和servlet坐标
<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)创建Controller和业务方法
public class UserController {
public String save(){
System.out.println("Controller running ...");
return "success";
}
}
创建试图页面
<%@ 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)创建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.xsd
http://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>
<!--配置内部资源解释器 我们可以通过属性注入的方式修改视图的的前后缀-->
<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的执行流程
执行流程的描述