一、Spring
1.简介
Spring框架是一个分层架构,包含一系列的功能要素,并被分为大约20个模块。
这些模块被总结为以下几个部分:
-
Core Container
Core Container(核心容器)包含有Core、Beans、Context和Expression Language模块
Core和Beans模块是框架的基础部分,提供IoC(转控制)和依赖注入特性。这里的基础概念是BeanFactory,它提供对Factory模式的经典实现来消除对程序性单例模式的需要,并真正地允许你从程序逻辑中分离出依赖关系和配置- Core模块主要包含Spring框架基本的核心工具类
- Beans模块是所有应用都要用到的,它包含访问配置文件、创建和管理bean以及进行Inversion of Control/Dependency Injection(Ioc/DI)操作相关的所有类
- Context模块构建于Core和Beans模块基础之上,提供了一种类似于JNDI注册器的框架式的对象访问方法。Context模块继承了Beans的特性,为Spring核心提供了大量扩展,添加了对国际化(如资源绑定)、事件传播、资源加载和对Context的透明创建的支持。ApplicationContext接口是Context模块的关键
- Expression Language模块提供了一个强大的表达式语言用于在运行时查询和操纵对象,该语言支持设置/获取属性的值,属性的分配,方法的调用,访问数组上下文、容器和索引器、逻辑和算术运算符、命名变量以及从Spring的IoC容器中根据名称检索对象
-
Data Access/Integration
- JDBC模块提供了一个JDBC抽象层,它可以消除冗长的JDBC编码和解析数据库厂商特有的错误代码,这个模块包含了Spring对JDBC数据访问进行封装的所有类
- ORM模块为流行的对象-关系映射API,如JPA、JDO、Hibernate、iBatis等,提供了一个交互层,利用ORM封装包,可以混合使用所有Spring提供的特性进行O/R映射,如前边提到的简单声明性事务管理
-
Web
Web上下文模块建立在应用程序上下文模块之上,为基于Web的应用程序提供了上下文,所以Spring框架支持与Jakarta Struts的集成。Web模块还简化了处理多部分请求以及将请求参数绑定到域对象的工作。Web层包含了Web、Web-Servlet、Web-Struts和Web、Porlet模块
- Web模块:提供了基础的面向Web的集成特性,例如,多文件上传、使用Servlet listeners初始化IoC容器以及一个面向Web的应用上下文,它还包含了Spring远程支持中Web的相关部分
- Web-Servlet模块web.servlet.jar:该模块包含Spring的model-view-controller(MVC)实现,Spring的MVC框架使得模型范围内的代码和web forms之间能够清楚地分离开来,并与Spring框架的其他特性基础在一起
- Web-Struts模块:该模块提供了对Struts的支持,使得类在Spring应用中能够与一个典型的Struts Web层集成在一起
- Web-Porlet模块:提供了用于Portlet环境和Web-Servlet模块的MVC的实现
-
AOP
AOP模块提供了一个符合AOP联盟标准的面向切面编程的实现,它让你可以定义例如方法拦截器和切点,从而将逻辑代码分开,降低它们之间的耦合性,利用source-level的元数据功能,还可以将各种行为信息合并到你的代码中
Spring AOP模块为基于Spring的应用程序中的对象提供了事务管理服务,通过使用Spring AOP,不用依赖EJB组件,就可以将声明性事务管理集成到应用程序中
-
Test
Test模块支持使用Junit和TestNG对Spring组件进行测试
2. 专业术语了解
- 组件/框架设计
侵入式设计
引入了框架,对现有的类的结构有影响;即需要实现或继承某些特定类。
例如: Struts框架
非侵入式设计
引入了框架,对现有的类结构没有影响。
例如:Hibernate框架 / Spring框架
- 控制反转(IOC)
对象的创建交给外部容器完成,这个就做控制反转。
- 依赖注入(DI)
处理对象的依赖关系
区别:
1)控制反转, 解决对象创建的问题 【对象创建交给别人】
2)依赖注入,在创建完对象后, 对象的关系的处理就是依赖注入 【通过set方法依赖注入】
- AOP
面向切面编程。切面,简单来说来可以理解为一个类,由很多重复代码形成的类。
切面举例:事务、日志、权限;
3. 开发步骤
spring各个版本中:
在3.0以下的版本,源码有spring中相关的所有包【spring功能 + 依赖包】
如2.5版本;
在3.0以上的版本,源码中只有spring的核心功能包【没有依赖包】
(如果要用依赖包,需要单独下载!)
1) 源码, jar文件:spring-framework-3.2.5.RELEASE
commons-logging-1.1.3.jar 日志
spring-beans-3.2.5.RELEASE.jar bean节点
spring-context-3.2.5.RELEASE.jar spring上下文节点
spring-core-3.2.5.RELEASE.jar spring核心功能
spring-expression-3.2.5.RELEASE.jar spring表达式相关表
以上是必须引入的5个jar文件,在项目中可以用户库管理!
2) 核心配置文件: applicationContext.xml
Spring配置文件:applicationContext.xml / bean.xml
约束参考:
spring-framework-3.2.5.RELEASE\docs\spring-framework-reference\htmlsingle\index.html如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
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">
</beans>
3)API
public class App {
// 1. 通过工厂类得到IOC容器创建的对象
@Test
public void testIOC() throws Exception {
// 创建对象
// User user = new User();
// 现在,把对象的创建交给spring的IOC容器
Resource resource = new ClassPathResource("cn/itcast/a_hello/applicationContext.xml");
// 创建容器对象(Bean的工厂), IOC容器 = 工厂类 + applicationContext.xml
BeanFactory factory = new XmlBeanFactory(resource);
// 得到容器创建的对象
User user = (User) factory.getBean("user");
System.out.println(user.getId());
}
//2. (方便)直接得到IOC容器对象
@Test
public void testAc() throws Exception {
// 得到IOC容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("cn/itcast/a_hello/applicationContext.xml");
// 从容器中获取bean
User user = (User) ac.getBean("user");
System.out.println(user);
}
}
4.bean对象创建的细节
/**
* 1) 对象创建: 单例/多例
* scope="singleton", 默认值, 即 默认是单例 【service/dao/工具类】
* scope="prototype", 多例; 【Action对象】
*
* 2) 什么时候创建?
* scope="prototype" 在用到对象的时候,才创建对象。
* scope="singleton" 在启动(容器初始化之前), 就已经创建了bean,且整个应用只有一个。
* 3)是否延迟创建
* lazy-init="false" 默认为false, 不延迟创建,即在启动时候就创建对象
* lazy-init="true" 延迟初始化, 在用到对象的时候才创建对象
* (只对单例有效)
* 4) 创建对象之后,初始化/销毁
* init-method="init_user" 【对应对象的init_user方法,在对象创建爱之后执行 】
* destroy-method="destroy_user" 【在调用容器对象的destriy方法时候执行,(容器用实现类)】
*/
@Test
public void testIOC() throws Exception {
// 得到IOC容器对象 【用实现类,因为要调用销毁的方法】
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("cn/itcast/a_hello/applicationContext.xml");
System.out.println("-----容器创建-----");
// 从容器中获取bean
User user1 = (User) ac.getBean("user");
User user2 = (User) ac.getBean("user");
System.out.println(user1);
System.out.println(user2);
// 销毁容器对象
ac.destroy();
}
5. Spring IOC容器
1)创建对象
Spring IOC容器,是spring核心内容。
作用: 创建对象 & 处理对象的依赖关系
IOC容器创建对象,有几种方式:
- 调用无参数构造器
- 带参数构造器
- 工厂创建对象
工厂类,静态方法创建对象
工厂类,非静态方法创建对象
<?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:p="http://www.springframework.org/schema/p"
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">
<!-- ###############对象创建############### -->
<!-- 1. 默认无参数构造器
<bean id="user1" class="cn.itcast.b_create_obj.User"></bean>
-->
<!-- 2. 带参数构造器 -->
<bean id="user2" class="cn.itcast.b_create_obj.User">
<constructor-arg index="0" type="int" value="100"></constructor-arg>
<constructor-arg index="1" type="java.lang.String" value="Jack"></constructor-arg>
</bean>
<!-- 定义一个字符串,值是"Jack" ; String s = new String("jack")-->
<bean id="str" class="java.lang.String">
<constructor-arg value="Jacks"></constructor-arg>
</bean>
<bean id="user3" class="cn.itcast.b_create_obj.User">
<constructor-arg index="0" type="int" value="100"></constructor-arg>
<constructor-arg index="1" type="java.lang.String" ref="str"></constructor-arg>
</bean>
<!-- 3. 工厂类创建对象 -->
<!-- # 3.1 工厂类,实例方法 -->
<!-- 先创建工厂 -->
<bean id="factory" class="cn.itcast.b_create_obj.ObjectFactory"></bean>
<!-- 在创建user对象,用factory方的实例方法 -->
<bean id="user4" factory-bean="factory" factory-method="getInstance"></bean>
<!-- # 3.2 工厂类: 静态方法 -->
<!--
class 指定的就是工厂类型
factory-method 一定是工厂里面的“静态方法”
-->
<bean id="user" class="cn.itcast.b_create_obj.ObjectFactory" factory-method="getStaticInstance"></bean>
</beans>
2)对象依赖关系
Spring中,如何给对象的属性赋值? 【DI, 依赖注入】
- 通过构造函数
- 通过set方法给属性注入值
- p名称空间
- 自动装配(了解)
- 注解
(1)(常用)Set方法注入值
<!-- dao instance -->
<bean id="userDao" class="cn.itcast.c_property.UserDao"></bean>
<!-- service instance -->
<bean id="userService" class="cn.itcast.c_property.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
<!-- action instance -->
<bean id="userAction" class="cn.itcast.c_property.UserAction">
<property name="userService" ref="userService"></property>
</bean>
(2)内部bean
<!-- ##############内部bean############## -->
<bean id="userAction" class="cn.itcast.c_property.UserAction">
<property name="userService">
<bean class="cn.itcast.c_property.UserService">
<property name="userDao">
<bean class="cn.itcast.c_property.UserDao"></bean>
</property>
</bean>
</property>
</bean>
(3)p 名称空间注入属性值 (优化)
<?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:p="http://www.springframework.org/schema/p"
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">
<!-- ###############对象属性赋值############### -->
<!--
给对象属性注入值:
# p 名称空间给对象的属性注入值
(spring3.0以上版本才支持)
-->
<bean id="userDao" class="cn.itcast.c_property.UserDao"></bean>
<bean id="userService" class="cn.itcast.c_property.UserService" p:userDao-ref="userDao"></bean>
<bean id="userAction" class="cn.itcast.c_property.UserAction" p:userService-ref="userService"></bean>
<!-- 传统的注入:
<bean id="user" class="cn.itcast.c_property.User" >
<property name="name" value="xxx"></property>
</bean>
-->
<!-- p名称空间优化后 -->
<bean id="user" class="cn.itcast.c_property.User" p:name="Jack0001"></bean>
</beans>
(4)自动装配(了解)
- 根据名称自动装配:autowire="byName";即自动去IOC容器中找与属性名同名的引用的对象,并自动注入。
<!-- ###############自动装配############### -->
<bean id="userDao" class="cn.itcast.d_auto.UserDao"></bean>
<bean id="userService" class="cn.itcast.d_auto.UserService" autowire="byName"></bean>
<!-- 根据“名称”自动装配: userAction注入的属性,会去ioc容器中自动查找与属性同名的对象 -->
<bean id="userAction"
class="cn.itcast.d_auto.UserAction" autowire="byName"></bean>
也可以定义到全局, 这样就不用每个bean节点都去写autowire=”byName”
<?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:p="http://www.springframework.org/schema/p"
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" default-autowire="byName"> 根据名称自动装配(全局)
<!-- ###############自动装配############### -->
<bean id="userDao" class="cn.itcast.d_auto.UserDao"></bean>
<bean id="userService" class="cn.itcast.d_auto.UserService"></bean>
<bean id="userAction" class="cn.itcast.d_auto.UserAction"></bean>
</beans>
- 根据类型自动装配:autowire="byType";但必须确保改类型在IOC容器中只有一个对象;否则报错。
<?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:p="http://www.springframework.org/schema/p"
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" default-autowire="byType">
<!-- ###############自动装配############### -->
<bean id="userDao" class="cn.itcast.d_auto.UserDao"></bean>
<bean id="userService" class="cn.itcast.d_auto.UserService"></bean>
<!-- 如果根据类型自动装配: 必须确保IOC容器中只有一个该类型的对象 -->
<bean id="userAction" class="cn.itcast.d_auto.UserAction"></bean>
<!-- 报错: 因为上面已经有一个该类型的对象,且使用了根据类型自动装配
<bean id="userService_test" class="cn.itcast.d_auto.UserService" autowire="byType"></bean>
-->
</beans>
总结:Spring提供的自动装配主要是为了简化配置; 但是不利于后期的维护。(一般不推荐使用)
(5)注解
注解方式可以简化spring的IOC容器的配置!
使用注解步骤:
- 先引入context名称空间
即 xmlns:context="http://www.springframework.org/schema/context"
- 开启注解扫描
即<context:component-scan base-package="cn.itcast.e_anno2"></context:component-scan>
-
使用注解
通过注解的方式,把对象加入ioc容器。创建对象以及处理对象依赖关系,相关的注解:
@Component 指定把一个对象加入IOC容器
@Repository 作用同@Component; 在持久层使用
@Service 作用同@Component; 在业务逻辑层使用
@Controller 作用同@Component; 在控制层使用
@Resource 属性注入
总结:
使用注解,可以简化配置,且可以把对象加入IOC容器,及处理依赖关系(DI); 注解可以和XML配置一起使用。
二、Spring MVC
1. springMvc:是一个表现层框架,
作用:就是从请求中接收传入的参数,将处理后的结果数据返回给页面展示。
原理 如下图所示:
关于上图的一个说明:
1)首先,Http请求:将客户端请求提交到DispatcherServlet这个Servlet。
2)然后,找到处理器:由DispatcherServlet控制器查询出一个或多个HandlerMapping,并且找到处理请求的Controller(控制器类)。
3)然后,调用处理器:DispatcherServlet将请求提交到Controller(控制器类)。
4)、5)然后,调用业务处理和返回结果:Controller调用业务逻辑处理后,返回了ModelAndView。
6)、7)然后,处理视图映射并返回模型: DispatcherServlet查询到一个或多个ViewResoler视图解析器后,找到ModelAndView指定的视图(页面)。
8)最后,就是Http响应了:视图负责将结果显示到客户端页面上。
spring mvc处理请求的流程图:
2. 核心配置文件(如springMVC.xml)需要配置的东西:
- 配置@Controller注解扫描
<context:component-scan base-package="com.spring.controller"></context:component-scan>
- 注解驱动
作用:替我们自动配置最新版的注解的处理器映射器和处理器适配器。
如果没有显示的配置处理器映射器和处理器适配那么springMvc会去默认的dispatcherServlet.properties中查找, 对应的处理器映射器和处理器适配器去使用,这样每个请求都要扫描一次他的默认配置文件,效率非常低,会降低访问速度,所以要显示的配置处理器映射器和处理器适配器。
<mvc:annotation-driven></mvc:annotation-driven>
- 配置视图解析器
作用:在controller中指定页面路径的时候就不用写页面的完整路径名称了,可以直接写页面去掉扩展名的名称。
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 真正的页面路径 = 前缀 + 去掉后缀名的页面名称 + 后缀 -->
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
3. web.xml的基础配置
<!-- spirngMvc前端控制器 -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 如果没有指定springMvc核心配置文件那么默认会去找/WEB-INF/+<servlet-name>中的内容 + -servlet.xml配置文件 -->
<!-- 指定springMvc核心配置文件位置 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<!-- tomcat启动的时候就加载这个servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
4.搭建SSM架构
1)导入包,相关的包可在这下载:ssm(spring+springmvc+mybatis)整合需要的jar包
然后使用逆向工程生成pojo和映射文件以及接口
2)配置 空的mybatis核心配置文件SqlMapConfig.xml(貌似也可以不用,把它删了也没有出现什么问题)
3)配置ApplicationContext-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean>
<!-- mapper配置 -->
<!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis的全局配置文件 -->
<!-- <property name="configLocation" value="classpath:SqlMapConfig.xml" /> -->
</bean>
<!-- 配置Mapper扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ssm.dao"/>
</bean>
</beans>
4)配置 @Service注解扫描: ApplicationContext-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- @service扫描 -->
<context:component-scan base-package="com.ssm.service"></context:component-scan>
</beans>
5)配置 事务:ApplicationContext-trans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 传播行为 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.ssm.service.*.*(..))" />
</aop:config>
</beans>
6)配置SpringMvc.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- @Controller注解扫描 -->
<context:component-scan base-package="com.ssm.controller"></context:component-scan>
<!-- 注解驱动:
替我们显示的配置了最新版的注解的处理器映射器和处理器适配器 -->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
<!-- 配置视图解析器
作用:在controller中指定页面路径的时候就不用写页面的完整路径名称了,可以直接写页面去掉扩展名的名称
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 真正的页面路径 = 前缀 + 去掉后缀名的页面名称 + 后缀 -->
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置自定义转换器
注意: 一定要将自定义的转换器配置到注解驱动上
-->
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<!-- 指定自定义转换器的全路径名称 -->
<bean class="com.ssm.controller.converter.CustomGlobalStrToDateConverter"/>
</set>
</property>
</bean>
</beans>
7)配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>MySSM</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 加载spring容器 -->
<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>
<!-- springmvc前端控制器 -->
<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:SpringMvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- 配置Post请求乱码 -->
<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>
</web-app>
8)控制层(controller) :
package com.ssm.controller;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.ssm.pojo.Items;
import com.ssm.service.ItemsService;
import com.ssm.vo.QueryVo;
@Controller
public class ItemsController {
@Autowired
private ItemsService itemsService;
@RequestMapping("/list")
public ModelAndView ItemsList() throws Exception{
List<Items> list = itemsService.list();
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemList", list);
modelAndView.setViewName("itemList");
return modelAndView;
}
/**
* springMvc中默认支持的参数类型:也就是说在controller方法中可以加入这些也可以不加, 加不加看自己需不需要,都行.
*HttpServletRequest
*HttpServletResponse
*HttpSession
*Model
*/
@RequestMapping("/itemEdit")
public String ItemsEdit(HttpServletRequest request, HttpServletResponse response,HttpSession session, Model model) throws Exception{
String idStr = request.getParameter("id");
Items items = itemsService.findItemsById(Integer.parseInt(idStr));
//Model模型:模型中放入了返回给页面的数据
//model底层其实就是用的request域来传递数据,但是对request域进行了扩展.
model.addAttribute("item", items);
//如果springMvc方法返回一个简单的string字符串,那么springMvc就会认为这个字符串就是页面的名称
return "editItem";
}
//springMvc可以直接接收基本数据类型,包括string.spirngMvc可以帮你自动进行类型转换.
//controller方法接收的参数的变量名称必须要等于页面上input框的name属性值
//public String update(Integer id, String name, Float price, String detail) throws Exception{
//spirngMvc可以直接接收pojo类型:要求页面上input框的name属性名称必须等于pojo的属性名称
@RequestMapping("/updateitem")
public String update(Items items) throws Exception{
/*items.setCreatetime(new Date());*/
itemsService.updateItems(items);
return "success";
}
//如果Controller中接收的是Vo,那么页面上input框的name属性值要等于vo的属性.属性.属性.....
@RequestMapping("search")
public String search(QueryVo queryVo) throws Exception{
System.out.println(queryVo);
return "";
}
}
其中:在控制层我定义了一个自定义转换类的包(即converter) ,创建了一个CustomGlobalStrToDateConverter类用于将字符串转换成时间,即:
package com.ssm.controller.converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.core.convert.converter.Converter;
/**
* S - source:源
* T - target:目标
*
*/
public class CustomGlobalStrToDateConverter implements Converter<String, Date> {
@Override
public Date convert(String source) {
try {
Date date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(source);
return date;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
9)业务层(service) ,创建了一个service接口和实现类,即:
package com.ssm.service;
import java.util.List;
import com.ssm.pojo.Items;
public interface ItemsService {
public List<Items> list() throws Exception;
public Items findItemsById(Integer id) throws Exception;
public void updateItems(Items items) throws Exception;
}
package com.ssm.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ssm.dao.ItemsMapper;
import com.ssm.pojo.Items;
import com.ssm.pojo.ItemsExample;
@Service
public class ItemsServiceImpl implements ItemsService {
@Autowired
private ItemsMapper itemsMapper;
@Override
public List<Items> list() throws Exception {
//如果不需要任何查询条件,直接将example对象new出来即可
ItemsExample example = new ItemsExample();
List<Items> list = itemsMapper.selectByExampleWithBLOBs(example);
return list;
}
@Override
public Items findItemsById(Integer id) throws Exception {
Items items = itemsMapper.selectByPrimaryKey(id);
return items;
}
@Override
public void updateItems(Items items) throws Exception {
itemsMapper.updateByPrimaryKeyWithBLOBs(items);
}
}
5. 总结1:
ssm整合:
1)Dao层
- pojo和映射文件以及接口使用逆向工程生成
- SqlMapConfig.xml mybatis核心配置文件
- ApplicationContext-dao.xml 整合后spring在dao层的配置:(1)数据源(2)会话工厂(3)扫描Mapper
2)service层
- 事务:ApplicationContext-trans.xml
- @Service注解扫描: ApplicationContext-service.xml
3)controller层
配置SpringMvc.xml,包含:
- 注解扫描:扫描@Controller注解
- 注解驱动:替我们显示的配置了最新版的处理器映射器和处理器适配器
- 视图解析器:显示的配置是为了在controller中不用每个方法都写页面的全路径
4)web.xml
- springMvc前端控制器配置
- spring监听
参数绑定(从请求中接收参数)重点
1)默认类型:
在controller方法中可以有也可以没有,看自己需求随意添加。即:httpservletRqeust,httpServletResponse,httpSession,Model(ModelMap其实就是Mode的一个子类,一般用的不多)
2)基本类型:string,double,float,integer,long.boolean
3)pojo类型:页面上input框的name属性值必须要等于pojo的属性名称
4)vo类型:页面上input框的name属性值必须要等于vo中的属性.属性.属性....
5)自定义转换器converter:
作用:由于springMvc无法将string自动转换成date,所以需要自己手动编写类型转换器。包含以下步骤:
- 需要编写一个类实现Converter接口
- 在springMvc.xml中配置自定义转换器
- 在springMvc.xml中将自定义转换器配置到注解驱动上
6. controller重定向和请求转发
1)重定向:浏览器中url发生改变,request域中的数据不可以带到重定向后的方法中
- model.addAttribute("id", items.getId());
- 在springMvc中凡是以redirect:字符串开头的都为重定向。 即 return "redirect:itemEdit/"+items.getId();
2)请求转发:浏览器中url不发生改变,request域中的数据可以带到转发后的方法中
- model.addAttribute("id", items.getId());
- spirngMvc中请求转发:返回的字符串以forward:开头的都是请求转发, 后面forward:itemEdit.action表示相对路径,相对路径就是相对于当前目录,当前为类上面指定的items目录.在当前目录下可以使用相对路径随意跳转到某个方法中
- 后面forward:/itemEdit.action路径中以斜杠开头的为绝对路径,绝对路径从项目名后面开始算return "forward:/items/itemEdit.action";
7. @RequestMapping:通过RequestMapping注解可以定义不同的处理器映射规则。
1)URL路径映射
@RequestMapping(value="/item")或@RequestMapping("/item)
value的值是数组,可以将多个url映射到同一个方法
2)窄化请求映射
- 在class上添加@RequestMapping(url)指定通用请求前缀, 限制此类下的所有方法请求url必须以请求前缀开头,通过此方法对url进行分类管理。如下:
@RequestMapping放在类名上边,设置请求前缀
@Controller
@RequestMapping("/item")
- 方法名上边设置请求映射url:
@RequestMapping放在方法名上边,如下:
@RequestMapping("/queryItem ")
在class和方法名上加入@RequestMapping后,此时,访问地址变为:/item/queryItem
3)请求方法限定
- 限定GET方法
@RequestMapping(method = RequestMethod.GET)
如果通过Post访问则报错:
HTTP Status 405 - Request method 'POST' not supported
例如:
@RequestMapping(value="/editItem",method=RequestMethod.GET)
- 限定POST方法
@RequestMapping(method = RequestMethod.POST)
如果通过Post访问则报错:
HTTP Status 405 - Request method 'GET' not supported
- GET和POST都可以
@RequestMapping(method={RequestMethod.GET,RequestMethod.POST})
8. 扩展
(1)文件(图片)上传
1)配置虚拟目录
方式一:在tomcat上配置图片虚拟目录,即在tomcat下的conf/server.xml中添加:
<Context docBase="F:\develop\upload\temp" path="/pic" reloadable="false"/>
然后访问http://localhost:8080/pic即可访问F:\develop\upload\temp下的图片。
方式二:通过eclipse配置:
2)导入相关jar包
CommonsMultipartResolver解析器依赖commons-fileupload和commons-io,需在工程中加入如下jar包,可在https://download.csdn.net/download/xudasong123/10597654下载:
3)在SpringMvc.xml配置文件中配置解析器,如下:
<!-- 文件(图片)上传 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置文件上传大小为5M -->
<property name="maxUploadSize">
<value>5242880</value>
</property>
</bean>
4)修改原先ItemsController类的update方法,如下:
@RequestMapping("/updateitem")
public String update(MultipartFile pictureFile,Items items,Model model,HttpServletRequest request) throws Exception{
/*items.setCreatetime(new Date());*/
//1.获得图片完整名称
String fileName=pictureFile.getOriginalFilename();
//2.使用随机生成的字符串+原图片扩展名组成新的图片名称,防止图片重名
String newFileName=UUID.randomUUID().toString()+fileName.substring(fileName.lastIndexOf("."));
//3.将图片存于硬盘
pictureFile.transferTo(new File("E:\\images\\"+newFileName));
//4.将图片名称保存于数据库
items.setPic(newFileName);
itemsService.updateItems(items);
//返回数据
//request.setAttribute("", arg1);
//指定返回的页面(如果controller方法返回值为void,则不走springMvc组件,所以要写页面的完整路径名称)
//request.getRequestDispatcher("/WEB-INF/jsp/success.jsp").forward(request, response);
//重定向:浏览器中url发生改变,request域中的数据不可以带到重定向后的方法中
//model.addAttribute("id", items.getId());
//在springMvc中凡是以redirect:字符串开头的都为重定向
return "redirect:itemEdit.action?id="+items.getId();
//请求转发:浏览器中url不发生改变,request域中的数据可以带到转发后的方法中
//model.addAttribute("id", items.getId());
//spirngMvc中请求转发:返回的字符串以forward:开头的都是请求转发,
//后面forward:itemEdit.action表示相对路径,相对路径就是相对于当前目录,当前为类上面指定的items目录.在当前目录下可以使用相对路径随意跳转到某个方法中
//后面forward:/itemEdit.action路径中以斜杠开头的为绝对路径,绝对路径从项目名后面开始算
//return "forward:/items/itemEdit.action";
}
5)修改editItems.jsp页面
form添加enctype="multipart/form-data":
<form id="itemForm"
action="${pageContext.request.contextPath }/item/editItemSubmit.action"
method="post" enctype="multipart/form-data">
<input type="hidden" name="pic" value="${item.pic }" />
file的name与controller形参一致:
<tr>
<td>商品图片</td>
<td><c:if test="${item.pic !=null}">
<img src="/pic/${item.pic}" width=100 height=100 />
<br />
</c:if> <input type="file" name="pictureFile" /></td>
</tr>
(2)json数据交互
首先需要了解两个注解:
一是:@RequestBody
作用:@RequestBody注解用于读取http请求的内容(字符串),通过springmvc提供的HttpMessageConverter接口将读到的内容转换为json、xml等格式的数据并绑定到controller方法的参数上。
本例子应用:@RequestBody注解实现接收http请求的json数据,将json数据转换为java对象
二是:@ResponseBody
作用:该注解用于将Controller的方法返回的对象,通过HttpMessageConverter接口转换为指定格式的数据如:json,xml等,通过Response响应给客户端
本例子应用:@ResponseBody注解实现将controller方法返回对象转换为json响应给客户端
下面说说请求json,响应json实现:
1)导入相关jar包
Springmvc默认用MappingJacksonHttpMessageConverter对json数据进行转换,需要加入jackson的包,可在这里下载:https://download.csdn.net/download/xudasong123/10597654,如下:
2)因为我们使用了注解驱动<mvc:annotation-driven /> ,所以不需要再配置文件里配置messageConverters
3)编写controller
//导入jackson的jar包在 controller的方法中可以使用@RequestBody,让spirngMvc将json格式字符串自动转换成java中的pojo
//页面json的key要等于java中pojo的属性名称
//controller方法返回pojo类型的对象并且用@ResponseBody注解,springMvc会自动将pojo对象转换成json格式字符串
@RequestMapping("/sendJson")
@ResponseBody
public Items json(@RequestBody Items items) {
System.out.println(items);
return items;
}
(3)RESTFUL支持
Restful就是一个资源定位及资源操作的风格。不是标准也不是协议,只是一种风格,是对http协议的诠释。
资源定位:互联网所有的事物都是资源,要求url中没有动词,只有名词。没有参数
Url格式:http://blog.csdn.net/beat_the_world/article/details/45621673
资源操作:使用put、delete、post、get,使用不同方法对资源进行操作。分别对应添加、删除、修改、查询。一般使用时还是post和get。Put和Delete几乎不使用。
应用RESTFUL需要对controller进行一些修改,如下:
/*通过@PathVariable可以接收url中传入的参数
*@RequestMapping("/itemEdit/{id}")中接收参数使用大括号中加上变量名称, @PathVariable中的变量名称要和RequestMapping
*中的变量名称相同
*/
@RequestMapping("/itemEdit/{id}")
public String itemEdit(@PathVariable("id") Integer id, HttpServletRequest reuqest,
Model model) throws Exception{
//String idStr = reuqest.getParameter("id");
Items items = itmesService.findItemsById(id);
//Model模型:模型中放入了返回给页面的数据
//model底层其实就是用的request域来传递数据,但是对request域进行了扩展.
model.addAttribute("item", items);
//如果springMvc方法返回一个简单的string字符串,那么springMvc就会认为这个字符串就是页面的名称
return "editItem";
}
(4)配置拦截器
Spring Web MVC 的处理器拦截器类似于Servlet 开发中的过滤器Filter,用于对处理器进行预处理和后处理。
1)拦截器定义
实现HandlerInterceptor接口,如下:
Public class HandlerInterceptor1 implements HandlerInterceptor{
/**
* controller执行前调用此方法
* 返回true表示继续执行,返回false中止执行
* 这里可以加入登录校验、权限拦截等
*/
@Override
Public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
// TODO Auto-generated method stub
Return false;
}
/**
* controller执行后但未返回视图前调用此方法
* 这里可在返回用户前对模型数据进行加工处理,比如这里加入公用信息以便页面显示
*/
@Override
Public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
}
/**
* controller执行后且视图返回后调用此方法
* 这里可得到执行controller时的异常信息
* 这里可记录操作日志,资源清理等
*/
@Override
Public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
}
}
2)在配置文件SpringMvc.xml增加拦截器配置
<!-- 配置拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<!-- 拦截请求的路径,要拦截所有必须配置成/** -->
<mvc:mapping path="/**"/>
<!-- 指定拦截器的位置 -->
<bean class="com.ssm.interceptor.Interceptor1"></bean>
</mvc:interceptor>
</mvc:interceptors>