Eclipse 搭建Spring Struts2

本文详细介绍如何在Struts2项目中集成Spring框架,包括引入依赖包、创建接口及其实现类、修改Action处理方式、配置Spring及Struts2等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在上一章中写了,搭建struts2的配置,这次接着上一章的博客,集成spring。

上一章:http://blog.youkuaiyun.com/lipp555/article/details/50724996

项目结构:


1,首先导入spring的包。

2.创建接口,和实现接口的类

接口:LoginManager

package com.ss02.manager;

public interface LoginManager {
	void loginVali(String name);
}
实现类:LoginManagerImp

package com.ss02.manager.imp;

import com.ss02.manager.LoginManager;

public class LoginManagerImp implements LoginManager {

	@Override
	public void loginVali(String name) {
		System.out.println("登录名:"+name);
		
	}

}

3.修改Action的写法:

LoginAction

package com.ss02.action;

import com.opensymphony.xwork2.ActionSupport;
import com.ss02.manager.LoginManager;

public class LoginAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7752780747809187029L;

	LoginManager loginManager;

	@Override
	public String execute() throws Exception {
		System.out.println("execute");
		loginManager.loginVali("lzz");
		return super.execute();
	}

	public String login() throws Exception {
		System.out.println("登录");
		loginManager.loginVali("--lzz--");
		return SUCCESS;
	}

	public LoginManager getLoginManager() {
		return loginManager;
	}

	public void setLoginManager(LoginManager loginManager) {
		this.loginManager = loginManager;
	}

}


4.添加spring配置文件,在WEB-INF文件夹下,添加springResource文件夹,在springResource文件夹下添加:applicationContext.xml,applicationContext-login.xml

applicationContext.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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
	default-autowire="byName" default-lazy-init="true">

	<bean id="baseTransactionProxy"
		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
		abstract="true">
		<property name="transactionManager" ref="transactionManager" />
		<property name="transactionAttributes">
			<props>
				<prop key="insertSynchronized*">PROPAGATION_REQUIRES_NEW</prop>
				<prop key="insert*">PROPAGATION_REQUIRED</prop>
				<prop key="create*">PROPAGATION_REQUIRED</prop>
				<prop key="add*">PROPAGATION_REQUIRED</prop>
				<prop key="update*">PROPAGATION_REQUIRED</prop>
				<prop key="reset*">PROPAGATION_REQUIRED</prop>
				<prop key="delete*">PROPAGATION_REQUIRED</prop>
				<prop key="del*">PROPAGATION_REQUIRED</prop>
				<prop key="save*">PROPAGATION_REQUIRED</prop>
				<prop key="edit*">PROPAGATION_REQUIRED</prop>
				<prop key="release*">PROPAGATION_REQUIRED</prop>
				<prop key="modify*">PROPAGATION_REQUIRED</prop>
				<prop key="change*">PROPAGATION_REQUIRED</prop>
				<prop key="invoke*">PROPAGATION_REQUIRED</prop>
				<prop key="submit*">PROPAGATION_REQUIRED</prop>
				<prop key="batchImport*">PROPAGATION_REQUIRED</prop>
				<prop key="flush*">PROPAGATION_REQUIRED</prop>
				<prop key="excute*">PROPAGATION_REQUIRED</prop>
				<prop key="replace*">PROPAGATION_REQUIRED</prop>
				<prop key="export*">PROPAGATION_REQUIRED</prop>
				<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
			</props>
		</property>
	</bean>


</beans>
applicationContext-login.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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
	default-autowire="byName" default-lazy-init="true">

	<bean id="loginAction" class="com.ss02.action.LoginAction" scope="prototype">
		<property name="loginManager" ref="loginManager" />
	</bean>
	
	<bean id="loginManager" class="com.ss02.manager.imp.LoginManagerImp"></bean>
<!--
	事务控制 	
	<bean id="loginManager" parent="baseTransactionProxy">
		<property name="target">
			<bean class="com.ss02.manager.imp.LoginManagerImp">
			</bean>
		</property>
	</bean> -->
</beans>
  

5.修改struts.xml中的文件,将struts托管给spring,将action中的class,改为spring中action的id:

<?xml version="1.0" encoding="UTF-8" ?>  
  <!DOCTYPE struts PUBLIC  
      "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
      "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<!-- struts的action配置文件 -->
	<!-- 将action托管给spring -->
	<constant name="struts.objectFactory" value="spring"></constant>
	<!-- 所有的action都应该放在对应的package下 -->
	<package name="ss" extends="struts-default" namespace="/lzz">
		<action name="login" class="loginAction">
			<!-- 定义逻辑视图和物理资源之间的映射 -->
			<result name="success">/page/Come.jsp</result>
			<result name="error">/page/Hello.jsp</result>
		</action>

		<action name="login_*" class="loginAction"
			method="{1}">
			<!-- 定义逻辑视图和物理资源之间的映射 -->
			<result name="success">/page/Come.jsp</result>
			<result name="error">/page/Hello.jsp</result>
		</action>
	</package>
</struts>  


6.在web.xml中添加spring的配置,放在struts配置前

  	<!-- 初始化 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<!-- 如果有多个文件,在文件之间用英文逗号隔开 -->
		<!-- <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/applicationContext-db.xml 
			</param-value> -->
		<param-value>/WEB-INF/springResource/applicationContext.xml,/WEB-INF/springResource/applicationContext-*.xml</param-value>
	</context-param>  
    <!-- 监听器 -->  
    <!-- 配置spring监听器 -->    
    <listener>    
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    
    </listener>  

配置结束。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值