步骤:
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