搭建maven環(huán)境
項目框架視圖
配置Spring applicationContext.xml
ssm框架思路是由Spring框架作為基干,整合SpringMVC和Mybatis,首先就是要寫Spring的配置文件
- 開啟spring注解掃描
<context:component-scan base-package="">
- 不掃描controller注解 交給SpringMVC處理
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- 開啟spring注解的掃描 controller 不需要掃描-->
<context:component-scan base-package="cn.cai">
<!-- 將controller層交給 springmvc掃描-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
測試可用后再進行springmvc的配置
配置SpringMVC
配置web.xml
- 先在web.xml中配置dispatcherServlet 前置控制器 攔截所有客戶端頁面的請求
- 配置characterEncodingFilter 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:springmvc.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>
<filter>
<filter-name>characterEncodingFilter</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>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
配置springmvc.xml
- 開啟掃描 只掃描org.springframework.stereotype.Controller下的注解
- 配置視圖解析器internalResourceViewResolver 配置視圖的資源路徑以及轉到什么類型的文件
- 將dispatcherServlet攔截的靜態(tài)資源放行
- 開啟mvc注釋支持
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--開啟掃描器 掃描controller標簽-->
<context:component-scan base-package="cn.cai..controller">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--開啟視圖解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/"/>
<property name="suffix" value=".jsp" />
</bean>
<!--靜態(tài)資源放行-->
<mvc:resources location="/resources/images/" mapping="/resources/images/**"/>
<mvc:resources location="/resources/css/" mapping="/resources/css/**"/>
<mvc:resources location="/resources/js/" mapping="/resources/js/**"/>
<!--開啟mvc注解支持-->
<mvc:annotation-driven/>
</beans>
測試mvc可用后進行下一步配置
開啟spring監(jiān)聽器
tomcat開啟后會通過web.xml讀取springmvc的配置文件
可是spring的配置文件雖然寫完了 卻讀取不了 于是配置一個監(jiān)聽器 寫在web.xml中 在tomcat啟動時讀取spring的配置文件
spring自己寫了一個監(jiān)聽類 繼承自ServletContextListener 生命周期與tomcat一致
org.springframework.web.context.ContextLoaderListener
<!-- spring監(jiān)聽器 只會加載WEB/INF下的applicationContext.xml 需要另外配置 配置文件路徑-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置了spring的配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
整個流程
- 配置spring 關閉對controller的掃描 交由springmvc處理
- 配置springmvc
2.1 配置DispatcherServlet 前置控制器(web.xml)
2.2 配置CharacterEncodingFilter 字符編碼過濾器 (web.xml)
2.3 開啟掃描(springmvc.xml)
2.4 配置InternalResourceViewResolver視圖解析器(springmvc.xml)
2.5 放行前端控制器攔截的靜態(tài)資源(springmvc.xml)
2.6 開啟springmvc注解的支持(springmvc.xml) - 配置ContextLoaderListener監(jiān)聽器 (web.xml)