SSM_day01
环境搭建
配置文件web.xml
创建Spring框架的IOC容器
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring/spring-*.xml</param-value>
</context-param>
<!-- 监听器:web监听器:
创建Spring框架的IOC容器.
将IOC容器对象存放到application域.
ApplicationContext ioc = WebApplicationContextUtils.getWebApplicationContext(application);
ServletContextListener监听器接口:
监听ServletContext(application域)创建和销毁.
如果监听到ServletContext创建(服务器启动),就会创建IOC容器(XmlWebApplicationContext).
如果监听到ServletContext销毁(服务器停止,或卸载项目),就会销毁IOC容器.
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
ContextLoaderListener实现ServletContextListener接口,执行以下方法
@Override
public void contextInitialized(ServletContextEvent event) { //application域被创建时执行
initWebApplicationContext(event.getServletContext());
}
//initWebApplicationContext创建XmlWebApplicationContext(IOC容器)
//configureAndRefreshWebApplicationContext方法(IOC容器初始化),
//String initParameter = sc.getInitParameter(CONFIG_LOCATION_PARAM);从application域寻找contextConfigLocation获取初始化参数,
//不进行设置,默认读取WEB-INF/applicationContext.xml
/**
* Close the root web application context.
*/
@Override
public void contextDestroyed(ServletContextEvent event) { //application域被销毁时执行
closeWebApplicationContext(event.getServletContext());
ContextCleanupListener.cleanupAttributes(event.getServletContext());
}
appplication域在服务器启动时创建
appplication域在服务器停止或卸载项目时销毁
字符编码过滤器 :只负责解决POST请求乱码问题.解决GET请求乱码问题在Tomcat/conf/server.xml中设置编码
Connector URIEncoding=“UTF-8” connectionTimeout=“20000” port=“8080” protocol=“HTTP/1.1” redirectPort=“8443”/
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern> <!--对所有请求路径进行过滤-->
<!--
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
-->
</filter-mapping>
支持RESTful风格.将POST请求转换为PUT或DELETE请求.会读取隐含参数: “_method” ;在核心控制执行前执行
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<servlet-name>springmvc</servlet-name>
</filter-mapping>
核心控制器:
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring/springmvc-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup> <!--服务器启动时创建servlet对象-->
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- URL : http://localhost:8080/contextPath/order/save.action -->
<!-- 精确匹配
<url-pattern>/user/save</url-pattern>
-->
<!-- 路径匹配 -->
<!--
<url-pattern>/user/*</url-pattern>
<url-pattern>/*</url-pattern>
-->
<!-- 扩展匹配 -->
<url-pattern>*.htm</url-pattern><!-- 主要用于进行页面跳转 -->
<url-pattern>*.do</url-pattern> <!-- 主要用于业务逻辑处理 -->
<!-- 默认匹配
<url-pattern>/</url-pattern>
-->
</servlet-mapping>