JFinal 拦截器无法获取表单post提交的数据

freedomcat 发布于 2016/06/16 09:55
阅读 802
收藏 0

@JFinal 你好,想跟你请教个问题:JFinal 拦截器无法获取表单post提交的数据,而ajax提交的数据可以获取到,控制台能打印数据。

public class VisitLogInterceptor implements Interceptor {
public static final Logger LOG=Logger.getLogger(VisitLogInterceptor.class);

@Override
public void intercept(Invocation inv) {
try{
HttpSession session = inv.getController().getSession();
   HttpServletRequest request=inv.getController().getRequest();
   Map<String,String[]> paraMap=inv.getController().getParaMap();
String remoteIp=request.getRemoteAddr();
String controller=inv.getControllerKey();
String action=inv.getActionKey();
String username=(String) session.getAttribute(PermissionChecker.ADMIN_USERNAME);
Record record=new Record();
record.set("log_time", new Date());
record.set("action", action);
record.set("controller", controller);
record.set("username", username);
record.set("remote_ip", remoteIp);
record.set("filter",JsonKit.toJson(paraMap));
Db.use("jjb_log").save("jjb_visit_log",record);
}catch(Exception e){
LOG.error(e.getMessage(), e);
}finally{
inv.invoke();
}  
}


}

加载中
0
JFinal
JFinal

   两种最常见用法,选一种即可:

Controller c = inv.getController();
c.getPara(..);
c.getModel(...);
c.getBean(...);

   上面的方式是先从 inv 中获取 controller,剩下的代码与 action 中完全一样。

HttpServletRequest req = inv.getController().getRequest();
req.getParameter(...);
   上面是先获取 HttpSerlvetRequest 对象,剩下的代码与常规 servlet 项目一样。
JFinal
JFinal
回复 @freedomcat : getParaMap() 然后在 for 循环中迭代处理
freedomcat
freedomcat
我是想做通用的拦截,不到参数的具体名字,该怎么做
0
JFinal
JFinal

   不知道 paraName 的情况下,参考一下 jfinal Controller 的 keepPara 方法:

/**
 * Keep all parameter's value except model value
 */
public Controller keepPara() {
 Map<String, String[]> map = request.getParameterMap();
 for (Entry<String, String[]> e: map.entrySet()) {
  String[] values = e.getValue();
  if (values.length == 1)
   request.setAttribute(e.getKey(), values[0]);
  else
   request.setAttribute(e.getKey(), values);
 }
 return this;
}


0
freedomcat
freedomcat

引用来自“JFinal”的评论

   不知道 paraName 的情况下,参考一下 jfinal Controller 的 keepPara 方法:

/**
 * Keep all parameter's value except model value
 */
public Controller keepPara() {
 Map<String, String[]> map = request.getParameterMap();
 for (Entry<String, String[]> e: map.entrySet()) {
  String[] values = e.getValue();
  if (values.length == 1)
   request.setAttribute(e.getKey(), values[0]);
  else
   request.setAttribute(e.getKey(), values);
 }
 return this;
}


我换了,还是取不到值,不知道哪里有问题

public class VisitLogInterceptor implements Interceptor {
public static final Logger LOG=Logger.getLogger(VisitLogInterceptor.class);

@Override
public void intercept(Invocation inv) {
try{

Controller controller = inv.getController();
HttpServletRequest request = inv.getController().getRequest();
Map<String, String[]> map = request.getParameterMap();
HttpSession session = controller.getSession();
String remoteIp=request.getRemoteAddr();
String controllerKey=inv.getControllerKey();
String action=inv.getActionKey();
String username=(String) session.getAttribute(PermissionChecker.ADMIN_USERNAME);
Record record=new Record();
record.set("log_time", new Date());
record.set("action", action);
record.set("controller", controllerKey);
record.set("username", username);
record.set("remote_ip", remoteIp);
record.set("filter",JsonKit.toJson(map));
Db.use("jjb_log").save("jjb_visit_log",record);
}catch(Exception e){
LOG.error(e.getMessage(), e);
}finally{
inv.invoke();
}  
}


}


JFinal
JFinal
终极方法:单步调试
返回顶部
顶部