spring3.1.1+struts2.3.3组合

本文介绍如何将Spring3.1.1与Struts2.3.3整合到一起,包括所需配置文件如applicationContext.xml、struts.xml及web.xml的具体设置,并提供了一个简单的测试类来验证整合是否成功。

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

spring3.1.1和struts2.3.3都发布了GA版本,于是下载整合到一起。

新建web工程,将spring3.1.1和struts2.3.3下载后,然后将所有的jar包放到项目中,

这些jar不是全部有用,建议删除一些,或者加上其他的jar,项目才能正常运行。

建议删除的jar:struts2-portlet*.jar,struts2-gxp*.jar,struts2-osgi*.jar,struts2-codebehind*.jar.

几个常用的文件和测试类:

1。applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" [
<!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml">
]>

<beans>
<bean id="testSpring" name="testSpring" class="com.xzxds.lei.test.TestSpring"/>
<bean name="testStruts2" class="com.xzxds.lei.test.TestStruts2"></bean>

</beans>

2。struts.xml 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<constant name="struts.objectFactory" value="spring"></constant>
	<package name="default" namespace="/" extends="struts-default">
	
	<action name="testStruts2" class="testStruts2" method="testMethod"/>

	</package>
	<!-- 
    <include file="example.xml"/> -->


	<!-- Add packages here -->

</struts>

3。web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<display-name>xzxds-web-config</display-name>

	<!-- struts config begin-->
	<filter>
		<filter-name>struts2x</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2x</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- struts config end-->

	<!-- spring config begin-->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml,classpath:struts.xml</param-value>
	</context-param>


	
	
	<!-- spring config end-->


	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>


4。测试类

package com.xzxds.lei.test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.ContextLoaderListener;

import com.opensymphony.xwork2.ActionSupport;


public class TestAction  {

	/**
	 * test struts and spring 
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		
		TestSpring ts=(TestSpring)ac.getBean("testSpring");
		
		System.out.println(ts.getName());
		
		TestStruts2 ts2=(TestStruts2)ac.getBean("testStruts2");
		
		ts2.testMethod();
		
		
	}

}

package com.xzxds.lei.test;


public class TestSpring {
	private String name="wooo";

	
	public String getName() {
		return name;
	}

	
	public void setName(String name) {
		this.name = name;
	}
	

}

package com.xzxds.lei.test;

import com.opensymphony.xwork2.ActionSupport;


public class TestStruts2 extends ActionSupport{
	
	
	public String testMethod() throws Exception {

		System.out.println("struts2 success");
		
		return "success";
	}



}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值