加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Spring整合strus2简单应用总结

发布时间:2020-12-15 01:10:56 所属栏目:大数据 来源:网络整理
导读:本身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.aopallianc

本身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实体类

package com.strus.entity;

public class Person {

    private String username;
    void setUsername(String username) {
        this.username = username;
    }
    
     say(){
        System.out.println("hellow," +username);
    }
}

dao

 com.strus.dao;

 PersonDao {

    public String save() {
        System.out.println("PersonDao save ..........");
        return "success!";
    }
}

action

 com.strus.action;

import com.strus.dao.PersonDao;

 PersonServer {

     PersonDao personDao;
     setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }
     execute(){
        System.out.println("PersonServer execute 。。。。。");
        personDao.save();
    }
}

4、创建Spring的bean.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    bean id="person" class="com.strus.entity.Person">
        property name="username" value="xiaoqiang"></property</bean="personDao"="com.strus.dao.PersonDao" scope="prototype"<!-- 注意: 在 IOC 容器中配置 Struts2 的 Action 时,需要配置 scope 属性,其值必须为 prototype -->
    ="personServer"="com.strus.action.PersonServer" ref="personDao">
beans>

?

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

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

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

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 ="struts.enable.DynamicMethodInvocation"="false" />
    ="struts.devMode"="true" />

    package ="default" namespace="/" extends="struts-default">
        
          
            Spring 整合 Struts2 时,在 Struts2 中配置的 Spring 的 Action 的 class 需要指向 IOC 容器中该 bean 的 id,
        -->
        action ="person-save"="personServer">
            result>/success.jspaction>
        
    >

6、web.xml创建

web-app xmlns:xsi 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">
    

     配置 Spring 配置文件的名称和位置 context-paramparam-name>contextConfigLocationparam-value>classpath:bean.xml>

         启动 IOC 容器的 ServletContextListener listenerlistener-class>org.springframework.web.context.ContextLoaderListener>        
         配置 Struts2 的 Filter filterfilter-name>struts2filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-mappingurl-pattern>/*>
web-app>

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

index.jsp

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

success.jsp

<%@ page language=">
<title>Insert title here</title>
</head>
<body>
    <h4>Success Page</h4>
</body>
</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 时,其值必须为 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);
}

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读