jfinal添加handler后页面访问不正常

有球必应 发布于 2016/05/09 13:06
阅读 484
收藏 0

想拦截出现404的请求,用拦截器无法捕获到Error404相关的异常,看网上说可以添加handler,来判断是否有对应的Action,添加了可以拦截到404的请求,但是也出现在了个问题,就是页面显示不正常,去掉自己写的Handler后又可以正常显示,帮忙看下我写的有什么不对的,配置如下

/**
* 配置处理器
*/
public void configHandler(Handlers me) {
// me.add(new ContextPathHandler("ctx_path"));
me.add(new TestHandler("ctx_path"));
}
原本的ContextPathHandle注释不注释结果都一样

我自己的Handler如下:

public class TestHandler extends Handler {

private String contextPathName;

public TestHandler() {
contextPathName = "CONTEXT_PATH";
}

public TestHandler(String contextPathName) {
if (StrKit.isBlank(contextPathName))
throw new IllegalArgumentException("contextPathName can not be blank.");
this.contextPathName = contextPathName;
}

public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {


// doHandler(target, response);
request.setAttribute(contextPathName, request.getContextPath());
nextHandler.handle(target, request, response, isHandled);
}


public void doHandler(String target, HttpServletResponse response) {
    String url[] = { target };
if (JFinal.me().getAction(target, url) == null) {
try {
    response.sendRedirect("/home");
} catch (IOException e) {
}
return;
}
}
}


加载中
0
理工男海哥
理工男海哥
public class JErrorRenderFactory extends FreeMarkerRender implements IErrorRenderFactory {
     
    private int errorCode;
     
    public JErrorRenderFactory() {
        super(null);
    }
 
    @Override
    public Render getRender(int errorCode, String view) {
        this.errorCode = errorCode;
        return this;
    }
 
    @Override
    public void render() {
          //do you render 
    }

    }

配置:

public void configConstant(Constants constants) {
         
        constants.setErrorRenderFactory(new JErrorRenderFactory());
         
}

Over!

0
理工男海哥
理工男海哥

或者参考:https://github.com/JpressProjects/jpress/blob/master/jpress/src/io/jpress/core/render/JErrorRenderFactory.java

配置文件:

https://github.com/JpressProjects/jpress/blob/master/jpress/src/io/jpress/core/JpressConfig.java

返回顶部
顶部