spring 學習之三(spring 與hibernate, struts2整合)

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";
	}
}










這裏就直接開始講解

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章