1.Struts和Spring之间的整合,还是配置问题。

2.最重要的是spring是个容器,原来所有的框架的使用是要注入到spring中的啊.... 怪不得,说它是个容器那,原来还真是个容器啊!

3.下面开始配置一下,使用吧,对了使用之前,记得jar包考进去,别忘了了!阿秋!

4.web.xml 选择2.5版的用啦

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Test33</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> <!-- spring有关 -->
<!-- 配置文件 --> <!-- needed for ContextLoaderListener --> 这个可以通过快捷方式alt+/+c 就可以出来啦!
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:app.xml</param-value>
</context-param> <!-- Bootstraps the root web application context before servlet initialization -->
<!-- 把Spring容器放到全局对象中 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Strtus2的过滤器 --> 这就是Struts在web中需要的过滤器啦!
<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>
</web-app>

5. 定义的方法,DAO,Service和Action类

public class JiSQ {

    public double add(double a,double b)
{
return (a+b);
}
}
public class TestDAO {
// 数据库连接
private String conn; public String getConn() {
return conn;
} public void setConn(String conn) {
this.conn = conn;
} public String getName()
{
return "得到连接="+conn+ "调用了DAO";
}
}
public class TestService {

    private TestDAO testDAO;

    public TestDAO getTestDAO() {
return testDAO;
} public void setTestDAO(TestDAO testDAO) {
this.testDAO = testDAO;
} public String getDAOName()
{
// TestDAO testDAO = new TestDAO(); return testDAO.getName();
}
}
public class TestAction {

    private TestService testService;

    public TestService getTestService() {
return testService;
} public void setTestService(TestService testService) {
this.testService = testService;
} public String test()
{
//TestService testService = new TestService(); System.out.println(""+testService.getDAOName());
return "success";
}
}

6.容器注入的代码

spring的配置文件app.xml

<?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"
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-4.2.xsd"> <bean id="jsq" class="com.hanqi.test.JiSQ"></bean> <bean id="testDAO" class="com.hanqi.test.TestDAO">
<property name="conn" value="Oracle"></property> -- 注入属性值
</bean>
<bean id="testService" class="com.hanqi.test.TestService">
<property name="testDAO" ref="testDAO"></property>
</bean> <!-- Action类的实例不能是单例的 -->
<bean id="testID" class="com.hanqi.test.TestAction" scope="prototype">
<property name="testService" ref="testService"></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> <!-- 覆盖默认的过滤的扩展名 -->
<constant name="struts.action.extension" value="do,action,,"></constant> <!-- 允许调用静态方法和静态属性 -->
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant> <!-- 定义包 -->
<package name="index" extends="struts-default"> <action name="testform" class="testID" method="test"> class == spring容器中注入的<action>类
<result>view.jsp</result>
</action> </package> </struts>

Struts2和Spring的整合的更多相关文章

  1. struts2+hibernate-jpa+Spring+maven 整合(1)

    1.0.0 struts2 与 spring 的整合. 1.1.0 新建maven工程 , 编写pom.xml ,这里只需要简单的添加 一个组件就够了: 在myeclipse 生成的pom.xml 添 ...

  2. Struts2与Spring的整合

    今天倒腾了半天,终于是把这个两个框架整合到一起了.还是要写一下总结,同时给大家一些帮助. 开发环境:myeclipse 9.0(不好用!)tomcat6.0 1.准备工作 需要导入的包:struts2 ...

  3. struts2+hibernate+spring简单整合且java.sql.SQLException: No suitable driver 问题解决

    最近上j2ee的课,老师要求整合struts2+hibernate+spring,我自己其实早早地有准备弄的,现在都第9个项目了,无奈自己的思路和头绪把自己带坑了,当然也是经验问题,其实只是用myec ...

  4. struts2+hibernate-jpa+Spring+maven 整合(2)

    1.修改pom.xml 1. 添加  slf4j-api <dependency> <groupId>org.slf4j</groupId> <artifac ...

  5. ssh整合思想初步 struts2与Spring的整合 struts2-spring-plugin-2.3.4.1.jar下载地址 自动加载Spring中的XML配置文件 Struts2下载地址

    首先需要JAR包 Spring整合Structs2的JAR包 struts2-spring-plugin-2.3.4.1.jar 下载地址 链接: https://pan.baidu.com/s/1o ...

  6. java学习笔记(11) —— Struts2与Spring的整合

    1.右键 项目名称 —— MyEclipse —— Add Spring Capabilities 2.选取 Copy checked Library contents to project fold ...

  7. Spring第四篇【Intellij idea环境下、Struts2和Spring整合】

    前言 Spring的第二和第三篇已经讲解了Spring的基本要点了[也就是Core模块]-本博文主要讲解Spring怎么与Struts2框架整合- Struts2和Spring的整合关键点: acti ...

  8. Struts2与Spring整合

    前言 本博文主要讲解Spring怎么与Struts2框架整合... Struts2和Spring的整合关键点: action对象交给Spring来创建 搭建环境 进入jar包 引入jar文件: 1)引 ...

  9. struts2与spring整合时需要注意的点

    首先我们需要明白spring整合struts2中的什么东西,spring中的核心就是IOC和AOP,IOC是对象的容器,AOP是处理动态代理的;比如spring与hibernate整合时就要用到aop ...

随机推荐

  1. 虚拟现实外包公司— VR开发编辑器意义重大 印证VR不仅服务于用户

    三大引擎制造商 Unity Technologies .Epic Games 和Crytek 讨论在自家产品中添加附件,方便开发商在虚拟现实中创作游戏.这会对许多人造成影响,特别是早已进入虚拟现实的先 ...

  2. mybatis实战教程(mybatis in action)之十:mybatis SqlSessionSupport 的使用,构件DAO 层的应用

    前面的系列mybatis 文章,已经基本讲到了mybatis的操作,但都是基于mapper隐射操作的,在mybatis 3中这个mapper 接口貌似充当了以前在ibatis 2中的 DAO 层的作用 ...

  3. 转:面试题:“你能不能谈谈,java GC是在什么时候,对什么东西,做了什么事情?”

    转自:http://jeromecen1021.blog.163.com/blog/static/18851527120117274624888/ 面试题目: 地球人都知道,Java有个东西叫垃圾收集 ...

  4. Elasticsearch mysql 增量同步

    主要用到了一个JDBC importer for Elasticsearch的库. 想要增量同步,有一些先决条件.首先数据库中要维护一个update_time的时间戳,这个字段表示了该记录的最后更新时 ...

  5. C# webbrowser实现真正意义上的F5刷新

    关于webbrowser的刷新在C#中有提供方便的方法: webbrowser.refresh(); 但是有时候会发现,不给力啊 那怎么办? 还有一招: webBrowser1.Document.Ex ...

  6. git bash下对文件的操作

    window下的e盘中webpack文件夹操作 新建文件夹:mkdir wpdemo: 新建文件:touch index.html; 删除文件夹:rm -r wpdemo; 删除文件:rm index ...

  7. #define中 #与##用法

    参考自: http://zjf30366.blog.163.com/blog/static/411164582009061075923/ #include<cstdio> #include ...

  8. .Net Enum

    /// <summary> /// 将enum转换成List<Model.Models.SelectViewModels>,即html的select标签使用的数据 /// &l ...

  9. Premier使用笔记

    在高中时喜欢打CS,也喜欢看高手的视频,一直打到这游戏淘汰了,为了纪念,也尝试着做了一个视频,就是利用该入门级软件.无意中翻出了当时使用这软件时的笔记,整理到这里,可能这些内容对于博客园来讲太垃圾,毫 ...

  10. Android 进程通信机制之 AIDL

    什么是 AIDL AIDL 全称 Android Interface Definition Language,即 安卓接口描述语言.听起来很深奥,其实它的本质就是生成进程间通信接口的辅助工具.它的存在 ...