如何使用过滤器 或者动作链 对所有的请求 的状态码进行判断
14 回复
@Filters(@By(type = MyFilter.class,args = {"500.html"}))
@Modules
@IocBy(type=ComboIocProvider.class, args={
"*anno", "com.lx.demo",
"*tx", // 事务拦截 aop
"*async"}) // 异步执行aop
@SetupBy(MainSetupBy.class)
@ChainBy(type = MyActionChainMaker.class,args = {})
public class MainModule {
}
public class MyFilter implements ActionFilter {
private String path;
public MyFilter(String path) {
this.path = path;
}
@Override
public View match(ActionContext actionContext) {
System.err.println(actionContext.getResponse().getStatus());
return actionContext.getResponse().getStatus() == 200 ? null : new ServerRedirectView(path);
}
}
package com.lx.demo.chain;
import com.lx.demo.processor.MyProcessor;
import org.nutz.mvc.*;
import org.nutz.mvc.impl.NutActionChain;
import org.nutz.mvc.impl.processor.*;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2018/6/13.
*/
public class MyActionChainMaker implements ActionChainMaker {
@Override
public ActionChain eval(NutConfig config, ActionInfo ai) {
// 提醒: config可以获取ioc等信息, ai可以获取方法上的各种配置及方法本身
// 正常处理的列表
List<Processor> list = new ArrayList<>();
list.add(new UpdateRequestAttributesProcessor()); // 设置base/msg等内置属性
list.add(new EncodingProcessor()); // 设置编码信息@Encoding
list.add(new ModuleProcessor()); // 获取入口类的对象,从ioc或直接new
list.add(new ActionFiltersProcessor()); // 处理@Filters
list.add(new AdaptorProcessor()); // 处理@Adaptor
list.add(new MethodInvokeProcessor()); // 执行入口方法
list.add(new ViewProcessor()); // 对入口方法进行渲染@Ok
list.add(new MyProcessor());
for (Processor p : list) {
try {
p.init(config, ai);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
// 最后是专门负责兜底的异常处理器,这个处理器可以认为是全局异常处理器,对应@Fail
Processor error = new FailProcessor();
try {
error.init(config, ai);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return new NutActionChain(list, error, ai);
}
}
http://nutzbook.wendal.net/dev_prepare/action_chain.html 看文档, 是定义动作链的配置文件, 不是自定义MyActionChainMaker
package com.lx.demo.processor;
import org.nutz.http.Response;
import org.nutz.mvc.ActionContext;
import org.nutz.mvc.impl.processor.AbstractProcessor;
import org.nutz.mvc.view.HttpServerResponse;
import javax.servlet.http.HttpServletResponse;
/**
* Created by Administrator on 2018/6/13.
*/
public class MyProcessor extends AbstractProcessor {
@Override
public void process(ActionContext ac) throws Throwable {
HttpServletResponse response = ac.getResponse();
System.out.println(response.getStatus());
}
}
添加回复
请先登陆