javaWeb项目,后台如何去取response对象中的值

yanxin9210 长沙学院 2015-12-01 06:21:04
在使用spring aop的自定义注解开发系统日志功能,需求是要监控操作时间、操作人、模块、动作、请求ip、请求浏览器、操作结果。

前几个值都可以从request和session中获取,而操作结果就必须要从response中获取。


现在的问题是:我已经在过滤器中取到了response,并且传给切点类了,但是不知道如何去取response里面的success值。

后台是spring+spring mvc+hibernate,前台是Extjs5

后台controller代码
@Override
@RequestMapping(value = "/saveDepartment", method = {RequestMethod.POST, RequestMethod.GET})
@SystemLogAfter //自定义注解
public void doSave(Department entity, HttpServletRequest request, HttpServletResponse response) throws IOException {
ExtJSBaseParameter parameter = ((ExtJSBaseParameter) entity);
// 查找所有父节点id相同(即同级节点)结果集
List<Department> departmentList = departmentService.queryByProerties("parentId", entity.getParentId());
System.out.println("entity:" + entity.getDepId());
System.out.println("entity:" + entity.getName());
boolean isExist = false;
// 遍历结果集
if (departmentList.size() > 0) {
for (Department department : departmentList) {
// 查找新增项或修改项的组织机构名是否与其他同级机构同名
System.out.println(department.getDepId());
System.out.println(department.getName());
System.out.println(department.getName().equals(entity.getName()));
System.out.println(department.getDepId().equals(entity.getDepId()));
if (department.getName().equals(entity.getName()) && !department.getDepId().equals(entity.getDepId())) {
isExist = true;
break;
//return;
}
}
}

if (!isExist) {
if (CMD_EDIT.equals(parameter.getCmd())) {
departmentService.update(entity);
} else if (CMD_NEW.equals(parameter.getCmd())) {
departmentService.persist(entity);
}
parameter.setCmd(CMD_EDIT);
parameter.setSuccess(true); //操作成功
}
else{
parameter.setSuccess(false); //操作失败
}
writeJSON(response, parameter); //生成json数据是用的jackson框架
}


前台js可以获取到response中的success值
Ext.Ajax.request({
url : appBaseUri + '/sys/department/saveDepartment',
params : {
cmd : vals['cmd'],
depId : vals['depId'],
name : vals['name'],
parentId : vals['parentId'],
manager : vals['manager'],
tel : vals['tel'],
sortId : vals['sortId'],
memo : vals['memo']
},
method : "POST",
success : function(response) {
if (response.responseText != '') {
var res = Ext.JSON.decode(response.responseText); //获取response中的json数据
if (res.success) {
globalObject.msgTip('操作成功!');
Ext.getCmp('departmentmanagementgrid').getStore().reload();
Ext.getCmp('departmentTree').getStore().reload();
} else {
globalObject.errTip('当前部门名称已存在!');
}
}
},
failure : function(response) {
globalObject.errTip('操作失败!');
}
});


切点类
@Aspect  
@Component
public class SystemLogAfterAspect {

@Resource
private SysLogService sysLogService;

@Resource
private AuthorityService authorityService;

// Controller层切点
@Pointcut("@annotation(com.djbh.aop.SystemLogAfter)")
public void controllerAspect() {
}

/**
* 后置通知 用于拦截Controller层记录用户的操作
*
* @param joinPoint 切点
*/
@After("controllerAspect()")
public void doAfter(JoinPoint joinPoint) {

//使用SystemContextUtils工具类获取request response session
HttpServletRequest request = HttpContextUtils.getRequest();
HttpServletResponse response = HttpContextUtils.getResponse();
HttpSession session = HttpContextUtils.getSession();

// 读取session中的用户
SysUser sysUser = (SysUser)session.getAttribute(Constant.SESSION_SYS_USER);

// 读取cmd值,cmd = new 或 edit,用来判断是新增保存还是修改保存
String cmd = request.getParameter("cmd");
String url = request.getRequestURI();
String contextPath = request.getContextPath() + "/";
// 请求的模块
String model = null;
// 请求的动作
String action = null;
System.out.println(url);
if (StringUtils.isNotBlank(url)) {
url = url.replace(contextPath, "");
// 正则表达式
Pattern p = Pattern.compile("[/]+");
// 将url字符串按/分割成字符串数组
String[] urls = p.split(url);
for (int i = 0; i < urls.length; i++) {
System.out.println(i+":" + urls[i]);
}
// 组合需要格式的字符串
StringBuffer sbUrl = new StringBuffer(urls[0]).append(".").append(urls[1]);
System.out.println(sbUrl);
Authority authority = authorityService.getByProerties("url", sbUrl.toString());

model = authority == null ? "其他" : authority.getMenuName();

if (urls[2].startsWith("get")) {
action = "查询(获取)";
}
else if (urls[2].startsWith("save") && "new".equals(cmd)) {
action = "新增";
}
else if (urls[2].startsWith("save") && "edit".equals(cmd)) {
action = "修改";
}
else if (urls[2].startsWith("delete")) {
action = "删除";
}
else if (urls[2].startsWith("login")) {
action = "登录";
}
else if (urls[2].startsWith("logout")) {
action = "注销";
}
else {
action = "其他";
}
}

// 请求的IP
String ip = request.getRemoteAddr();

// 请求的浏览器
String webBrower = request.getHeader("User-Agent");

try {
// *========控制台输出=========*//
System.out.println("=====后置通知开始=====");
System.out.println("用户名:" + sysUser.getUserName());
System.out.println("姓名:" + sysUser.getRealName());
System.out.println("请求IP:" + ip);
System.out.println("浏览器:" + webBrower);
System.out.println("请求方法:"
+ (joinPoint.getTarget().getClass().getName() + "."
+ joinPoint.getSignature().getName() + "()"));
System.out.println("url:" + url);
System.out.println("模块:" + model);
System.out.println("动作:" + action);
System.out.println("cmd:" + cmd);

// *========数据库日志=========*//
SysLog sysLog = new SysLog();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sysLog.setLogTime(sdf.format(new Date()));
sysLog.setUserName(sysUser.getUserName());
sysLog.setRealName(sysUser.getRealName());
sysLog.setIp(ip);
sysLog.setWebBrower(webBrower);
sysLog.setModel(model);
sysLog.setAction(action);

// 保存数据库
sysLogService.persist(sysLog);
System.out.println("=====后置通知结束=====");
} catch (Exception e) {
// 记录本地异常日志
// logger.error("==前置通知异常==");
// logger.error("异常信息:{}", e.getMessage());
}
}
}


我在切点类中获得了response对象,怎么去取controller中的parameter的success值呢?请高人指点啊!
...全文
7002 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
pzahah 2016-12-09
请问下: //使用SystemContextUtils工具类获取request response session HttpServletRequest request = HttpContextUtils.getRequest(); HttpServletResponse response = HttpContextUtils.getResponse(); HttpSession session = HttpContextUtils.getSession(); 是怎么实现的?
回复
yanxin9210 2015-12-06
已经通过将操作结果放到request中实现了该功能
回复
SunnyKandy1224 2015-12-02
切入点在controller层上面,而即使你在执行完成controller代码,response对象还是没有完全执行完成。
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2015-12-01 06:21
社区公告
暂无公告