Spring - 3 Spring框架整合WEB 1(与struts2整合)

124 篇文章 1 订阅
17 篇文章 0 订阅
  • 使用spring与struts2整合

  • 需要导入spring-web-4.2.4.RELEASE.jar

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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值