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 aspect class code : using annotations
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 to intercept the login method I am now , the thought of using this tool ServletActionContext get request parameters in section class, miserable is the request parameters while scraping a lot of arguments on the request packet header , I feel very good, there is no good the solution ?
------ Solution ---------------------------------------- ----
ActionContext actionContext = ServletActionContext.getContext();
Map<String,Object> map = actionContext.getParameters();
has been resolved , the revolution had to rely on their own
------ For reference only -------------------------- -------------
come down to help ,
------ For reference only --------------- ------------------------
what looks like a real problem ......
------ For reference only - -------------------------------------
come, I have encountered this problem
------ For reference only -------------------------------------- -
Results posted , the solution does
?
没有评论:
发表评论