一:新建工程: 选择Spring,Web Application, Struts2, Application Server(选择tomcat就可以了),Hibernate;等待IDEA下载依赖包
二:spring的引入与测试:
测试的包:src下面新建test包
TestService.java
package test; public class TestService { private String name; public void setName(String name) { this.name = name; } public String getName() { return name; } public void hello() { System.out.println("hello "+ getName()); } }
第一个配置文件(applicationContext.xml)web/Web-INF目录下新建spring的配置文件applicationContext.xml文件
Test.java
package test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext ac = new FileSystemXmlApplicationContext("web/WEB-INF/applicationContext.xml"); TestService ts = (TestService)ac.getBean("testService"); ts.hello(); }
直接测试Test的main函数,操作是右击编辑区,点击Run "Test.main()",正常的话,应该会出现如下效果:
三:Hibernate的引入与测试:
1>要额外引入一些依赖:mysql-connector-java-5.1.26,commons-dbcp2-2.1,commons-pool2-2.4.1;直接使用IDEA的maven功能下载依赖包,然后直接引入:File->project Structure->Libraries->点击右侧的加号->From maven; 然后输入对应的包名就可以了;
2>使用IDEA的persistence下的OR映射功能; (a)建立数据库
(b)需要先配置applicationContext.xml,让其接管Hibernate的配置;如果没有进行这一步的话,在Persistence界面是不会出现sessionFactory的。
配置内容:
org.hibernate.dialect.MySQLDialect true jdbc:mysql://localhost:3306/TESTDB com.mysql.jdbc.Driver
(c)进行持久化类生成:右击sessionFactory->Generate Persistence Mapping
(d)新建dao层和daoImpl层:
package dao public interface UserDao { public void addUser(User user); }
sessionFactory注入dao层
package dao; import bean.User; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; public class UserDaoImpl implements UserDao { @Qualifier("sessionFactory") @Autowired private SessionFactory sessionFactory; @Override public void addUser(User user) { Session s = sessionFactory.openSession(); Transaction tx= s.beginTransaction(); s.save(user); tx.commit(); } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } }
(e)新建service层和serviceImpl层:
package service public interface UserService{ public void addUser(User user); }
dao层注入service层:
package service; public class UserServiceImpl implements UserService{ private UserDao userDao; public void setUserDao(UserDao userDao){ this.userDao = userDao; } @override public void addUser(User user){ userDao.addUser(user); } }
让Spring接管Hibernate的配置文件:
org.hibernate.dialect.MySQLDialect true jdbc:mysql://localhost:3306/user com.mysql.jdbc.Driver
classpath:bean/User.hbm.xml
进行测试:Test.java
public class Test { public static void main(String[] args) { ApplicationContext ac = new FileSystemXmlApplicationContext("web/WEB-INF/applicationContext.xml"); UserDao userDao = (UserDaoImpl)ac.getBean("userDao"); User user = new User(); user.setName("kaka"); user.setPhone("123456123"); userDao.addUser(user); } }
查看MySQL数据库,看是否增加了一条记录
三:struts2的引入与测试:
引入两个依赖:struts2-spring-plugin-2.3.24,spring-web-3.0.5和log4j-1.2.14.jar;选择手工加入;先如下下载好,"Download to "不要打勾:
下载struts2-spring-plugin-2.3.24会附带下载好spring-web-3.0.5; 下载完后查看下下载的目录, 只是查看下下载目录就可以了如下:
直接复制这两个包,放到项目的lib文件下,之后右击一下lib下的刚引入的这两个jar包,"Add as Library";对于log4j-1.2.14就相对简单,直接打勾Download to 就可以了.相关截图如下:
第三个配置文件:修改web/WEB-INF/web.xml文件,引入struts和spring:
struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /* org.springframework.web.context.ContextLoaderListener contextConfigLocation WEB-INF/applicationContext.xml
新建Action在struts.xml中配置:
第四个配置文件:struts.xml:
/success.jsp
最后!!!
所有类的实例都由spring去管理(applicationContext.xml)
配置流程详见:http://blog.csdn.net/sysushui/article/details/68937005
请求传递过程分析详见:http://www.cnblogs.com/laibin/p/5847111.html