本文讲两个方面:
一、单独spring框架的使用
- .创建项目
- .导入spring 相关架包:详见
- .编写项目类文件
- .编写spring 配置文件
- .解析spring配置文件
ApplicationContext ac=new ClassPathXmlApplicationContext();
- .使用spring获取bean
// 根据 配置的 name 或 id 取对象
User user=ac.getBean("user");
// 根据 类型取对象 User user1=ac.getBean(User.class);
二、ssh三大框架的整合
- .创建web项目
- .在项目的lib目录下加入相关架包:如下
- .添加spring支持、添加hibernate支持、添加struts2支持(不再加入架包,只为创建相关配置文件)
- .反向生成hibernate映射文件
- .整合hibernate配置文件
- .编写dao层 并整合dao层
- .编写biz 层 并整合biz层
- .编写action
- .编写struts.xml配置 , 和单用struts没区别
- .编写web.xml
<!-- 1.添加 监听器 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 2.添加初始化参数 contextConfigLocation 指定spring的配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-core.xml</param-value> <!-- <param-value>classpath:applicationContext-*.xml</param-value> --> <!-- <param-value>classpath:applicationContext-core.xml,classpath:applicationContext-biz.xml,classpath:applicationContext-dao.xml</param-value> --> </context-param> <!-- 3.配置OpenSessionInViewFilter --> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>