Spring学习(八)spring整合struts2

来源:互联网 发布:南洋华人 知乎 编辑:程序博客网 时间:2024/05/29 17:41

一、spring框架对struts等表现层框架的整合原理 :
使用spring的ioc容器管理struts中用于处理请求的Action
将Action配置成ioc容器中的bean

延伸:spring对持久层框架/技术的整合原理 (封装) :
提供模板类封装对应技术/框架的开发流程
通过对模板类的使用,实现对传统开发流程的”代替”。
二、整合方式:
插件方式
struts2为了实现对spring框架整合,也提供了一个插件的配置文件struts-plugin.xml
struts2-spring-plugin.jar
三、整合过程
a 创建web工程,添加struts2框架支持(导入struts2自带必需包并在web.xml中配置 Filter)

b web工程添加spring框架支持(导入spring核心层jar包,以及struts2文件下下的包,在web.xml中配置Listener监听器和容器配置文件参数)

四、具体实例
spring在web工程中的使用传送门->
结构:
这里写图片描述
页面:

 <form action="login.action" method="post">        登录名:<input type="text"  name="loginname"/><br/>        密码:<Input type="password" name="password"/><br/>        <Input type="submit" value="登录"/>    </form>

web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0"     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_3_0.xsd">  <filter>    <filter-name>etoak</filter-name>    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  </filter>  <filter-mapping>    <filter-name>etoak</filter-name>    <url-pattern>*.action</url-pattern>  </filter-mapping>  <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>/WEB-INF/applicationContext.xml</param-value>  </context-param>  <display-name></display-name>   <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>

applicationContext.xml

<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.2.xsd">    <!--         每在工程添加一个用于处理请求的Action        就需要将其配置成ioc容器中的bean        1 action何时实例化?  容器启动时            单独使用struts2时,action何时实例化?                客户端每次提交请求时        2 action实例化状态?  单例            单独使用struts2时,非单例        注意 : 为了保证action状态的一致性;在将Action        配置成ioc容器中的bean对象时,需要将其状态设置为~非单例状态     -->    <bean id="action" class="com.etoak.action.LoginAction"        scope="prototype"></bean></beans>

struts.xml

<!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"    "http://struts.apache.org/dtds/struts-2.3.dtd"><struts>    <!--         请求解析器 ActionProxy "login"        请求调度者 ActionInvocation      -->    <package name="etoak" extends="struts-default">        <!--             请求和处理之间的映射关系如何形成            <action name="请求" class="Action">            class属性:指向的是ioc容器中某个bean的ID值         -->        <action name="login" class="action">            <result name="success" type="redirect">/success.jsp</result>        </action>    </package></struts>

LoginAction.java

public class LoginAction extends ActionSupport {    private String loginname;    private String password;    public String getLoginname() {        return loginname;    }    public void setLoginname(String loginname) {        this.loginname = loginname;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    @Override    public String execute() throws Exception {        // 处理login.action请求 - DAO        System.out.println("loginname--"+loginname);        System.out.println("password--"+password);        // 调用DAO    ioc依赖注入 (setter注入)        return this.SUCCESS;    }}
0 0
原创粉丝点击