`

在Struts2中整合Spring的IoC

阅读更多

申明  要Spring+Strut2  HelloWord代码的留下你们的邮箱,有时间的话就跟你们发过来

In the past, I posted an example on how to use Displaytag with Struts and Spring, using Spring JDBC for data access(1, 2). In this post, I will describe how to do the same using Struts 2.0. The only major step that needs to be done here is to override the default Struts 2.0 OjbectFactory. Changing the ObjectFactory to Spring give control to Spring framework to instantiate action instances etc. Most of the code is from the previous post, but I will list only the additional changes here.

  1. Changing the default Object factory: In order to change the Ojbect factory to Spring, you have to add a declaration in the struts.properties file.
    struts.objectFactory = spring
    struts.devMode = true
    struts.enable.DynamicMethodInvocation = false
    src/struts.properties
  2. The Action class: Here is the code for the action class
    package actions;
    
    import java.util.List;
    
    import business.BusinessInterface;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class SearchAction extends ActionSupport {
    private BusinessInterface businessInterface;
    
    private String minSalary;
    
    private String submit;
    
    private List data;
    
    public String getSubmit() {
     return submit;
    }
    
    public void setSubmit(String submit) {
     this.submit = submit;
    }
    
    public BusinessInterface getBusinessInterface() {
     return businessInterface;
    }
    
      public String execute() throws Exception {
     try {
       long minSal = Long.parseLong(getMinSalary());
       System.out.println("Business Interface: " + businessInterface + "Minimum salary : " + minSal);
       data = businessInterface.getData(minSal);
       System.out.println("Data : " + data);
    
     } catch (Exception e) {
       e.printStackTrace();
     }
    
     return SUCCESS;
    }
    
    public void setBusinessInterface(BusinessInterface bi) {
     businessInterface = bi;
    }
    
    public String getMinSalary() {
     return minSalary;
    }
    
    public void setMinSalary(String minSalary) {
     this.minSalary = minSalary;
    }
    
    public List getData() {
     return data;
    }
    
    public void setData(List data) {
     this.data = data;
    }
    }
    SearchAction.java
    • The Action class here does not have access to the HttpServetRequest and HttpServletResponse. Hence the action class itself was changed to the session scope for this example (see below)
    • In order for the action class to be aware of the Http Session, the action class has to implement the ServletRequestAware interface, and define a setServletRequest method, which will be used to inject the ServletRequest into the action class.
    • The BusinessInterface property is injected by Spring framework.
  3. The struts Configuration:
    <!DOCTYPE struts PUBLIC
         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
         "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>
    <package name="Struts2Spring" namespace="/actions" extends="struts-default">
     <action name="search" class="actions.SearchAction">
       <result>/search.jsp</result>
     </action>
    </package>
    </struts>
    src/struts.xml
    • The action's class attribute has to map the id attribute of the bean defined in the spring bean factory definition.
  4. The Spring bean factory definition
    <?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-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
    default-autowire="autodetect">
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
     <property name="driverClassName">
       <value>oracle.jdbc.driver.OracleDriver</value>
     </property>
     <property name="url">
       <value>jdbc:oracle:thin:@localhost:1521:orcl</value>
     </property>
     <property name="username">
       <value>scott</value>
     </property>
     <property name="password">
       <value>tiger</value>
     </property>
    </bean>
    
    <!-- Configure DAO -->
    <bean id="empDao" class="data.DAO">
     <property name="dataSource">
       <ref bean="dataSource"></ref>
     </property>
    </bean>
    <!-- Configure Business Service -->
    <bean id="businessInterface" class="business.BusinessInterface">
     <property name="dao">
       <ref bean="empDao"></ref>
     </property>
    </bean>
    <bean id="actions.SearchAction" name="search" class="actions.SearchAction" scope="session">
        <property name="businessInterface" ref="businessInterface" />
      </bean>
    </beans>
    WEB-INF/applicationContext.xml
    • The bean definition for the action class contains the id attribute which matches the class attribute of the action in struts.xml
    • Spring 2's bean scope feature can be used to scope an Action instance to the session, application, or a custom scope, providing advanced customization above the default per-request scoping.

  5. The web deployment descriptor
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
    <display-name>Struts2Spring</display-name>
    
    <filter>
     <filter-name>struts2</filter-name>
     <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    
    <filter-mapping>
     <filter-name>struts2</filter-name>
     <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
     <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    <welcome-file-list>
     <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    </web-app>
    web.xmlThe only significant addition here is that of the RequestContextListener. This listener allows Spring framework, access to the HTTP session information.
  6. The JSP file: The JSP file is shown below. The only change here is that the action class, instead of the Data list is accessed from the session.
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="http://displaytag.sf.net" prefix="display"%>
    <%@ taglib prefix="s" uri="/struts-tags"%>
    <%@ page import="actions.SearchAction,beans.Employee,business.Sorter,java.util.List,org.displaytag.tags.TableTagParameters,org.displaytag.util.ParamEncoder"%>
    <html>
    <head>
    <title>Search page</title>
    <link rel="stylesheet" type="text/css" href="/StrutsPaging/css/screen.css" />
    </head>
    <body bgcolor="white">
    <s:form action="/actions/search.action">
    <table>
      <tr>
        <td>Minimum Salary:</td>
        <td><s:textfield label="minSalary" name="minSalary" /></td>
      </tr>
      <tr>
        <td colspan="2"><s:submit name="submit" /></td>
      </tr>
    </table>
    </s:form>
    <jsp:scriptlet>
    
     SearchAction action = (SearchAction)session.getAttribute("actions.SearchAction");
     session.setAttribute("empList", action.getData());
      if (session.getAttribute("empList") != null) {
       String sortBy = request.getParameter((new ParamEncoder("empTable")).encodeParameterName(TableTagParameters.PARAMETER_SORT));
       Sorter.sort((List) session.getAttribute("empList"), sortBy);
      
     </jsp:scriptlet>
    
    <display:table name="sessionScope.empList" pagesize="4" id="empTable" sort="external" defaultsort="1" defaultorder="ascending" requestURI="">
    <display:column property="empId" title="ID" sortable="true" sortName="empId" headerClass="sortable" />
    <display:column property="empName" title="Name" sortName="empName" sortable="true" headerClass="sortable" />
    <display:column property="empJob" title="Job" sortable="true" sortName="empJob" headerClass="sortable" />
    <display:column property="empSal" title="Salary" sortable="true" headerClass="sortable" sortName="empSal" />
    </display:table>
    <jsp:scriptlet>
      }
     </jsp:scriptlet>
    
    </body>
    </html:html>
    search.jsp
分享到:
评论

相关推荐

    struts2整合spring

    在Struts2中整合Spring的IoC支持

    struts2+spring+hibernate整合示例

    1 首先整合spring和hibernate,这次我们在spring 中配置bean使用注解的方式 ,hibernate实体映射关系也使用注解的方式,配置完毕后用简单方法测试下hibernate是否整合成功。 a 加入支持:添加 spring核心包、...

    学习笔记之struts2整合Spring

    整合Spring,换句话说,也就是让spring的IOC功能为我们的struts action注入逻辑组件....

    struts2+spring4+hibernate5所有jar包

    struts2+spring4+hibernate5的所有jar包所有jar包包括spring Aop基本包、spring Ioc基本包、springweb开发包、spring事务控制、spring整合junit、spring整合struts包、hibernate包、hibernate整合spring包、struts2...

    Struts2+Hibernate+Spring整合实例

    Struts2+Hibernate+Spring整合实例,登陆注册实例,简单来说,Spring通过IoC容器上管(Struts2)Action的创建并依赖注入给控制器,下管(hibernate)SessionFactory的创建并依赖注入给DAO组件,是一个巨大的工厂

    struts2-spring-plugin-2.1.2.jar

    导入struts2-spring-plugin包,在web.xml中设置spring的监听器, spring监听器对应的API类为:org.springframework.web.context.ContextLoaderListener。 struts2-spring-plugin包为我们将struts2的对象工厂设置为...

    SSH整合 struts+hibernate+spring

    SSH整合概述 应用IoC进行整合 应用AOP进行整合 Open Session In View模式

    struts+spring+hibernate整合

    ModelDriven、Preparable 拦截器、编写自定义的类型转换器、Struts2 处理 Ajax、OpenSessionInViewFilter、迫切左外连接、Spring 声明式事务、Spring IOC 管理各个组件等。

    SSH笔记-Spring整合Struts2

    SSH笔记-Spring整合Struts2,作用是使用 IOC 容器来管理 Struts2 的 Action

    搞定J2EE:STRUTS+SPRING+HIBERNATE整合详解与典型案例 (1)

    11.4 整合Spring和Struts 11.4.1 Spring和Struts的整合方式 11.4.2 编写实现登录的页面regedit.jsp 11.4.3 编写存储登录用户信息的类User.java 11.4.4 编写控制器RegeditAction.java 11.4.5 编写业务逻辑接口Regedit...

    Spring+Hibernate+struts是如何整合起来的

    首先添加一个WEB工程 然后依次添加Struts框架,spring框架,...在添加Hibernate时注意选择现有的Spring配置文件,Hibernate的配置文件集成在Spring的配置文件中 最后在映射表的时候选择Spring的DAO作为IoC的实现

    DWR,Struts,Hibernate和Spring的J2EE架构开发大全

    基于Struts+Hibernate+Spring的整合架构及其在Web开发中的应用.pdf 基于Struts+Spring+Hibernate架构的轻量级J2EE的研究与应用.pdf 基于Struts+Spring+Hibernate架构的进销存管理系统的设计与实现.pdf 基于...

    aven2+spring(ioc aop)+struts+mybatis

    关于Spring 和 Struts2的整合

    struts spring hibernate整合框架

    Spring IOC : 将程序中容易产生耦合的黏贴代码( dao.setDriver() ) 从程序中提取出去, 配置到配置文件中,由 工厂 根据配置文件创建,并初始化对象。这样,在代码 中就可以彻底利用接口进行编程.

    轻量级J2EE企业应用实战--Struts+Spring+Hibernate整合开发笔记

    轻量级 J2EE 企业应用实战 -- Struts+Spring+Hibernate 整合开发笔记 本资源为轻量级 J2EE 企业应用实战开发笔记,涵盖 Struts、Spring 和 Hibernate 三大框架的整合开发实践。笔记从 JDK 安装和配置环境变量开始,...

    图文教程MyEclipse配置struts+hibernate+spring.doc

    本文档主要讲述了如何在MyEclipse中配置struts、hibernate和spring三个框架,以实现一个完整的Web应用程序。下面是从本文档中提取的重要知识点: 1.struts框架的配置 struts是一个基于MVC模式的Web应用程序框架。...

    搞定J2EE:STRUTS+SPRING+HIBERNATE整合详解与典型案例 (3)

    11.4 整合Spring和Struts 11.4.1 Spring和Struts的整合方式 11.4.2 编写实现登录的页面regedit.jsp 11.4.3 编写存储登录用户信息的类User.java 11.4.4 编写控制器RegeditAction.java 11.4.5 编写业务逻辑接口Regedit...

    struts+spring+hibernate 整合实例

    struts+spring+hibernate 整合实例。供学习三大框架整合人士学习之用,整合,spring 整合IOC 采用注解方式,

Global site tag (gtag.js) - Google Analytics