Spring总结(二)

Spring整合Struts2

1.假设前端页面已经完成如下图,所需jar包如下,对menu.jsp中的新增客户进行保存操作

2.调用action

3.web.xml中配置struts2拦截器

4.src文件目录下配置struts2.xml,创建log4j.properties日志配置文件

5.新建web层action包,创建CustomerAction类

package com.sie.web.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 com.sie.service.CustomerService;





public class CustomerAction extends ActionSupport{
	private static final long serialVersionUID = 113695314694166436L;
	/*
	 * 保存客户
	 */
	public String save(){
		System.out.println("WEB层,保存客户>>>");
		//方式1:使用工厂
//		ApplicationContext ap = new ClassPathXmlApplicationContext("applicationContext.xml");
//		CustomerService customerService = (CustomerService) ap.getBean("customerService");
//		customerService.save();
		
		//方式2:使用web工厂方式
		ServletContext sc = ServletActionContext.getServletContext();
		WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(sc);
		CustomerService customerService = (CustomerService) context.getBean("customerService");
		customerService.save();
		return NONE;
	}

}

6.新建业务层,创建CustomerService接口,实现这个接口的CustomerServiceImpl类

package com.sie.service;

import com.sie.dao.CustomerDao;



public class CustomerServiceImpl implements CustomerService{
	private CustomerDao customerDao;
	
	public void setCustomerDao(CustomerDao customerDao) {
		this.customerDao = customerDao;
	}

	@Override
	public void save() {
		// TODO Auto-generated method stub
		System.out.println("Service层,保存客户信息>>>");
		this.customerDao.save();
	}

}

7.新建持久层,创建CustomerDao接口,实现这个接口的CustomerDaoImpl类

package com.sie.dao;

public class CustomerDaoImpl implements CustomerDao{

	@Override
	public void save() {
		// TODO Auto-generated method stub
		System.out.println("这是持久层,保存客户信息>>>");
	}

}

8.新建ApplicationContext.xml,Spring核心配置文件

9.启动服务进行测试

问题:用方式1的时候,在我们每次执行保存的时候,程序都会去加载配置文件创建工厂,这样会造成系统很大的开销和占用内存。

解决方法:使用web工厂方式,在系统启动的时候只加载一次spring配置文件

步骤一:向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>springCase</display-name>
    <!-- 配置WEB整合的监听器 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 加载方式:默认只能加载WEB-INF目录下的配置文件,提供配置方式,加载src目录下 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <!-- struts2基本配置 -->
  <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>
  
  <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>
</web-app>

步骤二:CustomerAction中使用方式2

ServletContext详情链接:

https://www.cnblogs.com/taiguyiba/p/6130293.html

WebApplicationContext的作用,转载https://blog.csdn.net/a925763055/article/details/51455764

1. 它允许从相对于Web根目录的路径中加载配置文件完成初始化工作。从WebApplicationContext中可以获取ServletContext引用,整个Web应用上下文对象将作为属性放置在ServletContext中,以便Web应用环境可以访问Spring上下文。

2.WebApplicationContext还为Bean提供了三个新的作用域,request、session和globalsession。 其中两个参数HttpServletRequest:服务器从客户端拿去数据 ,HttpServletResponse:服务器向前台传送数据

步骤三:测试结果如下,解决了重复加载xml文件的问题。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值