配置动作链

动作链,很强大,但之前很少解释,导致很少人用到.

在conf文件夹中,新建一个文件夹mvc,新增一个配置文件叫nutzbook-mvc-chain.js, 内容如下

  1. var chain={
  2. "default" : {
  3. "ps" : [
  4. "net.wendal.nutzbook.mvc.LogTimeProcessor",
  5. "org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor",
  6. "org.nutz.mvc.impl.processor.EncodingProcessor",
  7. "org.nutz.mvc.impl.processor.ModuleProcessor",
  8. "!org.nutz.integration.shiro.NutShiroProcessor",
  9. "org.nutz.mvc.impl.processor.ActionFiltersProcessor",
  10. "org.nutz.mvc.impl.processor.AdaptorProcessor",
  11. "org.nutz.mvc.impl.processor.MethodInvokeProcessor",
  12. "org.nutz.mvc.impl.processor.ViewProcessor"
  13. ],
  14. "error" : 'org.nutz.mvc.impl.processor.FailProcessor'
  15. }
  16. };

这个动作链配置文件,与nutz默认的配置文件,仅仅多了一行 net.wendal.nutzbook.mvc.LogTimeProcessor

所以,新建一个类叫LogTimeProcessor, 包自然就是net.wendal.nutzbook.mvc咯, 继承org.nutz.mvc.impl.processor.AbstractProcessor

  1. package net.wendal.nutzbook.mvc;
  2. import javax.servlet.http.HttpServletRequest;
  3. import org.nutz.lang.Stopwatch;
  4. import org.nutz.log.Log;
  5. import org.nutz.log.Logs;
  6. import org.nutz.mvc.ActionContext;
  7. import org.nutz.mvc.impl.processor.AbstractProcessor;
  8. public class LogTimeProcessor extends AbstractProcessor {
  9. private static final Log log = Logs.get();
  10. @Override
  11. public void process(ActionContext ac) throws Throwable {
  12. Stopwatch sw = Stopwatch.begin();
  13. try {
  14. doNext(ac);
  15. } finally {
  16. sw.stop();
  17. if (log.isDebugEnabled()) {
  18. HttpServletRequest req = ac.getRequest();
  19. log.debugf("[%4s]URI=%s %sms", req.getMethod(), req.getRequestURI(), sw.getDuration());
  20. }
  21. }
  22. }
  23. }

然后打开MainModule这个类,加入一个注解

  1. @ChainBy(args="mvc/nutzbook-mvc-chain.js")

启动tomcat并登陆登出,可以看到有类似的log输出

  1. 2015-04-09 19:46:59,140 net.wendal.nutzbook.mvc.LogTimeProcessor.process(LogTimeProcessor.java:27) DEBUG - [POST]URI=/nutzbook/user/login 30ms

这个类的主要目的是演示动作链的配置, 因为druid的监控页面有更详尽的统计数据了(以前可没有druid,呵呵)