首页 > 编程语言 > 详细

SSH组合之Spring3整合Struts2

时间:2014-03-14 18:09:25      阅读:493      评论:0      收藏:0      [点我收藏+]

1、Add Spring Capabilities

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

3、struts.xml

(1) 配置为Spring

(2) 配置Action;

bubuko.com,布布扣
 <action name="*_cat" method="{1}" class="catAction">
bubuko.com,布布扣

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

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

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

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

bubuko.com,布布扣
    <!-- 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>
bubuko.com,布布扣

 

struts.xml

bubuko.com,布布扣
<?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>    
bubuko.com,布布扣

 

4、CatAction.java

bubuko.com,布布扣
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;
    }
    
    
}
bubuko.com,布布扣

 

Done!

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

SSH组合之Spring3整合Struts2

原文:http://www.cnblogs.com/xingyyy/p/3599508.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!