这篇开始整合struts2+spring
1、加入spring的包,或者是之前你已经导入所有的包,现在只需加入那个删掉的struts+spring包即可。
2、在src下复制struts.xml然后改名为spring的配置文件applicationContext.xml,并加入对struts的管理
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="loginAction" class="com.action.LoginAction" scope="prototype">
</bean>
</beans>
3、struts.xml改成
<?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>
<constant name="struts.devMode" value="false" />
<package name="struts2" extends="struts-default">
<!-- 此处的class的内容要与Spring配置文件中的bean的id相同 -->
<action name="login" class="loginAction">
<result name="success">/result.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
4、在web.xml加入spring相关支持
<!-- 用来定位Spring框架配置文件 ,最好放在struts配置的前面,防止出现意外错误。-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml,classpath*:applicationContext*.xml</param-value>
</context-param>
<!-- 配置Spring监听 ,这也放在struts前面吧。-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
5、把index.jsp改成(其实只是添加“+spring”的字眼。。)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Hello World From Struts2 + Spring</h1>
<form action="login">
<label for="name">Please enter your name</label><br/>
<input type="text" name="name"/>
<input type="submit" value="Say Hello"/>
</form>
</body>
</html>
6、重启tomcat,打开页面
点击登录后弹出
这里便完成了struts2跟spring的整合,最后一篇整合hibernate