搭建ssh框架相关配置文件总结

第一步 导入jar包

 

第二步 搭建struts2环境

1)创建action(继承actionnsuport),创建struts.xml配置文件,配置action

public class UserAction extends ActionSupport{}

(2)配置struts2的过滤器,在web.xml中

        
struts2
        
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  
  
        
struts2
        
/*
  

第三步 搭建hibernate环境

(1)创建实体类,User

public class User {	private Integer uid;	private String username;	private String password;	private String email;	private String code;	private Boolean state;	//再生成set/get方法}

实体类编写规则

1 实体类里面属性私有的

 

2 私有属性使用公开的setget方法操作

 

3 要求实体类有属性作为唯一值(一般使用id值)

 

4 实体类属性建议不使用基本数据类型,使用基本数据类型对应的包装类

1)八个基本数据类型对应的包装类

- int  Integer

- charCharacter

- 其他的都是首字母大写 比如 double  Double

2)比如 表示学生的分数,假如 int score;

- 比如学生得了0分 ,int score = 0;

- 如果表示学生没有参加考试,int score = 0;不能准确表示学生是否参加考试

解决:使用包装类可以了, Integer score = 0,表示学生得了0分,

表示学生没有参加考试,Integer score = null;

(2)配置实体类和数据库表映射关系,User.hbm.xml

(3)创建hibernate核心配置文件,hibernate.cfg.xml

- 引入映射配置文件

(配置文件需要引入约束)

true
true
update
org.hibernate.dialect.MySQLDialect

第四步 搭建spring环境

(1)创建spring核心配置文件

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: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">

(2)让spring配置文件在服务器启动时候加载,在web.xml中

- 配置监听器

- 指定spring配置文件位置

  
contextConfigLocation
  
classpath:bean.xml
  
      ......  
    
org.springframework.web.context.ContextLoaderListener
    

第五步 struts2和spring整合

(1)把action在spring配置(action多实例的)

(2)在struts.xml中action标签class属性里面写 bean的id值

bean.xml
struts.xml

 

第六步 spring和hibernate整合

(1)把hibernate核心配置文件中数据库配置,在spring里面配置

(2)把hibernate的sessionFactory在spring配置

第七步 完成互相注入关系(在dao里面使用hibernateTemplate)

(1)在dao注入hibernateTemplate对象

(2)在hibernateTemplate对象中注入sessionFactory

public class UserAction extends ActionSupport{	private UserService userService;	public void setUserService(UserService userService) {		this.userService = userService;	}}
@Transactionalpublic class UserService {	private UserDao userDao;	public void setUserDao(UserDao userDao) {		this.userDao = userDao;	}}/**问题:如果在service类上面没有添加注解,出现异常问题:只读模式下(FlushMode.NEVER/MANUAL)写操作不被允许:把你的Session改成FlushMode.COMMIT/AUTO或者清除事务定义中的readOnly标记。 错误原因:          OpenSessionInViewFilter在getSession的时候,会把获取回来的session的flush mode 设为FlushMode.NEVER。          然后把该sessionFactory绑定到TransactionSynchronizationManager,使request的整个过程都使用同一个session,在请求过后再接除该sessionFactory的绑定,最后closeSessionIfNecessary根据该session是否已和transaction绑定来决定是否关闭session。在这个过程中,若HibernateTemplate 发现自当前session有不是readOnly的transaction,就会获取到FlushMode.AUTO Session,使方法拥有写权限。也即是,如果有不是readOnly的transaction就可以由Flush.NEVER转为Flush.AUTO,拥有insert,update,delete操作权限,如果没有transaction,并且没有另外人为地设flush model的话,则doFilter的整个过程都是Flush.NEVER。所以受transaction(声明式的事务)保护的方法有写权限,没受保护的则没有。*/
public interface UserDao {}
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {}

最后结构图:

测试:启动服务器(我试过tomcat和resin),如果没有报错,并且数据库中存在数据表,则说明成功。

注意:数据库必须有,表可以没有;log4j.properties是一个可以显示详细日志信息的配置文件。

补:

在web.xml中在配置如下,原因是防止"no session"问题

  
openSessionInViewFilter
  
org.springframework.orm.hibernate5.support.OpenSessionInViewFilter
  
     
  
openSessionInViewFilter
  
/*