准备工作:
1.新建一个动态web项目(不要xml文件,使用全注解)
2.往lib加东西(注意别重复)
Strust2最小包+3个包(14个)、Hibernate的包、Spring的包、还有ojdbc6.jar
3.resource 资源文件夹
jdbc.properties、log4j.propertiesa、struts.xml
第一步:让Struts2和Spring相结合
1.需要一个全注解的配置类 WebInitialize.java 用来代替web.xml
package com.zhaoming.config; import javax.servlet.FilterRegistration.Dynamic; import javax.servlet.ServletContext; import javax.servlet.ServletException; import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter; import org.springframework.orm.hibernate4.support.OpenSessionInViewFilter; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.ContextLoaderListener; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; public class WebInitializer implements WebApplicationInitializer { public void onStartup(ServletContext servletContext) throws ServletException { servletContext.addFilter("hibernateFilter", OpenSessionInViewFilter.class).addMappingForUrlPatterns(null, false, "/*"); AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); root.register(DataSourceConfig.class); //servletContext.setInitParameter("contextConfigLocation", "classpath:applicationContext.xml"); servletContext.addListener(new ContextLoaderListener(root)); Dynamic filter=servletContext.addFilter("struts2", new StrutsPrepareAndExecuteFilter()); filter.addMappingForUrlPatterns(null,false,"/*"); } }
2.新建index.jsp,一个单独的转向到com.zhaoming.action ——>InitAction,成功返回login.jsp,测试成功。
第二步:把 hibernate和Spring结合在一起
创建一个与hibernate相关的DataSourceConfig配置类,主要工作:
(1)连接数据库
(2)获取sessionFactory
(3)事务控制
package com.zhaoming.config; import java.util.Properties; import javax.annotation.Resource; import org.hibernate.SessionFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; import org.springframework.orm.hibernate4.HibernateTransactionManager; import org.springframework.orm.hibernate4.LocalSessionFactoryBean; import org.springframework.transaction.annotation.EnableTransactionManagement; import com.alibaba.druid.pool.DruidDataSource; @Configuration @ComponentScan("com.zhaoming") @EnableTransactionManagement //开启事务控制 @PropertySource("classpath:jdbc.properties") public class DataSourceConfig { @Resource Environment env; @Resource DruidDataSource dataSource; @Resource SessionFactory sessionFactory; @Bean(name="dataSource") public DruidDataSource getDruidDataSource(){ DruidDataSource dds = new DruidDataSource(); dds.setUrl(env.getProperty("jdbc.url")); dds.setUsername(env.getProperty("jdbc.username")); dds.setPassword(env.getProperty("jdbc.password")); dds.setDriverClassName(env.getProperty("jdbc.class_name")); return dds; } @Bean(name="sessionFactory") public LocalSessionFactoryBean getLocalSessionFactoryBean(){ LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean(); lsfb.setDataSource(dataSource); lsfb.setPackagesToScan("com.zhaoming"); Properties prop = new Properties(); prop.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle9Dialect"); prop.setProperty("hibernate.current_session_context_class", "org.springframework.orm.hibernate4.SpringSessionContext"); prop.setProperty("show_sql","true"); lsfb.setHibernateProperties(prop); return lsfb; } @Bean(name="transactionManager") public HibernateTransactionManager getHibernateTransactionManager(){ HibernateTransactionManager htm = new HibernateTransactionManager(); htm.setSessionFactory(sessionFactory); return htm; } }
测试是否能获取到session
第三步:根据自己的需要去写Dao、Service,贯穿整个项目。
1.通过MyEclipse建一个User实体类
2.UserDaoIface
3.UserDaoImp —— 测试
4.ServiceIface
5.ServiceImp —— 测试
6.jsp
7.相对应的Action —— 测试
至此,一个简易的SSH框架就完成了。
时间: 2024-10-04 13:56:12