zoukankan      html  css  js  c++  java
  • Spring整合Struts2

    • Spring整合Struts2的核心思想:Struts2的Action实例交给Spring的IOC容器装配管理。
    • 整合步骤:

                1. 导入开发包:

                     除了Struts2和Spring开发所必须的jar以外,还要导入以下三个jar,即:

                     image

              2. 编写测试代码

         (1). 配置web.xml文件

         Spring整合Struts2时,依然需要加载Spring的配置文件applicationContext.xml文件,从而是IOC容器能够管理应用中所需的bean,所以需要在web.xml文件中配置一个listener来完成加载Spring配置文件的功能。同时也要指定类路径下的Spring配置文件。

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" 
        xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
        
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </context-param>
        
        <filter>
            <filter-name>FilterDispatcher</filter-name>
            <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        </filter>
        
        <filter-mapping>
            <filter-name>FilterDispatcher</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    </web-app>

      (2). 编写Struts2的Action类

    package action;
    
    import service.LoginService;
    import bean.User;
    
    import com.opensymphony.xwork2.ModelDriven;
    
    public class LoginAction implements ModelDriven<User>{
        private LoginService loginService;
        private User user;
        
        public String execute() {
            boolean flag = loginService.login(user);
            if(flag) {
                return "success";
            } 
            return "fail";
        }
        
        public void setLoginService(LoginService loginService) {
            this.loginService = loginService;
        }
        
        public void setUser(User user) {
            this.user = user;
        }
        
        @Override
        public User getModel() {
            return user;
        }
    }

         (3). 配置Spring的配置文件applicationContext.xml

         在 IOC 容器中配置 Struts2 的 Action 时, 需要配置 scope 属性, 其值必须为 prototype。在Struts2中,客户端每次发送一个action请求都会产生一个Action实例。

    <?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="loginService" class="service.LoginService"></bean>
        <bean id="user" class="bean.User"></bean>
        
        <bean id="loginAction" class="action.LoginAction" scope="prototype">
            <property name="loginService" ref="loginService"></property>
            <property name="user" ref="user"></property>
        </bean>
    </beans>

          (4). 配置struts2.xml文件

          此时应该注意的是,struts.xml文件中的<action>元素的class属性将不再是该Action对应的实际类型了,而是合法的Java标示符即可,该标示符将和 applicationContext.xml 文件中的 Action 的 bean 的 id 属性值对应。

    <?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.enable.DynamicMethodInvocation" value="false" />
        <constant name="struts.devMode" value="true" />
        
        <package name="default" namespace="/user" extends="struts-default">
            <action name="login" class="loginAction" method="execute">
                <result name="success">/success.jsp</result>
                <result name="fail">/fail.jsp</result>
            </action>
        </package>
    </struts>

          注意:以上测试代码并不完整,请自行编写 LoginService 类中 login(User user) 的实现逻辑。

    • 我们再来思考此种整合方法的不足之处,当有多个Action的时侯,我们在配置struts.xml和applicationContext.xml文件时就比较繁琐,那么我们就有第二种整合方法避免了繁琐的配置。
    • 整合案例:

    (1) 编写Struts2的Action类

    package mobile_scm.test.action;
    
    import mobile_scm.test.service.TestService;
    
    public class TestAction {
        private String name;
        private TestService testService;//业务对象,需要Spring注入
    
        public void setTestService(TestService testService) {
            this.testService = testService;
        }
        public String execute() {
            boolean flag = testService.validateName(name);
            if(flag) {
                return "success";
            }
            return "fail";
        }
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }

    (2) 编写业务逻辑

    package mobile_scm.test.service;
    
    public interface TestService {
        public boolean validateName(String name);
    }
    package mobile_scm.test.service;
    
    public class TestServiceImpl implements TestService {
    
        @Override
        public boolean validateName(String name) {
            boolean flag = false;
            if("kate".equalsIgnoreCase(name)) {
                System.out.println("welcome " + name);
                flag = true;
            }
            return flag;
        }
    }

    (3) 编写配置文件:struts.xml文件不需要做任何变动,使用Struts2框架的时候,该怎么配置就怎么配置。在applicationContext.xml 中不需要注入Action实例,只需要注入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 name="struts.action.extension" value="do" />
        <constant name="struts.enable.DynamicMethodInvocation" value="false" />
        <constant name="struts.devMode" value="true" />
    
        <package name="default" namespace="/" extends="struts-default">
            <action name="showname" class="mobile_scm.test.action.TestAction">
                <result type="dispatcher" name="success">/WEB-INF/jsp/showname.jsp</result>
                <result name="fail">/WEB-INF/jsp/result.jsp</result>
            </action>
        </package>
    
    </struts>

    applicationContext.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">
        
        <!-- 注意:
            (1)id的值必须Action中需要注入的对象名字相同,本例中要和Action类TestAction中
            的private TestService testService属性名相同 
            (2)autowire="byName":通过属性名字自动注入
        -->
        <bean id="testService" autowire="byName"
            class="mobile_scm.test.service.TestServiceImpl"></bean>
    
    </beans>
  • 相关阅读:
    HashMap于Hashtable的区别
    redis分布式锁
    mybatis基本认识
    怎么获取硬件线程数,Future,创建线程
    查看端口号有什么在用
    javaScript 中的字符操作
    获取类里面的所有属性
    给Date赋值
    实现多人聊天
    客户端与服务器端执行报重置问题
  • 原文地址:https://www.cnblogs.com/shi-blog/p/4136067.html
Copyright © 2011-2022 走看看