三大框架整合:
第一步:导入jar包
第二步:配置spring的配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 包扫描 -->
<context:component-scan base-package="cn.itcast.springmvc.service"/>
<!-- 读取配置文件 -->
<context:property-placeholder location="classpath:*.properties"/>
<!-- 数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- sqlSessionFactoryBean -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:mappers/*.xml"></property>
</bean>
<!-- 配置我们所有的接口扫描包 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.itcast.springmvc.dao"></property>
</bean>
<!-- 事物管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启事物 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
第二步:配置mybatis的配置文件
第三步:配置springmvc的配置文件
<!-- 包扫描路径一定要配置到controller层,不要放大包扫描路径 -->
<context:component-scan base-package="cn.itcast.springmvc.controller"></context:component-scan>
<!-- 配置我们的处理器映射器和处理器适配器 -->
<mvc:annotation-driven/>
<!-- 配置我们的资源视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
第四步:配置web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</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>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
SpirngMVC处理器上面五个内置的参数:
HttpServletRequest
HttpServletResponse
HttpSession
Model
ModelMap
SpringMVC当中的简单参数绑定
@RequestMapping("/itemEdit.action")
public ModelAndView skipToEdit(Integer id){
Items items = itemService.selectItemByPrimaryKey(id);
ModelAndView view = new ModelAndView();
view.setViewName("editItem");
view.addObject("item", items);
return view;
}
保证前端传递参数名称与方法上的名称一致即可
SpirngMC当中绑定pojo参数
@RequestMapping("/updateitem.action")
public String updateItem(Items items) throws UnsupportedEncodingException{
itemService.updateItem(items);
return "success";
}
保证前端页面传递参数与对象中的属性保持一致即可
SpirngMVC当中的自定义参数绑定
第一步:定义我们的dateConverters
public class DateConverter implements Converter<String, Date> {
@Override
public Date convert(String source) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date parse = format.parse(source);
return parse;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
第二步:配置我们的转换器
<!-- 配置我们的处理器映射器和处理器适配器 -->
<mvc:annotation-driven conversion-service="conversionService"/>
<!-- 配置我们的转换器 -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean id="dateConverter" class="cn.itcast.springmvc.util.DateConverter"></bean>
</set>
</property>
</bean>
课程回顾:
SpringMVC的介绍
SpringMVC的流程
SpringMVC的入门案例,配置四个东西,三个核心组件,一个servlet
SpringMVC的各个组件的详细介绍
三大框架的整合:
SpringMVC参数的绑定:简单数据类型绑定,对象数据类型绑定,包装数据类型绑定,自定义数据类型绑定