SSH三个框架的知识点
一、Hibernate框架
1. Hibernate的核心配置文件
1.1 数据库信息、连接池配置
1.2 Hibernate信息
1.3 映射配置
1.4 Hibernate核心配置文件
如果单纯使用Hibernate框架,核心配置文件名称hibernate.cfg.xml并且一定要放在src下面,而hibernate和spring整合的时候,hibernate核心配置文件名称和位置没有固定要求的(因为到时候会有一个参数指定其位置)。
2. Hibernate映射配置文件
2.1 实体类和数据库表映射关系:使用的是ORM思想
3. hibernate操作的步骤
3.1 在Spring框架对hibernate框架进行封装,使用HibernateTemplate类
二、Struts2框架
1. Action相关的操作
1.1 action创建三种方式:
写一个POJO,并在其中写上 public String execute();方法
创建一个类并实现Action接口
使用的最多的方法是集成类ActionSupport
1.2 配置action访问路径
创建struts.xml配置文件,这个文件名称和位置固定src下面
1.3 配置访问action的多个方法
使用通配符的方式配置比较常见
1.4 在action获取表单提交数据
获取request对象,Struts2提供的API有ActionContext和ServletActionContext
属性封装
模型驱动,要实现ModelDriven接口
1.5 在action中操作域对象
使用ServletActionContext获取域对象
1.6 在web.xml中配置Struts提供的过滤器
2. 值栈
值栈在开发中用得不多,掌握两点:
2.1 向值栈中放数据
set方法
push方法
定义变量生成get方法
2.2 从值栈中获取数据
在jsp中使用struts2标签+ognl获取
1 <s:property> 2 3 <s:iterator>
3. 拦截器
3.1 AOP和责任链
3.2 自定义拦截器
自定义拦截器可以通过继承MethodFilerInterceptor创建
三、Spring框架
1. Spring框架核心配置文件
1.1 名称和位置没有固定要求,官方推荐使用applicationContext.xml作为配置文件名
1.2 在Spring核心配置文件中引入Schema约束
2. 创建对象
2.1 xml配置方式:<bean id="" class="" />
2.2 注解方式:四个注解,Component,Service,Controller,Repository
3. 注入属性
3.1 xml配置方式
3.2 注解方式:两个直接,Resource,Autowired
4. 使用ServletContext对象和监听器实现
4.1 在服务器启动的时候,加载Spring,监听器的配置在web.xml中
4.2 配置Spring的监听器
4.3 指定Spring配置文件的位置
4.4 要导入一个Spring整合web项目的jar包
5. AOP的思想以及JdbcTemplate的使用
四、SSH三大框架的整合思想
1. web应用的三层为:
1.1 web层,(struts2),Struts2框架用的最多的是action
1.2 service层(spring),spring中用的最多的是IoC和AOP,把对象的创建交给Spring进行管理
1.3 dao层(hibernate),hibernate则是用来操作数据库,进行CRUD
2. 哪么这三个框架应该是如何整合呢?
思想是两两整合:
2.1 struts2和Spring进行整合
2.1.1 在struts中action的创建交给Spring进行创建,但是要注意action是多实例的。
2.1.2 要注意导入spring整合Struts2的jar包
2.2 hibernate和Spring进行整合
2.2.1 hibernate中的核心类是SessionFactory,这里要把SessionFactory的创建交给Spring进行管理
2.2.2 Hibernate的核心文件中进行了数据库信息的配置,这里也要交给Spring进行处理
2.2.3 为Dao对象配置持久层的Spring提供的Template
2.2.4 注意导入Spring整合DAO层的ORM包
五、Struts2和Spring整合的具体步骤
1. 把Struts2的action交给Spring进行管理
2. 实现过程
2.1 导入用于整合的jar包
图5-1 Spring单独使用需要导入的jar包
2.2 Spring为了整合Struts还需要额外再导入一个jar包:
图5-2 Spring整合Struts2所需的jar包
2.3 导入Struts2的jar包(struts2版本为2.3.24):
图5-3 Struts2中所需的最少jar包
其实图5-3中的jar包就是在Struts2项目解压之后的apps中有一个blank项目,将其中的jar包导入进来。
3. 创建Action
1 package com.ssh.domain; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 public class UserAction extends ActionSupport { 6 7 @Override 8 public String execute() throws Exception { 9 10 return NONE; // 表示不返回到任何页面中去 11 } 12 13 }
4. 创建Strut2的核心配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <!DOCTYPE struts PUBLIC 4 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 5 "http://struts.apache.org/dtds/struts-2.3.dtd"> 6 7 <struts> 8 9 <package name="demo1" extends="struts-default" namespace="/"> 10 11 <action name="userAction" class="com.ssh.domain.UserAction"> 12 13 </action> 14 15 </package> 16 17 </struts>
位置在src下面,名称是struts.xml
5. 在web.xml中配置struts2的过滤器
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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_2_5.xsd" id="WebApp_ID" version="2.5"> 3 <display-name>spring_struts2</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 13 <filter> 14 <filter-name>struts2</filter-name> 15 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 16 </filter> 17 18 <filter-mapping> 19 <filter-name>struts2</filter-name> 20 <url-pattern>/*</url-pattern> 21 </filter-mapping> 22 </web-app>
至此,上面四步已经将Struts2的环境配置好了,然后就是来配置Spring了。
6. 导入Spring整合Web项目的jar包,也就是监控项目启动的监听器所在的jar包。
图5-4 Spring整合web项目需要的jar包
7. 创建Spring的核心配置文件并在其中引入约束
这个约束配置的比较多,可以直接拿过来使用的。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context.xsd 11 http://www.springframework.org/schema/aop 12 http://www.springframework.org/schema/aop/spring-aop.xsd 13 http://www.springframework.org/schema/tx 14 http://www.springframework.org/schema/tx/spring-tx.xsd"> 15 16 </beans>