Spring进阶之Spring和struts的整合
来源:互联网 发布:淘宝团队管理权限 编辑:程序博客网 时间:2024/06/05 05:46
一)使用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;}@Overridepublic String execute() throws Exception {// TODO Auto-generated method stubActionContext.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.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://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>
运行结果:
0 0
- Spring进阶之Spring和struts的整合
- struts和spring的整合
- struts和spring的整合!
- 整合 Struts 和 Spring
- struts和spring整合
- 整合 Struts 和 Spring
- 整合 Struts 和 Spring
- Spring和Struts整合
- 整合spring 和struts
- spring和struts整合
- spring 和 struts 整合
- struts 和spring 整合
- 整合Struts和Spring
- Spring和Struts整合
- Sping进阶之Spring和Hibernate的整合
- Shiro和spring的整合之进阶篇
- JPetStore中Struts和Spring的整合
- Spring和Struts的整合方案1
- Asp.Net MVC4.0学习之七--增加搜索方法和视图
- 告诉你一个秘密
- 织梦小常识dede title 标题长度限制(超出显示省略号,不超出则显示全部)
- FFMpeg写MP4文件例子分析
- HTML教程 - html实例和调试
- Spring进阶之Spring和struts的整合
- UVA 10047 The Moncycle
- Asp.Net MVC4.0 学习之八--为Movie模型和库表添加字段
- linux驱动学习之并发和竞争控制
- MyEclipse中配置多个相同版本的tomcat
- OpenStack云计算快速入门之一:OpenStack及其构成简介
- android如何实现开机自动启动Service或app
- 开源的毕业设计—3D虚拟社区
- Asp.Net MVC4.0 学习之九--为模型添加验证