首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

施用spring aop拦截struts2中的action,发现不太好拦截传入action参数了

2013-06-19 
使用spring aop拦截struts2中的action,发现不太好拦截传入action参数了action代码:package com.zjlolife.a

使用spring aop拦截struts2中的action,发现不太好拦截传入action参数了
action代码:

package com.zjlolife.action;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;

import com.zjlolife.domain.Item;
import com.zjlolife.domain.User;
import com.zjlolife.service.UserService;

public class UserAction {
    @Resource
private UserService userService;

private User user;

private Item item;

public User getUser() {
System.out.println("getUser");
return user;
}


public void setUser(User user) {
System.out.println("setUer");

this.user = user;
}


public Item getItem() {
return item;
}


public void setItem(Item item) {
this.item = item;
}


public String login() {
User loginUser = userService.login(user);
        if(loginUser!=null) {
        ServletActionContext.getRequest().getSession().setAttribute("user", loginUser);
        return "success";
        }
        return "login";
}

public String addItem() {
userService.addItem(item);
return "success";
}
}


aop切面类代码:采用注解

package com.zjlolife.util;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Aspect
public class LogInterceptor {
@Pointcut("execution (* com.zjlolife.action.UserAction.*(..))")
    private void anyMethod() {};
    
    @Pointcut("execution (* com.zjlolife.action.UserAction.*et*(..))")
    private void noInterceptorMethod() {};
    
    @Pointcut("anyMethod()&&!noInterceptorMethod()")
    private void InterceptorMethod() {};
    @Around("InterceptorMethod()")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
    LoggerUtils.setLogger(LogInterceptor.class);
    //Logger logger = LoggerFactory.getLogger(LogInterceptor.class);
    System.out.println("intercepyot");
    Class className = pjp.getTarget().getClass();
Object[] args = pjp.getArgs();
String methodName = pjp.getSignature().getName();
Object result = null;
try {
result = pjp.proceed();
}
catch(Throwable t) {
//如果出现异常就记录异常日志
LoggerUtils.log(className, args, methodName,t);
throw new Throwable(t);
}
//如果操作正常记录日志
LoggerUtils.log(className, args, methodName);
return result;
}
}



我现在拦截的action的login方法,想过在切面类中使用ServletActionContext这个工具获取request参数,惨是request参数同时又包刮好多关于请求头的参数,感觉不太好,有没有好的解决方法?


[解决办法]

ActionContext actionContext = ServletActionContext.getContext();
Map<String,Object> map = actionContext.getParameters();


已解决,革命还得靠自己

热点排行