SSH之struts1方法1--使用Spring的ActionSupport

本文介绍了一种在Struts2中整合Spring的方法,通过让Action继承自ActionSupport并利用getWebApplicationContext方法获取Spring上下文,进而实现对业务逻辑层的服务调用。此方案虽然简单直接,但存在耦合度高、违反IoC原则等不足。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

步骤:

1:Action继承org.springframework.web.struts.ActionSupport

2:在Action中使用ApplicationContext ctx = this.getWebApplicationContext() 获取spring上下文

3:通过ctx获取相应的bean

 

Action代码


public class LoginAction extends ActionSupport {
    private static final Logger logger = Logger.getLogger(LoginAction.class);

 

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
           HttpServletRequest request, HttpServletResponse response) throws Exception {
        logger.debug("【调用控制器】LoginAction");

 

        //获取Spring context, 然后获取bean
        ApplicationContext ctx = this.getWebApplicationContext();
        IOperatorService operatorService = (IOperatorService)ctx.getBean("operatorService");

 

       //获取操作员form
      
LoginForm loginForm = (LoginForm)form;
       Operator operator = new Operator();
       operator.setOperatorName(loginForm.getUsername());
       operator.setOperatorPass(loginForm.getPassword());

       //验证登陆

 


       if(operatorService.isExists(operator)){
           return mapping.findForward("success");
       }else{
           return mapping.findForward("fail");
       }
    }
}

 

 

 

优点:

1:简单

缺点:

1:耦合高

2:违反Ioc

3:无法使用多方法的Action

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值