SSH组合之Spring3整合Struts2

1、Add Spring Capabilities

2、Add struts2 Capabilities+对应版本的struts2-spring-plugin-2.2.1.jar,最好是对应版本

3、struts.xml

(1) 配置为Spring

(2) 配置Action;

SSH组合之Spring3整合Struts2
 <action name="*_cat" method="{1}" class="catAction">
SSH组合之Spring3整合Struts2

这里class 的”catAction“需要在applicationContext.xml配置才能找到真正的class;

action类不需要继承ActionSupport,只需要有对应每个Action名称的方法;

可以在action类有相关的变量,需要get-set函数;

如果action类需要使用Dao层,则dao变量需要声明

SSH组合之Spring3整合Struts2
    <!-- struts2的Action -->
    <!-- scope默认为singleston,这里一定要设置 成prototype。这样浏览器每次请求一次,spring都会生成一个新的Action对象-->
    <bean id="catAction" scope="prototype" class="com.cat.action.CatAction">
    <property name="catService" ref="catService"></property>
    </bean>
        <!-- 配置Dao -->
    <bean id="catService" class="com.cat.service.catDao">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
SSH组合之Spring3整合Struts2

 

struts.xml

SSH组合之Spring3整合Struts2
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <constant name="struts.objectFactory" value="spring" /><!-- 配置为Spring,表该Action由Sprin*生 -->
    <constant name="struts.devMode" value="true" /> <!-- 是否开发模式 -->

    <package name="cat" extends="struts-default">
        <!--<global-results>
            <result name="exception">/exception.jsp</result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping result="exception" exception="java.lang.Exception"></exception-mapping>
        </global-exception-mappings>-->

        <action name="*_cat" method="{1}" class="catAction">
            <param name="action">{1}</param>
            <result>/list.jsp</result>
            <result name="list">list.jsp</result>
        </action>
    </package>
</struts>    
SSH组合之Spring3整合Struts2

 

4、CatAction.java

SSH组合之Spring3整合Struts2
package com.cat.action;

import java.util.ArrayList;
import java.util.List;

import com.cat.bean.Cat;
import com.cat.service.ICatService;

public class CatAction {

    private ICatService catService;
    private Cat cat;
    private List<Cat> catList=new ArrayList<Cat>();
    
    public String add(){
        catService.createCat(cat);
        return list();
    }
    public String list(){
        catList=catService.listCats();
        return "list";
    }
    public ICatService getCatService() {
        return catService;
    }
    public void setCatService(ICatService catService) {
        this.catService = catService;
    }
    public Cat getCat() {
        return cat;
    }
    public void setCat(Cat cat) {
        this.cat = cat;
    }
    public List<Cat> getCatList() {
        return catList;
    }
    public void setCatList(List<Cat> catList) {
        this.catList = catList;
    }
    
    
}
SSH组合之Spring3整合Struts2

 

Done!

SSH组合之Spring3整合Struts2,布布扣,bubuko.com

SSH组合之Spring3整合Struts2

上一篇:Struts2+hibernate+spring 配置事物


下一篇:JAVA在线观看视频教程完整版