本身strus2没接触过,所以这块学的一知半解,正常不整合的还没学(接着学)

step:

1、创建web工程

2、在/WEB-INF/lib引入jar包

asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-fileupload-1.3.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
commons-logging-1.1.3.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
log4j-1.2.17.jar
ognl-3.0.6.jar
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
spring-jdbc-4.0.0.RELEASE.jar
spring-orm-4.0.0.RELEASE.jar
spring-tx-4.0.0.RELEASE.jar
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar
struts2-core-2.3.15.3.jar
struts2-spring-plugin-2.3.15.3.jar   红色底纹为必须加入jar,有它才能整合
xwork-core-2.3.15.3.jar

3、编写测试代码

person实体类

  1. package com.strus.entity;
  2.  
  3. public class Person {
  4.  
  5. private String username;
  6. public void setUsername(String username) {
  7. this.username = username;
  8. }
  9.  
  10. public void say(){
  11. System.out.println("hellow," +username);
  12. }
  13. }

dao

  1. package com.strus.dao;
  2.  
  3. public class PersonDao {
  4.  
  5. public String save() {
  6. System.out.println("PersonDao save ..........");
  7. return "success!";
  8. }
  9. }

action

  1. package com.strus.action;
  2.  
  3. import com.strus.dao.PersonDao;
  4.  
  5. public class PersonServer {
  6.  
  7. private PersonDao personDao;
  8. public void setPersonDao(PersonDao personDao) {
  9. this.personDao = personDao;
  10. }
  11. public void execute(){
  12. System.out.println("PersonServer execute 。。。。。");
  13. personDao.save();
  14. }
  15. }

4、创建Spring的bean.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <bean id="person" class="com.strus.entity.Person">
  6. <property name="username" value="xiaoqiang"></property>
  7. </bean>
  8. <bean id="personDao" class="com.strus.dao.PersonDao" scope="prototype"></bean>
  9. <!-- 注意: 在 IOC 容器中配置 Struts2 的 Action 时, 需要配置 scope 属性, 其值必须为 prototype -->
  10. <bean id="personServer" class="com.strus.action.PersonServer">
  11. <property name="personDao" ref="personDao"></property>
  12. </bean>
  13. </beans>

5、拷贝strus.xml到src下,与bean.xml同级文件

我也不清楚为啥要拷贝这个文件,学strus2时候我估计就明白咋回事了

刚学不懂得先记着哈哈,先说要修改的位置,个人觉得就是action标签的很重要啦

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5.  
  6. <struts>
  7.  
  8. <constant name="struts.enable.DynamicMethodInvocation" value="false" />
  9. <constant name="struts.devMode" value="true" />
  10.  
  11. <package name="default" namespace="/" extends="struts-default">
  12.  
  13. <!--
  14. Spring 整合 Struts2 时, 在 Struts2 中配置的 Spring 的 Action 的 class 需要指向 IOC 容器中该 bean 的 id,
  15. -->
  16. <action name="person-save" class="personServer">
  17. <result>/success.jsp</result>
  18. </action>
  19.  
  20. </package>
  21.  
  22. </struts>

