1. 程式人生 > >Spring整合Struts2框架的第一種方式(Action由Struts2框架來創建)。在我的上一篇博文中介紹的通過web工廠的方式獲取servcie的方法因為太麻煩,所以開發的時候不會使用。

Spring整合Struts2框架的第一種方式(Action由Struts2框架來創建)。在我的上一篇博文中介紹的通過web工廠的方式獲取servcie的方法因為太麻煩,所以開發的時候不會使用。

rac println 需要 如果 value const tps http 建立

1. spring整合struts的基本操作見我的上一篇博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,這裏面將spring與struts2框架整合起來,並且實現了action獲取service,說明spring與struts2框架已經建立聯系,互通了,但是這種使用web工廠的方式太麻煩了,在開發中並不會使用這種方法,所以我就要介紹spring整合struts2框架的另外一種方法。目前有兩種方法,現在介紹第一種方式,第二種方式見我的下一篇博文:。

2. 因為導入的struts2-spring-plugin-2.3.24.jar 包自帶一個配置文件 struts-plugin.xml ,該配置文件中有如下代碼:<constant name="struts.objectFactory" value="spring" /> 開啟一個常量,如果該常量開啟,那麽下面的常量就可以使用struts.objectFactory.spring.autoWire = name,該常量是可以讓Action的類來自動裝配Bean對象!!

3.更改CustomerAction代碼,不是通過原來的web工廠的方式,而是通過數據封裝的方式(使用了裏面的模型封裝),代碼如下:

package com.huida.web;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils; import com.huida.domain.Customer; import com.huida.service.CustomerService; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; public class CustomerAction extends ActionSupport implements
ModelDriven<Customer>{ //手動實例化對象 //private Customer customer=new Customer(); private CustomerService customerService; public void setCustomerService(CustomerService customerService) { this.customerService = customerService; } /* * 保存客戶的方法 */ public String save(){ System.out.println("Action中執行了save方法"); customerService.save(); return NONE; } @Override public Customer getModel() { return null; } }

4.action由struts2創建,所以需要在strutsx.xml中配置action,配置內容為:

<?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="crm" namespace="/" extends="struts-default">
        <action name="customer_*" class="com.huida.web.CustomerAction" method="{1}">
            <result name=""></result>
        </action> 
    </package>
</struts>

5.在applicationContext.xml中配置service,這樣在加載配置文件的時候會掃描到CustomerService,然後為其創建對象,配置內容為:

<?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    <bean id="customerService" class="com.huida.service.CustomerServiceImpl">
        
    </bean>
</beans>

6. 我們可以驗證一下struts與spring整合是否成功。

啟動服務器-->在瀏覽器中輸入http://localhost:8080/ssh1-->在頁面中點擊客戶管理-->新增客戶-->點擊保存按鈕。在控制臺上輸出如下內容:

技術分享圖片

通過以上步驟我們便將struts與spring通過傳統的方法整合起來了。但是這種整合不是我推薦的方法,所以在下一篇博文中我就對將我推薦的方法進行總結。

Spring整合Struts2框架的第一種方式(Action由Struts2框架來創建)。在我的上一篇博文中介紹的通過web工廠的方式獲取servcie的方法因為太麻煩,所以開發的時候不會使用。