Struts2的Action访问Spring的业务逻辑组件的两种策略

1、让spring管理控制器Action,并利用依赖注入为控制器注入业务逻辑组件

业务逻辑代码

package service;

public class MyService {

    public boolean validLogin(String username,String password){
        //简单的验证逻辑:用户名为张三且密码为123456则登录成功
        if("张三".equals(username)&&"123456".equals(password)){
            return true;
        }
        return false;
    }
}

控制器Action代码

package controller;

import com.opensymphony.xwork2.ActionSupport;

import service.MyService;

public class LoginAction extends ActionSupport{
    private String username;
    private String password;

    private MyService myService;

    //用于spring依赖注入
    public void setMyService(MyService myService) {
        this.myService = myService;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    public String execute(){
        if(myService.validLogin(getUsername(), getPassword())){
            return SUCCESS;
        }
        return ERROR;
    }

}

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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <bean id="myService" class="service.MyService"></bean>
    <!-- 注意:控制器Action的scope要设置为prototype -->
    <bean id="loginAction" class="controller.LoginAction" scope="prototype">
        <property name="myService" ref="myService"></property>
    </bean>

</beans>

struts.xml配置

<?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.i18n.encoding" value="UTF-8"></constant>
    <constant name="struts.devMode" value="true"></constant>
    <package name="default" extends="struts-default">
        <!-- 使用spring容器管理的Action控制器:此处class为spring容器中bean的id -->
        <action name="login" class="loginAction">
            <result name="error">/error.jsp</result>
            <result>/welcome.jsp</result>
        </action>
    </package>
</struts>

2、利用spring的自动装配方式,Action会自动重spring容器中获取所需的逻辑组件

业务逻辑和控制器Action代码一样,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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <!--这里只需要配置业务逻辑的bean-->
    <bean id="myService" class="service.MyService"></bean>
</beans>

struts.xml

<?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.i18n.encoding" value="UTF-8"></constant>
    <constant name="struts.devMode" value="true"></constant>
    <package name="default" extends="struts-default">
        <!-- 这里class属性不再是spring中bean的id,而是控制器的全类名-->
        <action name="login" class="controller.LoginAction">
            <result name="error">/error.jsp</result>
            <result>/welcome.jsp</result>
        </action>
    </package>
</struts>

这里写图片描述
从控制台中可以看出struts2-spring-plugin-2.3.24.1.jar使用自动装配的方式是通过名字来自动装配的
通过设置struts.objectFactory.spring.autoWire常量改变spring插件的自动装配方式,该常量接受如下几个值:

  • name:使用byName自动装配
  • type:使用byType自动装配
  • auto:spring插件自动检测需要哪种装配方式
  • constructor:与type类似,区别是使用构造器来构造注入所需的参数,而不是使用setter方式注入

详细请参看《轻量级java ee 企业应用实战》李刚

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值