MyElicpse整合SS2H
1.首先加入struts2的jar包到web-inf的lib目录下,同事copy配置文件到src目录下
2.在web.xml里面加入filter
<filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping>
3.然后在编写action里面的内容,和struts.xml里面的内容
<package name="mytest" namespace="/" extends="struts-default"><action name="login" name="code"><!-- 指明 Spring 配置文件在何处 --><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/applicationContext*.xml</param-value></context-param><!-- 定义 Spring 的上下文监听器,它会负责初始化 ApplicationContext --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
6.spring和struts2整合需要加入一个jar包 struts2-spring-plugin-2.0.11.2
7.复制一个applicationContext.xml 取名为applicationContext_action.xml,这里的class就是刚才编写的action类
因为struts2是每个请求对应一个实例,所以要加入 scope="prototype"
<bean id="LoginAction" scope="prototype"><property name="userSerivce"><ref bean="userService"/></property></bean>
8.myeclipse --> add hibernate Capabilities
把四个jar包都选上,然后统一放到/WebRoot/WEB-INF/lib底下
然后和spring整合,选择已经存在的applicationContext.xml文件 然后给sessionFactory起名字,然后给dataSource起名字
不用创建sessionFactory class --》finish
然后添加两个连接池的jar包
9.applicationContext.xml最终如下所述
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="dataSource"value="root"></property><property name="password" value="root"></property></bean><bean id="sessionFactory"/></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="hibernate.hbm2ddl.auto">update</prop><prop key="hibernate.show_sql">true</prop><prop key="hibernate.format_sql">true</prop></props></property><property name="mappingResources"><list><value>org/oneedu/spring/web/pojo/TbUser.hbm.xml</value></list></property></bean><bean id="hibernateTemplate"/></property></bean></beans>
10.因为hibernate和spring有一个JAR包冲突 ---》asm2.3.3 把它删除掉
11.然后在对应的DAO SERVICE层里面写上对应的getset方法就可以了