饭要一口一口吃,程序也要一步一步写,

很多看起来很复杂的东西最初都是很简单的

下面要整合struts和spring

spring就是我们的管家,原来我们费事费神的问题统统扔给她就好了

先写一个测试方法

  1. package com.hibernate;
  2. import static org.junit.Assert.*;
  3. import org.hibernate.SessionFactory;
  4. import org.junit.Test;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7. import com.beans.HelloWorldAction;
  8. public class SpringTest {
  9. private ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
  10. @Test
  11. public void test() {
  12. HelloWorldAction a=(HelloWorldAction)ac.getBean("helloWorldAction");
  13. System.out.println(a);
  14. }
  15. }

当然,现在肯定是不成立的

spring还不知道我们的helloWorldaction是什么

我们要先声明

在HelloWorldAction "public class HelloWorldAction"

上面添加

@Controller

@Scope("prototype")

现在上面的test已经可以运行了,

但也只是spring接管了HelloWorldAction类,并不代表spring与struts整合了

顶多算是一个spring的helloworld

接下来,我们需要在web.xml中加入监听器

新的web.xml如图所示

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  3. <display-name>oa</display-name>
  4.  
  5. <listener>
  6. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  7. </listener>
  8.  
  9. <context-param>
  10. <param-name>contextConfigLocation</param-name>
  11. <param-value>classpath:applicationContext*.xml</param-value>
  12. </context-param>
  13.  
  14. <filter>
  15. <filter-name>struts2</filter-name>
  16. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  17. </filter>
  18.  
  19. <filter-mapping>
  20. <filter-name>struts2</filter-name>
  21. <url-pattern>/*</url-pattern>
  22. </filter-mapping>
  23. <welcome-file-list>
  24. <welcome-file>index.html</welcome-file>
  25. <welcome-file>index.htm</welcome-file>
  26. <welcome-file>index.jsp</welcome-file>
  27. <welcome-file>default.html</welcome-file>
  28. <welcome-file>default.htm</welcome-file>
  29. <welcome-file>default.jsp</welcome-file>
  30. </welcome-file-list>
  31. </web-app>

然后在加入一个 struts2-spring-plugin-2.1.8.1.jar

将struts.xml中的action的class属性改为"helloWorldAction"

约定俗成,第一个字母小写,其他不变,写错了要报错

重启服务器,在浏览器中输入

localhost:8080/oa/test/helloworld.action

不报错就成功了

struts2,hibernate,spring整合笔记(4)--struts与spring的整合的更多相关文章

  1. Spring学习笔记(一)—— Spring介绍及入门案例

    一.Spring概述 1.1 Spring是什么 Spring是一个开源框架,是于2003年兴起的一个轻量级的Java开发框架, 由Rod Johnson 在其著作<Expert one on ...

  2. 【Spring学习笔记-MVC-15.1】Spring MVC之异常处理=404界面

    作者:ssslinppp       异常处理请参考前篇博客:<[Spring学习笔记-MVC-15]Spring MVC之异常处理>http://www.cnblogs.com/sssl ...

  3. 【Spring学习笔记-MVC-13.2】Spring MVC之多文件上传

    作者:ssslinppp       1. 摘要 前篇文章讲解了单文件上传<[Spring学习笔记-MVC-13]Spring MVC之文件上传>http://www.cnblogs.co ...

  4. 【Spring学习笔记-MVC-5】利用spring MVC框架,实现ajax异步请求以及json数据的返回

    作者:ssslinppp      时间:2015年5月26日 15:32:51 1. 摘要 本文讲解如何利用spring MVC框架,实现ajax异步请求以及json数据的返回. Spring MV ...

  5. Spring学习笔记(四)-- Spring事务全面分析

    通过本系列的文章对Spring的介绍,我们对Spring的使用和两个核心功能IOC.AOP已经有了初步的了解,结合我个人工作的情况,因为项目是金融系 统.那对事务的控制是不可缺少的.而且是很严格的控制 ...

  6. Spring学习笔记(四)—— Spring中的AOP

    一.AOP概述 AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.O ...

  7. Spring学习笔记4—流程(Spring Web Flow)

    Spring Web Flow是Spring框架的子项目,作用是让程序按规定流程运行. 1 安装配置Spring Web Flow 虽然Spring Web Flow是Spring框架的子项目,但它并 ...

  8. Spring学习笔记(二)Spring基础AOP、IOC

    Spring AOP 1. 代理模式 1.1. 静态代理 程序中经常需要为某些动作或事件作下记录,以便在事后检测或作为排错的依据,先看一个简单的例子: import java.util.logging ...

  9. spring学习笔记(二)spring中的事件及多线程

    我们知道,在实际开发中为了解耦,或者提高用户体验,都会采用到异步的方式.这里举个简单的例子,在用户注册的sh时候,一般我们都会要求手机验证码验证,邮箱验证,而这都依赖于第三方.这种情况下,我们一般会通 ...

随机推荐

  1. 关于auto和decltype

    auto会忽略顶层const,保留底层const ; const int* const p = &i; auto p2 = p; //p2是const int*,不是const int* co ...

  2. C文件函数总结

    1.fopen(打开文件) 表头文件 #include<stdio.h> 定义函数 FILE *fopen(const char * path,const char * mode); pa ...

  3. gdb调试 使用心得

    1: 对于在应用程序中加入参数进行调试的方法:   直接用 gdb app -p1 -p2 这样进行调试是不行的.   需要像以下这样使用:    #gdb app    (gdb) r -p1 -p ...

  4. php小知识点

    1.字符串可以里面的字符可以像数组一样访问,比如$s = "123";$s[1]就等于2,如果字符串为中文则会乱码,需要使用mb_substr进行截取: 2.php中的单引号(' ...

  5. mktime性能问题

    #include <time.h> int main() { for (int i = 0; i < 100000; ++i) { struct tm tm = {}; tm.tm_ ...

  6. bootstrap sr-only

    有时候 UI 上会出现一些仅供视觉识别的元素,比如说“汉堡包菜单按钮”,只有视力正常的人才能清楚辨识这些元素的作用.而残障人士,比如弱势或盲人是不可能知道这些视觉识别元素是什么的.他们上网使用的是屏幕 ...

  7. extjs中datefield组件的使用

    xtype: 'datefield', id: 'dateShangmfa', name: 'dateShangmfa', fieldLabel: '日期',//设置标签文本 editable: fa ...

  8. javascript写的ajax请求

    <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...

  9. php xcache 配置 使用 (转载)

    xcache的使用与配置 一.安装Xcache # wget http://xcache.lighttpd.net/pub/Releases/1.3.0/xcache-1.3.0.tar.gz # t ...

  10. C#解析JSON几种方式-整理

    一.什么是JSON JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition ...