码迷,mamicode.com
首页 > 编程语言 > 详细

Spring学习-8-Spring与struts整合

时间:2016-08-11 17:28:03      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

实例代码分析

3. SpringStruts框架整合

Spring,负责对象对象创建

Struts, 用Action处理请求

SpringStruts框架整合,

关键点:让struts框架action对象的创建,交给spring完成!

步骤:

引入jar文件

1)引入struts .jar相关文件

2spring-core 相关jar文件

3spring-web 支持jar

spring-web-3.2.5.RELEASE.jar Spring源码】

struts2-spring-plugin-2.3.4.1.jar Struts源码】

配置:

4)配置XML

struts.xml struts路径与action映射配置】

bean.xml spring ioc容器配置】

web.xml

【核心过滤器: 引入struts功能】

【初始化springioc容器】

查看api

4) 代码以及配置

dao

package com.cx.dao;

/**
 * Created by cxspace on 16-8-9.
 */
public class UserDao {

    public void save(){
        System.out.println("DB:保存用户");
    }

}

  

service

 

package com.cx.service;

import com.cx.dao.UserDao;

/**
 * Created by cxspace on 16-8-9.
 */
public class UserService {

    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void save(){
        userDao.save();
    }

}

  

action

package com.cx.action;

import com.cx.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

/**
 * Created by cxspace on 16-8-9.
 */
public class UserAction extends ActionSupport{

    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public String execute(){
        userService.save();
        return SUCCESS;
    }
}

bean配置

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       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">


    <bean id="userDao" class="com.cx.dao.UserDao"></bean>

</beans>

================================================================

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       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">

    <bean id="userService" class="com.cx.service.UserService">
        <property name="userDao" ref="userDao"></property>
    </bean>
</beans>

================================================================

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       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">

        <bean id="userAction" class="com.cx.action.UserAction">
            <property name="userService" ref="userService"></property>
        </bean>
</beans>

================================================================

  

struts配置

<?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="default"  extends="struts-default">
      <action name="user" class="userAction" method="execute">
         <result name="success">/index.jsp</result>
      </action>
   </package>
</struts>

  

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <!--struts配置-->

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--spring配置  主要是初始化spring的ioc容器-->

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/bean-*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

  

index.jsp

 

<%--
  Created by IntelliJ IDEA.
  User: cxspace
  Date: 16-8-9
  Time: 下午6:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
    Spring-struts整合ok
  </body>
</html>

  

  

 

Spring学习-8-Spring与struts整合

标签:

原文地址:http://www.cnblogs.com/cxspace/p/5761624.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!