Struts2和Spring的整合
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的整合的更多相关文章
- struts2+hibernate-jpa+Spring+maven 整合(1)
1.0.0 struts2 与 spring 的整合. 1.1.0 新建maven工程 , 编写pom.xml ,这里只需要简单的添加 一个组件就够了: 在myeclipse 生成的pom.xml 添 ...
- Struts2与Spring的整合
今天倒腾了半天,终于是把这个两个框架整合到一起了.还是要写一下总结,同时给大家一些帮助. 开发环境:myeclipse 9.0(不好用!)tomcat6.0 1.准备工作 需要导入的包:struts2 ...
- struts2+hibernate+spring简单整合且java.sql.SQLException: No suitable driver 问题解决
最近上j2ee的课,老师要求整合struts2+hibernate+spring,我自己其实早早地有准备弄的,现在都第9个项目了,无奈自己的思路和头绪把自己带坑了,当然也是经验问题,其实只是用myec ...
- struts2+hibernate-jpa+Spring+maven 整合(2)
1.修改pom.xml 1. 添加 slf4j-api <dependency> <groupId>org.slf4j</groupId> <artifac ...
- 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 ...
- java学习笔记(11) —— Struts2与Spring的整合
1.右键 项目名称 —— MyEclipse —— Add Spring Capabilities 2.选取 Copy checked Library contents to project fold ...
- Spring第四篇【Intellij idea环境下、Struts2和Spring整合】
前言 Spring的第二和第三篇已经讲解了Spring的基本要点了[也就是Core模块]-本博文主要讲解Spring怎么与Struts2框架整合- Struts2和Spring的整合关键点: acti ...
- Struts2与Spring整合
前言 本博文主要讲解Spring怎么与Struts2框架整合... Struts2和Spring的整合关键点: action对象交给Spring来创建 搭建环境 进入jar包 引入jar文件: 1)引 ...
- struts2与spring整合时需要注意的点
首先我们需要明白spring整合struts2中的什么东西,spring中的核心就是IOC和AOP,IOC是对象的容器,AOP是处理动态代理的;比如spring与hibernate整合时就要用到aop ...
随机推荐
- Mac下Virtual Box Host-Only网络配置
Mac下的虚拟机其实有很多,Parallels.VMware Fusion.Virtual Box都不错,Parallels是目前试过感觉最好的,Fusion装64位系统驱动支持似乎不完善,而且混合模 ...
- 在 Web 项目中应用 Apache Shiro
Apache Shiro 是功能强大并且容易集成的开源权限框架,它能够完成认证.授权.加密.会话管理等功能.认证和授权为权限控制的核心,简单来说,"认证"就是证明你是谁? Web ...
- pt-online-schema-change 修改主键导致数据删除失败的问题调查
pt-online-schema-change在线DDL工具可以做到DDL操作不锁表,不影响线上操作.对于线上超过100W的大表,一般情况下都用这个工具做DDL,最重要的考虑点还是“不影响线上操作” ...
- Inno Setup制作安装包的几个问题
1. 卸载时,如何判断应用程序是否运行 InnoSetup 提供变量AppMutex,用来保存应用程序的Mutex名称.现在很多应用程序都是唯一实例运行.这样避免配置文件被错误修改以及其他很多衍 ...
- SpringMvc JSON 406,吐血。。。。
解决方法一: 1.导入jackson-core-2.5.1.jar和jackson-databind-2.5.1.jar 2.Spring配置文件添加: 1 spring3为:org.springfr ...
- randomAccess接口
http://www.blogjava.net/lzqdiy/archive/2007/04/22/112578.html
- 如何使用Math对象快速计算数组中的最大值或最小值
Math 对象下包含 min() 和 max() 方法 用于确定一组数值中的最大值和最小值.这两个方法都可以接收任意多个数值参数. var max = Math.max(1,2,3,4,5,6); c ...
- 注释驱动的 Spring cache 缓存介绍
概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...
- 2、NASA HS3(Hurricane AND Server Storm Sentinel)
国内访问不到,但是通过ppt可以看到,数据支撑做的很到位,前台展示很炫. 气象领域WebGL应用最好案例的当之无愧啊.这篇全部贴图了. 来自为知笔记(Wiz)
- SQL归档
/* 作用:归档3个月前的短信发送记录 创建人:zhuxiang 日期:2012-10-30 */ ALTER Proc [dbo].[Sms_Job_History_Send] As Begin D ...