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. Flexigrid从对象中加载数据

    (有问题,在找…………) Flexigrid是用来动态加载数据的一种比较好(老)的Jquery表插件,然后有些时候,我们需要其从本地或者jQuery对象中加载数据,比如有这么个需求,页面显示中有两个表 ...

  2. AngularJS---表达式

    AngularJS的表达式是放在{{}}里面,用{{ }}符号将一个变量绑定到$scope上. angularJS中的表达式有如下特点: 1.只能在其所属作用域内部 所有的表达式都在其所属的作用域内部 ...

  3. Mysql5.5源码安装步骤笔记记录

    1.cmake软件的安装wget https://cmake.org/files/v3.5/cmake-3.5.0-rc3.tar.gztar xf cmake-3.5.0.tar.gzcd cmak ...

  4. mssql整理

    select charindex( 'a ', 'bcad ') 1 删除女性数据2.SELECT * FROM Group2 where PATINDEX('%[吖-做]%',[Nick])=0 找 ...

  5. 如何用iframe在网页中插入另一个网页的一部分内容,做成页中页

    <html><head></head><body><h1>这是一段引用的内容!!!</h1><div style=&quo ...

  6. Oracle 游标示例,带异常处理

    Oracle游标示例一则,带异常处理. DECLARE CURSOR c_dl IS SELECT ID, NSRSBH, WSPZXH, ZXYY_DM, HZRQ, SWJG_DM, GXSJ F ...

  7. msqlserver 千万级别单表数据去掉重复记录使用临时表

    由于上周末小写把数据数据重复写入数据库,没办法,得去重! 最新使用的语句: use data set nocount ondelete DoRecordProperty from( select TI ...

  8. 【HOW】用PowerShell脚本修改用户配置文件属性显示次序

    首先将如下脚本保存为PowerShell文件,如:ReorderUserProfileProperty.ps1. 在执行此脚本时,如果不输入任何参数,将列出所有用户配置文件属性的名称和显示次序:如果只 ...

  9. 转 @html.ActionLink的几种参数格式

    一 Html.ActionLink("linkText","actionName") 该重载的第一个参数是该链接要显示的文字,第二个参数是对应的控制器的方法, ...

  10. AX 2012 template table use in Query

    queryRuntmp = new QueryRun(querytmp); queryRuntmp.setRecord(arAgingForLeasingTmp); while(queryRuntmp ...