zoukankan      html  css  js  c++  java
  • SSH组合之Spring3整合Struts2

    1、Add Spring Capabilities

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

    3、web.xml

    <!-- Struts2的Filter,所有请求都被映射到Struts2上 -->
        <filter>
            <filter-name>struts</filter-name><!-- filter名称 -->
            <filter-class>
                org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter<!-- filter入口 -->
            </filter-class>
        </filter>
        <!-- struts2的filter的uri配置 -->
        <filter-mapping>
            <filter-name>struts</filter-name><!-- filter名称 -->
            <url-pattern>/*</url-pattern><!-- 截获所有URL -->
        </filter-mapping>

    3、struts.xml

    (1) 配置为Spring

        <constant name="struts.objectFactory" value="spring" /><!-- 配置为Spring,表该Action由Spring产生 -->
        <constant name="struts.devMode" value="true" /> <!-- 是否开发模式 -->

    (2) 配置Action;

     <action name="*_cat" method="{1}" class="catAction">

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

    <!-- struts2会到Spring中寻找与action的class属性相同的bean的id-->

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

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

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

        <!-- 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>

    struts.xml

    注意继承ActionSupport的action会自动执行execute,但在这里需要action制定method属性去执行

    <?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由Spring产生 -->
        <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>    

    4、CatAction.java

    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;
        }
        
        
    }

    Done!

  • 相关阅读:
    C#不显示在任务栏
    打开文件,文件夹
    C#文本操作
    C#路径2
    C#当前程序路径获取
    HDU 5155 Harry And Magic Box dp
    POJ 1971 Parallelogram Counting
    CodeForces 479C Exams 贪心
    CodeForces 508E Arthur and Brackets 贪心
    CodeForces 483B 二分答案
  • 原文地址:https://www.cnblogs.com/xingyyy/p/3599508.html
Copyright © 2011-2022 走看看