spring环境搭建以及和struts整合

1、首先导入spring所需要的包

2、web.xml中添加spring的监听器以及spring配置文件所在位置

<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring-servlet.xml</param-value>
	</context-param>

3、编写业务逻辑组件,建立FirstService接口和实现类FirstServiceImpl类

public interface FirstService {
	boolean valid(String username);
}
public class FirstServiceImpl implements FirstService {
	public boolean valid(String username) {
		if (username.equals("kevin")) {
			System.out.println("spring");
			return true;
		} else {
			return false;
		}
	}
}

LoginAction中做一点小改动,用业务逻辑组件来进行判断,同时引入业务逻辑接口,在后面的spring配置文件中依赖注入

public class LoginAction extends ExampleSupport {
	private FirstService fs;

	public void setFs(FirstService fs) {
		this.fs = fs;
	}

	@Override
	public String execute() throws Exception {
		<del>if ("kevin".equals(getUsername())) {</del>
		if(fs.valid(getUsername())){

4、spring配置文件命名为spring-servlet.xml,放在web-inf下,(这里我命名为其他名字一直报错,web.xml里位置、名字也是对应的,但还是报错,也不清楚什么原因,只好命名回spring-servlet.xml)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

	<bean id="firstService" class="com.demo.service.FirstServiceImpl" />
	<bean id="loginAction" class="com.demo.action.LoginAction"
		scope="prototype">
		<property name="fs" ref="firstService" />
	</bean>

</beans>

配置文件里添加了业务逻辑组件的bean:firstService以及action的bean,将action的实例化交给spring的IOC容器来管理,并在action的bean中依赖注入业务逻辑组件

注意:这里loginAction的bean的scope为prototype,保证每次请求都会创建一个新的action来处理,避免了action的线程安全问题,因为spring默认scope是单例模式,这样只会创建一个action,而每次访问都是访问同一个action,数据不安全,struts2要求的是每次访问都是不同的action实例

5、最后还要修改一下struts配置文件

先在配置文件顶部加上

<!-- 将action托管给spring -->
	<constant name="struts.objectFactory" value="spring" />

然后把loginAction的配置改一下,class指向spring配置文件里的bean的ID

<action name="Login" class="loginAction"

6、测试

页面上输入用户名密码,验证正确后进入欢迎页面,并且后台也输出了spring字样,说明spring环境搭建成功,同时和struts2也整合成功

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-04 02:39:52

spring环境搭建以及和struts整合的相关文章

Spring环境搭建之:导入jar包、配置文件名称及放置位置

Spring环境搭建之:导入jar包.配置文件名称及放置位置 现在项目开发中spring框架应用的还是比较多的,自己用的还不太熟练,每次用的时候总配置半天,总有些配置弄错,就找个时间总结以下,方便以后再搭建环境的时候直接拿来用. 以Spring4为例,这里就不提供Spring的下载地址了,官网上很容易下的到. 1.导入相关jar包 建好web项目以后,导入Spring自己的jar包 spring-beans-4.0.0.M2.jar spring-context-4.0.0.M2.jar spr

【Spring环境搭建】在Myeclipse下搭建Spring环境-web开发

<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.s

JAVA web环境搭建(使用Tomcat8整合httpd)

说明:这里是Linux服务综合搭建文章的一部分,本文可以作为单独搭建Tomcat并整合httpd的参考.如果需要查看相关软件版本和主机配置要求,请根据目录自行查看. Linux服务综合搭建的文章目录 8.rhel7 JAVA web环境搭建(使用Tomcat8整合httpd) 8.1 单独安装Tomcat(很简单) 8.1.1 准备httpd和网站目录 1 [[email protected] ~]# yum install httpd 2 3 [[email protected] ~]# sy

spring环境搭建

在做ssh环境搭建时,先搭建spring和hibernate的环境搭建,在搭建struts环境. 好处:在搭建struts环境之前出的错和web容器无关. 一.引入配置文件 1:从已有的配置文件复制过来 2:通过xml模板新建配置文件: a:首先进行applicationContext.xml文件提示配置(下次再配置可省略此步骤): Window----preferences----输入xml----选择xml Catalog----add配置 b:右键new---XML(Basic Templ

OA项目2:环境搭建之ssh框架整合

首注:本学习教程为传智播客汤阳光讲师所公布的免费OA项目视频的文字版,本人用此来加强巩固自己开发知识,如有网友转载,请注明.谢谢. 今天做Spring+Hibernate+Struts2整合. 一 Struts2与Spring的整合. 1.整合之前Struts2例子: 在src下建立一个包,包名为:cn.clear.oa.test,在包下面建立一个类TestAction.java,内容如下: 1 package cn.clear.oa.test; 2 3 import com.opensymph

Spring 环境搭建

一.需要的Jar包 二.web.xml 配置文件的修改 spring有三种启动方式,使用ContextLoaderServlet,ContextLoaderListener和ContextLoaderPlugIn,spring3.0及以后版本中已经删除ContextLoaderServlet 和Log4jConfigServlet,可以采用余下两种启动方式ContextLoaderListener和ContextLoaderPlugIn,建议使用ContextLoaderListener. 第一

1.2 Spring环境搭建--Spring源码深度解析

前言: Spring 的源码已经从 svn 迁移到 GitHub.而且也改为基于 Gradle 的构建来构建项目.它取代了之前的 Ant+Ivy 系统,所以要构建 Spring 源码环境首先要安装 GitHub 以及 Gradle. 安装 GitHub Windows 系统对应的 GitHub 版本下载地址为: http://windows.github.com/.如图所示:

spring环境搭建(简单实例)

1使用Maven导入需要的依赖(在project标签下) <properties> <spring_version>3.2.2.RELEASE</spring_version> </properties> <!--spring依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</ar

Spring环境搭建错误

1,cvc-complex-type.2.4.c 此错误是因为导使用jar包都是spring 4.0的jar包,但是配置文件引入的xsd文件版本不对,或者少引入了xsd文件导致. 2, Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'juggler' defined in class path resour