Spring Aop拦截Action
aop能够拦截到action方法,但是对于struts2自动封装的参数却丢失了。当然在没有aop情况下是能够正常接收到参数的。代码如下:当我输入用户名和密码时,打印出的结果
环绕通知(前)execute
null--null
failer
然后倒腾了半天,查了点资料,在后面强制使用cglib代理,此时就能正确获取参数
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
这是为什么呢?
@Aspect
@Component
public class ActionInterceptor {
/**
* 声明切入点
*/
@Pointcut("execution(String com.uestc.babasport.action..*.*(..))")
public void anyMethod() {
}
@Around("anyMethod()")
public Object around(ProceedingJoinPoint joinPoint){
Object object = null;
try {
System.out.println("环绕通知(前)"+joinPoint.getSignature().getName());
object = joinPoint.proceed();
System.out.println(object.toString());
//System.out.println("环绕通知(后)");
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return object;
}
}
--------------------编程问答-------------------- 人工置顶
@Controller
public class EmployeeLoginAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String userName;
private String password;
private String tip;
@Resource
private EmployeeService employeeService;
//seter getter省略了
@Override
public String execute() throws Exception {
System.out.println(getUserName()+"--"+getPassword());
if (getUserName() != null && !"".equals(getUserName().trim())
&& getPassword() != null && !"".equals(getPassword().trim())) {
if(employeeService.validate(getUserName(), getPassword())){
ActionContext ac = ActionContext.getContext();
ac.put("message", "您已经成功登录");
ac.put("url", "default");
ac.getSession().put("employee", employeeService.find(Employee.class,getUserName().trim()));
setTip(null);
setUserName(null);
setPassword(null);
return "message";
}else{
tip = "您输入的用户名或者密码有误";
}
}
return "failer";
}
}
补充:Java , Java EE