spring 学习之三(spring 与hibernate, struts2整合)
如果不会struts环境搭建的可以参考《Strust2学习之一(struts2 环境搭建)》
如果不会spring和 hibernate整合的可以参考《
Spring 学习之二(Spring 和 hibernate 整合) 》
这里就直接开始讲解spring 与struts2的整合:
第一步:在web.xml中加入: <!-- 在web容器中实例化spring容器, --> <!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 对Spring容器进行实例化 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
第二步: 在spring中配置文件中<bean id="studentDAO" class="com.wfg.dao.test.StudentDAO"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="studentAction" class="com.wfg.action.test.StudentAction"> <property name="studentDAO" ref="studentDAO"/> </bean>
现在就可以在jsp中用
<a href="${pageContext.request.contextPath }/studentAction.action">student test </a> 进行访问测试
当然studentaction中的studentdao不需要在new了此时
public class StudentAction { private StudentDAO studentDAO; public StudentDAO getStudentDAO() { return studentDAO; } public void setStudentDAO(StudentDAO studentDAO) { this.studentDAO = studentDAO; } public String execute(){ System.out.println(studentDAO); //List list = studentDAO.findAll(); //System.out.println(list); return "dd"; } }
这里就直接开始讲解