6、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" 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.  
  4. <!-- 配置 Spring 配置文件的名称和位置 -->
  5. <context-param>
  6. <param-name>contextConfigLocation</param-name>
  7. <param-value>classpath:bean.xml</param-value>
  8. </context-param>
  9.  
  10. <!-- 启动 IOC 容器的 ServletContextListener -->
  11. <listener>
  12. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  13. </listener>
  14. <!-- 配置 Struts2 的 Filter -->
  15. <filter>
  16. <filter-name>struts2</filter-name>
  17. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  18. </filter>
  19.  
  20. <filter-mapping>
  21. <filter-name>struts2</filter-name>
  22. <url-pattern>/*</url-pattern>
  23. </filter-mapping>
  24. </web-app>

7、创建测试,index.jsp和success.jsp

index.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10.  
  11. <a href="person-save">Person Save</a>
  12.  
  13. </body>
  14. </html>

success.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <h4>Success Page</h4>
  11. </body>
  12. </html>

编译运行如下:

点击:Person Save

思路总结:

1. Spring 如何在 WEB 应用中使用 ?

1). 需要额外加入的 jar 包:

spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar

2). Spring 的配置文件, 没有什么不同

3). 如何创建 IOC 容器 ?

①. 非 WEB 应用在 main 方法中直接创建
②. 应该在 WEB 应用被服务器加载时就创建 IOC 容器:

在 ServletContextListener#contextInitialized(ServletContextEvent sce) 方法中创建 IOC 容器.

③. 在 WEB 应用的其他组件中如何来访问 IOC 容器呢 ?

在 ServletContextListener#contextInitialized(ServletContextEvent sce) 方法中创建 IOC 容器后, 可以把其放在
ServletContext(即 application 域)的一个属性中.

④. 实际上, Spring 配置文件的名字和位置应该也是可配置的! 将其配置到当前 WEB 应用的初始化参数中较为合适.

4). 在 WEB 环境下使用 Spring

①. 需要额外加入的 jar 包:

spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar

②. Spring 的配置文件, 和非 WEB 环境没有什么不同

③. 需要在 web.xml 文件中加入如下配置:

<!-- 配置 Spring 配置文件的名称和位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<!-- 启动 IOC 容器的 ServletContextListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

2. Spring 如何整合 Struts2 ?

1). 整合目标 ? 使 IOC 容器来管理 Struts2 的 Action!

2). 如何进行整合 ?

①. 正常加入 Struts2

②. 在 Spring 的 IOC 容器中配置 Struts2 的 Action
注意: 在 IOC 容器中配置 Struts2 的 Action 时, 需要配置 scope 属性, 其值必须为 prototype

<bean id="personAction"
class="com.atguigu.spring.struts2.actions.PersonAction"
scope="prototype">
<property name="personService" ref="personService"></property>
</bean>

③. 配置 Struts2 的配置文件: action 节点的 class 属性需要指向 IOC 容器中该 bean 的 id

<action name="person-save" class="personAction">
<result>/success.jsp</result>
</action>

④. 加入 struts2-spring-plugin-2.3.15.3.jar

3). 整合原理: 通过添加 struts2-spring-plugin-2.3.15.3.jar 以后, Struts2 会先从 IOC 容器中
获取 Action 的实例.

if (appContext.containsBean(beanName)) {
o = appContext.getBean(beanName);
} else {
Class beanClazz = getClassInstance(beanName);
o = buildBean(beanClazz, extraContext);
}

Spring整合strus2简单应用总结的更多相关文章

  1. Spring整合MyBatis(简单登录Demo)

    SpringMvc简单整合(登录模块) 1.1 整合思路 1.SqlSessionFactory对象应该放到spring容器中作为单例存在. 2.传统dao的开发方式中,应该从spring容器中获得s ...

  2. spring整合strus2的Hellowworld

    比较笨,看了三遍才能理解敲对并正确运行: step: 1.建立web工程( Dynamic Web project)一定要勾上创建web.xml 2.导入jar包 这个就比较坑了,我查了有半个小时才查 ...

  3. jedis与spring整合及简单的使用RedisTemplate操作

    整理一下redis与spring的整合.以及使用redisTemplate.首先是要导入spring所需要的jar.当然还有 jedis-2.1.0.jar,commons-pool-1.5.4.ja ...

  4. Spring Security4.X 简单实例介绍

    简介 本例子采用的是SpringMVC.SpringSecurity和Spring整合的简单使用 使用gradle搭建的项目(gradle比maven更加便捷),可以自行了解 web.xml配置 &l ...

  5. spring+springMVC+mybatis简单整合

    spring+springMVC+mybatis简单整合, springMVC框架是spring的子项目,所以框架的整合方式为,spring+Mybatis或springMVC+mybatis. 三大 ...

  6. 简单探讨spring整合mybatis时sqlSession不需要释放关闭的问题

    https://blog.csdn.net/RicardoDing/article/details/79899686 近期,在使用spring和mybatis框架编写代码时,sqlSession不需要 ...

  7. Spring整合JDBC实现简单的增删改

    Spring整合JDBC实现简单的增删改: 1.导入Spring的包和数据库的驱动包: 2.选择一个数据源(dbcp和C3P0) 3.导入数据源的包(这里我们使用dbcp) <span styl ...

  8. spring整合apache-shiro的简单使用

    这里不对shiro进行介绍,介绍什么的没有意义 初学初用,不求甚解,简单使用 一.导入jar包(引入坐标) <!--shiro和spring整合--> <dependency> ...

  9. 手写Mybatis和Spring整合简单版示例窥探Spring的强大扩展能力

    Spring 扩展点 **本人博客网站 **IT小神 www.itxiaoshen.com 官网地址****:https://spring.io/projects/spring-framework T ...

随机推荐

  1. 【模板】Treap

    Treap,又称树堆,是一种通过堆性质来维持BST平衡的数据结构.具体体现在对于树上每一个点来说,既有BST维护的值,又有一个堆维护的随机生成的值.维护平衡性的办法是根据堆维护的值的相对大小关系进行左 ...

  2. sscanf,sprintf 通过字符串与其它类型进行方便快捷的输入和输出

    http://c.biancheng.net/cpp/html/296.html 头文件:#include <stdio.h> sscanf()函数用于从字符串中读取指定格式的数据,其原型 ...

  3. 【tools】vim删除命令

    x 删除当前光标下的字符dw 删除光标之后的单词剩余部分.d$ 删除光标之后的该行剩余部分.dd 删除当前行. c 功能和d相同,区别在于完成删除操作后进入INSERT MODEcc 也是删除当前行, ...

  4. js模块化的两种规范AMD和CMD

    AMD 规范在这里:https://github.com/amdjs/amdjs-api/wiki/AMDCMD 规范在这里:https://github.com/seajs/seajs/issues ...

  5. Angular开发小笔记

    一.父组件怎么覆盖子组件的样式呢 1./deep/(不建议这么做,以后angular会取消,因为这样写不利于组件的独立性) 在父组件的scss里面写: :host{ 子组件名 /deep/ label ...

  6. CentOS6.x下源码安装MySQL5.5

    1. 更新yum源:http://www.cnblogs.com/vurtne-lu/p/7405931.html 2. 卸载原有的mysql数据库 [root@zabbix ~]# yum -y r ...

  7. go通过swig封装、调用c++共享库的技术总结

    go通过swig封装.调用c++共享库的技术总结 @(知识记录) 1 简介 最近在研究golang,希望能对目前既有的python服务做一些优化,这些服务目前已经占用了6-7台机器.选择golang的 ...

  8. 不同数据库下的web.config中数据库连接字符串

    <connectionStrings> <add name="OADBConnectionString" connectionString="Data ...

  9. MySQL5.7主从复制配置

    1 my.cnf文件 配置 binlog_format = ROW log_bin_trust_function_creators=1 log-error = /usr/local/mysql/dat ...

  10. emacs(考场+平时)配置方案

    考场配置: ;;在配置后面会对语句逐一解释的 (global-set-key (kbd "C-z") 'undo) (global-set-key (kbd "RET&q ...