1. 程式人生 > >3 Spring框架整合WEB 1(與struts2整合)

3 Spring框架整合WEB 1(與struts2整合)

  • 使用spring與struts2整合

web.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>springday01_crm</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>

	<!-- spring的webfilter,用來啟動servlet時載入applicationContext.xml -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>



	<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>
</web-app>

service與dao

package service;

import dao.UserDao;
import dao.UserDaoImpl;

public class UserServiceImpl implements UserService{
	//使用依賴注入來獲得udao,必須提供setter方法
	private UserDaoImpl udao;
	public void setUdao(UserDaoImpl udao) {
		this.udao = udao;
	}

	@Override
	public void save() {
		System.out.println("service層 save");
		udao.save();
	}

}
package dao;

public class UserDaoImpl implements UserDao {
	//dao層方法
	@Override
	public void save() {
		System.out.println("Dao層 save");
	}

}

測試類

package action;


import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.opensymphony.xwork2.ActionSupport;

import service.UserService;
import service.UserServiceImpl;

public class UserAction extends ActionSupport{
	//測試struts環境方法
	public String demo1() {
		System.out.println("running " + this.getClass().getName() + "." + new Exception().getStackTrace()[0].getMethodName());
		return SUCCESS;
	}
	
	//跳轉add頁面
	public String addUI() {
		return "addUI";
	}
	
	
	/*
	 * #使用struts2呼叫service層的方法
	public String add() {
//		System.out.println("running " + this.getClass().getName() + "." + new Exception().getStackTrace()[0].getMethodName());
		System.out.println("web層 save");
		UserService us = new UserServiceImpl();
		us.save();
		return NONE;
	}
		 */
	
	
	/*
	 * 使用spring工廠的模式來呼叫已交給spring管理的bean
	 * 注意:這樣可以實現,但是每次呼叫此方法都會載入一次配置檔案新建立一個ApplicationContext,這不是我們想要的
	 * 解決方法:
	 * 匯入spring核心包中的web jar包用WebApplicationContext解決此問題
	 * /
	 */
//	public String add() {
//		
//		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//		UserService us = (UserService) ac.getBean("userService");
//		us.save();
//		return NONE;
//	}
	
	
	/*
	 * 0. 解決傳送每訪問一次都會載入一次配置檔案的問題
	 * 1.引入spring-web-4.2.4.RELEASE.jar包
	 * 2 .配置監聽器
	 * 3.從ServletContext中獲得工廠繼續業務
	 */
	public String add() {
		//獲得servletContext
		ServletContext servletContext = ServletActionContext.getServletContext();
		//獲得WebApplicationContext需要傳入一個servletContext
		WebApplicationContext wContext = 
				WebApplicationContextUtils.getWebApplicationContext(servletContext);
		//獲得userService
		UserService us = (UserService) wContext.getBean("userService");
		//繼續業務
		us.save();
		return NONE;
	}
	
}

每次都載入配置檔案的結果

使用spring web包之後的效果

web包的原理:

* 將工廠建立好了以後放入到ServletContext域中.使用工廠的時候,從ServletContext中獲得.
* ServletContextListener:用來監聽ServletContext物件的建立和銷燬的監聽器.
* 當ServletContext物件建立的時候:建立工廠 , 將工廠存入到ServletContext