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 ...
随机推荐
- Js 验证中文字符长度
代码如下: //Oracle Varchar2 一个中文对应3个Byte,所以用3个x替换 var commentValue = commentValue.replace(/[^\x00-\xff]/ ...
- AngularJS的MVC中C的实现
Angular的MVC中C的实现 注:在controller操作Dom效率是很低的,我们使用封装的指令去操作Dom
- Appium的安装
APPium的官网地址为:http://appium.io,在官网可以看到安装步骤如下:
- TEX学习笔记
整理在这里, 方便以后容易查找. 毕竟每个tex的模板有些不一样. Beamer: Latex beamer 学习总结 http://blog.sina.com.cn/s/blog_6cf921f30 ...
- Nginx图片剪裁模块探究
http://nginx.org/en/docs/http/ngx_http_image_filter_module.html http://cwtea.blog.51cto.com/4500217/ ...
- Windows 驱动程序工具包中的头文件
MSDN原文:https://msdn.microsoft.com/zh-cn/library/windows/hardware/ff554695(v=vs.85).aspx Windows 驱动程序 ...
- localStorage、sessionStorage在无痕模式下被禁用
在移动web开发中,经常会使用到localStorage去缓存一些数据,一般情况下,我们只需要按照下面的代码去使用就不会有 问题. if(window.localStorage){ localStor ...
- WCF配置
服务端 <system.serviceModel> <services> <service name="WCF.Homedo.Service.Cache.Ser ...
- arc下dealloc得处理
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- w3svc服务启动 不了,错误 1068:依赖服务或组件无法启动
win10系统,装了iis就是启动不了,报错误 1068:依赖服务或组件无法启动. 各种实验无法使用,最后如下方法解决 运行命令regedit,打开注册表编辑器,进入:HKEY_LOCAL_MACHI ...