NutzCN Logo
精华 动作链可以用代码方式配置吗?
发布于 3220天前 作者 wendal 2086 次浏览 复制 上一个帖子 下一个帖子
标签:

写js很烦 --> 我只是原话复原某人的说法

8 回复

能, 自定义个ActionChainMaker实现就行

package net.wendal.nutzbook.mvc;

import java.util.ArrayList;
import java.util.List;

import org.nutz.integration.shiro.NutShiroProcessor;
import org.nutz.mvc.ActionChain;
import org.nutz.mvc.ActionChainMaker;
import org.nutz.mvc.ActionInfo;
import org.nutz.mvc.NutConfig;
import org.nutz.mvc.Processor;
import org.nutz.mvc.impl.NutActionChain;
import org.nutz.mvc.impl.processor.ActionFiltersProcessor;
import org.nutz.mvc.impl.processor.EncodingProcessor;
import org.nutz.mvc.impl.processor.FailProcessor;
import org.nutz.mvc.impl.processor.MethodInvokeProcessor;
import org.nutz.mvc.impl.processor.ModuleProcessor;
import org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor;
import org.nutz.mvc.impl.processor.ViewProcessor;

public class MyActionChainMaker implements ActionChainMaker {

	public ActionChain eval(NutConfig config, ActionInfo ai) {
		List<Processor> list = normalList();
		
		// 首先,在所有请求之前,插入2个
		list.add(0, new LogTimeProcessor());
		list.add(1, new DailyUniqueUsersProcessor());
		
		addBefore(list, ActionFiltersProcessor.class, new NutShiroProcessor());
		
		// 最后是专门负责兜底的异常处理器
		Processor error = new FailProcessor();
		return new NutActionChain(list, error, ai);
	}
	
	protected List<Processor> normalList() {
		List<Processor> list = new ArrayList<>();
		list.add(new UpdateRequestAttributesProcessor());
		list.add(new EncodingProcessor());
		list.add(new ModuleProcessor());
		list.add(new ActionFiltersProcessor());
		list.add(new MethodInvokeProcessor());
		list.add(new ViewProcessor());
		return list;
	}
	
	protected List<Processor> addBefore(List<Processor> list, Class<?> klass, Processor processor) {
		for (int i = 0; i < list.size(); i++) {
			if (klass.isAssignableFrom(list.get(i).getClass())) {
				list.add(i, processor);
				return list;
			}
		}
		return list;
	}
}

如何引用

@ChainBy(type=MyActionChainMaker.class, args={})
public void MainModule{}

这个实现超棒!

擦, 发现上面写的demo少了MethodInvokeProcessor

更正如下

	protected List<Processor> normalList() {
		List<Processor> list = new ArrayList<>();
		list.add(new UpdateRequestAttributesProcessor());
		list.add(new EncodingProcessor());
		list.add(new ModuleProcessor());
		list.add(new ActionFiltersProcessor());
		list.add(new AdaptorProcessor());
		list.add(new MethodInvokeProcessor());
		list.add(new ViewProcessor());
		return list;
	}

try了一下,发现有BUG会导致NPE
因为全部Processor都没有init
应改为:

public ActionChain eval(final NutConfig config, final ActionInfo ai) {
		List<Processor> list = normalList();

		addBefore(list, ActionFiltersProcessor.class, new NutShiroProcessor());

		Processor error = new KerboresFailProcessor();
		Lang.each(list, new Each<Processor>() {

			@Override
			public void invoke(int paramInt1, Processor processor, int paramInt2) throws ExitLoop, ContinueLoop, LoopException {
				try {
					processor.init(config, ai);
				} catch (Throwable e) {
					log.error(e);
				}
			}
		});
		try {
			error.init(config, ai);
		} catch (Throwable e) {
			e.printStackTrace();
		}
		return new NutActionChain(list, error, ai);
	}

哦... 我的锅

添加回复
请先登陆
回到顶部