一)使用springWeb模块,将spring集成struts2【register.jsp->UserAction->message.jsp】
(1)整合原理
(A)struts2的Action不再由struts2框架产生,反而由spring框架代为产生.
(B)当web服务器启动时,由spring负责产生
(C)每次请求后,struts2框架从spring容器中找到对应的bean来处理业务
(D)因为struts2的action是一个原型的,所以spring容器在创建action时,一定要配置成scopt="prototype"
(2)集成步骤
(A)添加struts2、spring、struts2-spring-plugin-2.3.1.1.jar到WEB-INF/lib/目录下
(B)配置struts.xml文件
<struts>
<package name="struts_spring" extends="struts-default" namespace="/">
<action name="register" class="userAction" method="execute">
<result name="success" type="dispatcher">/message.jsp</result>
</action>
</package>
</struts>
(C)配置spring.xml文件
<bean id="userAction" class="com.spring_struts.demo.UserAction" scope="prototype">
</bean>
(D)配置web.xml文件,目的是在部署web应用时,启动spring容器
<!-- 配置struts2过滤器 -->
<filter>
<filter-name>struts2_filter</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2_filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置springWeb模块的启动器,加载指定目录下的applicationcontext。xml文件呢 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<!-- 加载指定目录下一个或多个applicationcontext.xml文件 -->
<param-name>contextConfigLocation</param-name>
<!-- 以下路径是部署到Web服务器上的路径,而不是IDE工具中的路径 -->
<param-value>/WEB-INF/classes/com/spring_struts/demo/applicationContext.xml</param-value>
接着写配置文件applicationContext.xml:
接着在src目录下写struts.xml配置:
接着在web.xml的配置:
注册页面:
消息页面:
运行结果:
(1)整合原理
(A)struts2的Action不再由struts2框架产生,反而由spring框架代为产生.
(B)当web服务器启动时,由spring负责产生
(C)每次请求后,struts2框架从spring容器中找到对应的bean来处理业务
(D)因为struts2的action是一个原型的,所以spring容器在创建action时,一定要配置成scopt="prototype"
(2)集成步骤
(A)添加struts2、spring、struts2-spring-plugin-2.3.1.1.jar到WEB-INF/lib/目录下
(B)配置struts.xml文件
<struts>
<package name="struts_spring" extends="struts-default" namespace="/">
<action name="register" class="userAction" method="execute">
<result name="success" type="dispatcher">/message.jsp</result>
</action>
</package>
</struts>
(C)配置spring.xml文件
<bean id="userAction" class="com.spring_struts.demo.UserAction" scope="prototype">
</bean>
(D)配置web.xml文件,目的是在部署web应用时,启动spring容器
<!-- 配置struts2过滤器 -->
<filter>
<filter-name>struts2_filter</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2_filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置springWeb模块的启动器,加载指定目录下的applicationcontext。xml文件呢 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<!-- 加载指定目录下一个或多个applicationcontext.xml文件 -->
<param-name>contextConfigLocation</param-name>
<!-- 以下路径是部署到Web服务器上的路径,而不是IDE工具中的路径 -->
<param-value>/WEB-INF/classes/com/spring_struts/demo/applicationContext.xml</param-value>
</context-param>
接下来用代码实例来介绍:
首先贴出要导包的目录截图:
写一个实体类User:
package com.spring_struts.demo;
public class User {
private String name;
private String password;
public User() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
接着写UserAction:
package com.spring_struts.demo;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
ActionContext.getContext().put("user", user);
return "success";
}
}
接着写配置文件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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="userAction" class="com.spring_struts.demo.UserAction" scope="prototype">
</bean>
</beans>
接着在src目录下写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>
<package name="struts_spring" extends="struts-default" namespace="/">
<action name="register" class="userAction" method="execute">
<result name="success" type="dispatcher">/message.jsp</result>
</action>
</package>
</struts>
接着在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">
<display-name></display-name>
<!-- 配置struts2过滤器 -->
<filter>
<filter-name>struts2_filter</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2_filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置springWeb模块的启动器,加载指定目录下的applicationcontext。xml文件呢 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<!-- 加载指定目录下一个或多个applicationcontext.xml文件 -->
<param-name>contextConfigLocation</param-name>
<!-- 以下路径是部署到Web服务器上的路径,而不是IDE工具中的路径 -->
<param-value>/WEB-INF/classes/com/spring_struts/demo/applicationContext.xml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
注册页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>注册页面</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/register" method="post">
<table align="center">
<tr>
<td>姓名</td>
<td><input type="text" name="user.name"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="user.password"></td>
</tr>
<tr >
<td colspan="2">
<input type="submit" value="提交">
</td>
</tr>
</table>
</form>
</body>
</html>
消息页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>消息页面</title>
</head>
<body>
我的名字:${user.name }
我的密码:${user.password }
</body>
</html>
运行结果: