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. 使用.net 创建windows service

    最近公司项目需要,写了个windows 服务,windows 服务的内容可以在VS 中新建一个"windows服务项目", (1)服务中的主要代码: public partial ...

  2. Linux Epoll相关知识

    其实在Linux下设计并发网络程序,向来不缺少方法,比如典型的Apache模型(Process Per Connection,简称PPC),TPC(Thread PerConnection)模型,以及 ...

  3. JAVA多态的定义

    对于多态,可以总结它为: 一.使用父类类型的引用指向子类的对象: 二.该引用只能调用父类中定义的方法和变量: 三.如果子类中重写了父类中的一个方法,那么在调用这个方法的时候,将会调用子类中的这个方法: ...

  4. JVM GC算法 CMS 详解(转)

    前言 CMS,全称Concurrent Low Pause Collector,是jdk1.4后期版本开始引入的新gc算法,在jdk5和jdk6中得到了进一步改进,它的主要适合场景是对响应时间的重要性 ...

  5. 使用compass编译sass

    1.初始化项目 compass create test(项目名称),会在当前目录下创建test子目录,test的子目录下有config.gb文件,sass和stylesheets文件夹. 2.编写sa ...

  6. .NET DataSet、DataTable操作记录

    一直在习惯.net的编程思维,或是说C#吧.因为前几年一直在用PHP做站,现在用.net很不习惯,主要C#都依赖对控件.类的熟悉,不然很多功能都实现不了. 需求 最近做了一功能,从SQL Server ...

  7. http://www.cnblogs.com/nullcnb/p/3653262.html

    alert(hasPrototypeProperty(person,"name")); //false 来自原型 person1.name="mywei"; a ...

  8. OpenGL学习笔记0——安装库

    最近需要做一个基于Zigbee室内无线定位的系统,受到TI公司ZigBee Sensor Monitor软件的启发,打算用OpenGL来做一个3D显示空间内物体位置的程序.学习阶段选择VS2010+O ...

  9. source tree 推送错误解决

    fatal: The remote end hung up unexpectedly 出现这个问题是因为文件过大 解决办法: 打开git bash 输入git config --global http ...

  10. sencha touch+phonegap+node.js打包

    这讲我们来讲解下如何使用phonegapa创建项目环境并通过她们将sencha touch打包成app,这里我们只讲解打包android的apk,打包ios的过程有点类似,但是需要在mac环境下,最后 ...