1.理解:SpringMVC框架是替换目前程序中controller(struts2),起到控制器的作用(Controller)
2.特点:SpringMVC是一个轻量级的框架,是MVC框架,整个MVC架构中充当是 控制器(Contoller),采用注解式开发,效率更高同时更加的灵活
3.springmvc.xml文件的配置:
<!-- 1. 开启注解包 -->
<context:component-scan base-package="com.controller包" use-default-filters="false">
<!-- 指定只扫描 的注解类型为:Controller -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 2. 请求处理 -->
<mvc:annotation-driven />
<!-- 3. 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 4.解析静态资源的访问 -->
<mvc:default-servlet-handler/>
<!-- 5.配置文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 配置文件大小-->
<property name="maxUploadSize" value="1000000000000"></property>
</bean>
<!-- 6.配置拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/拦截资源/*"/>
<mvc:exclude-mapping path="/不拦截资源/*"/>
<bean class="com.拦截器的位置></bean>
</mvc:interceptor>
</mvc:interceptors>
4.spring.xml的配置
<!-- 0. 引入数据库相关小配置文件 -->
<context:property-placeholder location="classpath:com/conf/db.properties">
</context:property-placeholder>
<!-- 1.配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.mydirver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 2.配置sqlSessionFactory-复杂对象 -->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 对实体类起别名-->
<property name="typeAliasesPackage" value="com.baizhi.zmj.entity"></property>
<!-- 注册mapper文件-->
<!--<property name="mapperLocations" value="classpath:com/baizhi/zmj/mapper/*DaoMapper.xml"></property>-->
</bean>
<!-- 3. 配置dao组件 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="factory"></property>
<!-- 注入对应接口名(注入需要扫描的dao所在包)-->
<property name="basePackage" value="com.dao"></property>
</bean>
<!--4. 开启注解包扫描 -->
<context:component-scan base-package="com" use-default-filters="true">
<!-- 指定要排除的注解类型 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 5.配置事务管理器 -->
<bean id="tm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 6. 事务注解生效 -->
<tx:annotation-driven transaction-manager="tm"></tx:annotation-driven>
5.web.xml的配置
<!-- 1. 配置post请求中文乱码,配置编码的过滤器 -->
<filter>
<filter-name>ch</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>ch</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 2. 配置springMVC的前置控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置 springmvc.xml文件的位置 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 3. 启动spring工厂 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 4. 告知 spring.xml文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
6.springmvc的常见注解
@Controller
@RequestMapping
@RequestParam(value = "默认值")
@DateTimeFormat(pattern="yyyy-MM-dd")日期类型
7.参数
散列,数组,集合,对象
8.跳转方式:
(1)response
return "redirect:/xxx.jsp";
(2)forward
return "xxx" ;——jsp的前缀名
9.父子容器的污染问题:
spring.xml中 开启注解包扫描 扫描所有的包,排除Controller 父容器
springmvc.xml中开启注解包扫描,只允许Controller 子容器