Spring集成Web环境
- 导入坐标
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
- 【错误】:Maven异常:Maven异常:Could not transfer artifact org.slf4j:slf4j-api:jar:1.6.4 from/to central
- 解决办法:在使用idea时,pom文件报错,是因为jar包下载不完整,第一次下载失败时会在对应jar包的文件目录下生成一个lastUpdated文件,导致以后都不会真正下载jar包,将本地仓库下的lastUpdated文件删除执行compile,在更新一下jar包就可。
- 创建UserServlet类
@WebServlet(name = "UserServlet", value = "/UserServlet")
public class UserServlet extends HttpServlet {...}
- 配置Web.xml
...
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>com.itheima.web.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/userServlet</url-pattern>
</servlet-mapping>
...
- Tomcat 发布
ApplicationContext应用上下文
- 【手写】: 在Web项目中,可以使用ServletContextLintener监听Web应用的启动,在Web应用启动时,就加载Spring的配置文件,创建应用上下文对象ApplicationContext,再将其存储到最大域servletContext中,这样就可以在任意位置从域中获得应用上下文ApplicationContext对象了
- 创建 lintener 加载应用上下文
package com.itheima.listener;
......
public class ContextLoaderListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
ApplicationContext app=new AnnotationConfigApplicationContext(SpringConfiguration.class);
//将Spring的应用上下文对象存储到ServletContext对象中
ServletContext servletContext = sce.getServletContext();
servletContext.setAttribute("app",app);
System.out.printf("Spring容器创建完毕。。。。");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}
- 在web.xml中配置监听器
<listener>
<listener-class>com.itheima.listener.ContextLoaderListener</listener-class>
</listener>
- 在 Servlet 应用
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// ApplicationContext app=new AnnotationConfigApplicationContext(SpringConfiguration.class);
ServletContext servletContext = this.getServletContext();
ApplicationContext app= (ApplicationContext) servletContext.getAttribute("app");
UserService userService = app.getBean(UserService.class);
userService.save();
}
- 优化
- 在web.xml里配置application.xml文件
<!--全局初始化参数-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>applicationContext.xml</param-value>
</context-param>
- 新建一个 WebApplicationContextUtils 工具类,用于获取ApplicationContext
public class WebApplicationContextUtils {
public static ApplicationContext getWebApplicationContext(ServletContext servletContext){
return (ApplicationContext) servletContext.getAttribute("app");
}
}
- 此时 Servlet 类从 WebApplicationContextUtils 中获取 ApplicationContext
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// ApplicationContext app=new AnnotationConfigApplicationContext(SpringConfiguration.class);
ServletContext servletContext = this.getServletContext();
// ApplicationContext app= (ApplicationContext) servletContext.getAttribute("app");
ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
UserService userService = app.getBean(UserService.class);
userService.save();
}
Spring 提供获取上下文的工具
- 上面的分析不用手动实现,Spring提供了一个监听器ContextLoaderListener就是对上述功能的封装,该监听器内部加载Spring配置文件,创建应用上下文对象,并存储到Servrlet域中,提供了一个客户端工具WenApplicationCoontextUtils供使用者获得应用上下文对象。
- 在web.xml中配置ContextLoaderLinstener监听器(导入spring-web坐标)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!--全局初始化参数-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--配置监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
- 使用WebApplicationContextUtils获得应用上下文对象ApplicationContext
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
ApplicationContext app = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
UserService userService = app.getBean(UserService.class);
userService.save();
}
- 【报错】org.apache.catalina.core.StandardContext.listenerStart Error configuring application listener
- [解决] 取自:Dan1374219106
SpringMVC
SpringMVC快速入门
- 【需求】客户端发起请求,服务器接受请求,执行逻辑并进行视图跳转
- 开发步骤:
- 在 pom.xml 导入SpringMVC相关坐标
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
- 在 web.xml 配置SpringMVC核心控制器DispathcerServlet
<!--配置SpringMVC的前端控制器-->
<servlet>
<servlet-name>DispatherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatherServlet</servlet-name>
<url-pattern>/</url-pattern><!--缺省-->
</servlet-mapping>
- 创建Conroller类和视图界面
@Controller
public class UserController {
@RequestMapping("/quick")
public String save() {
System.out.println("Controller save running....");
return "success.jsp";
}
}
在web下新建一个 success.jsp
4. 使用注解配置Contrller类中业务方法的映射地址
<?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.itheima.controller"></context:component-scan>
</beans>
- 配置SpringMVC核心文件spring-mvc.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>
- 客户端发起请求测试
请求地址:http://localhose:8080/quick
SpringMVC 注解解析
- RequestMapping
- 作用:用于建立请求URL和处理请求方法之间的对应关系
- 位置:
– 类上,请求URL的第一季方位目录。此处不写就相当于应用的根目录
– 方法上,请求URL的第二季访问目录,与类上的市容@RequestMapping标注的一级目录一起组成访问的虚拟目录 - 属性:
– value:用于指定请求的URl。它和path属性的作用是一样的
– method:用于指定请求的方式
– params:用于指定限制请求参数的条件。它支持简单的表达式。要求请求参数的key和value必须和配置的一模一样。
– 例如:
– params={“accountName”},表示请求参数必须有acountName
– params={“money!100”},表示请求参数中money不能是100
@RequestMapping(value = "/quick",method = RequestMethod.GET,params = {"userName"})
组件扫描
<!--Controller的组件扫描-->
<context:component-scan base-package="com.itheima">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
XML的配置解析
- 视图解析器
SpringMvc有默认组件配置,默认组件都是DispatcherServlet.properties配置文件中配置的,
该配置文件地址org/springframework/web/servlet/DispatcherServlet.properties,该文件中配置了默认的视图解析器,如下:
org.springframework.web.servlet.ViewResolver=org.springframework.web.servlet.view.InternalResourceViewResolver
查看该解析器源码,可以看到该解析器的默认设置:
REDIRECT_UR工_PREF工X= "redirect: " --重定向前缀
FORNARD_URL_PREEIX ="forward : " --转发前缀(默认值)
prefix = ""; --视图名称前缀
suffix =“; --视图名称后缀
SpringMVC的相关组件
- 前端控制器:DispatcherServlet
- 处理器映射器:HandlerMapping·
- 处理器适配器: HandlerAdapter
- 处理器: Handler
- 视图解析器:View Resolver
- 视图: View
SpringMVC的注解和配置
- 请求映射注解:@RequestMapping
- 视图解析器配置:
REDIRECT_URL_PREFIX = “redirect:”
FORWARD_URL_PREFIX =““forward:”
prefix = “”;
suffix =”";