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

被折叠的 条评论
为什么被折叠?



