前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >jfinal 内置的handler功能

jfinal 内置的handler功能

作者头像
冷冷
发布2018-02-08 12:22:34
1.6K0
发布2018-02-08 12:22:34
举报
文章被收录于专栏:冷冷

jfinal内置了以个handler ,其中RoutesHandler ActionHandler 是框架核心依赖的我们不考虑。主要研究下边的5个


image
image

UrlSkipHandler

排除满足正则的url,跳过的url会被下一个filter处理

代码语言:javascript
复制
/**
 * Skip the excluded url request from browser.
 * The skiped url will be handled by next Filter after JFinalFilter
 * <p>
 * Example: me.add(new UrlSkipHandler(".+\\.\\w{1,4}", false));
 */
public class UrlSkipHandler extends Handler {
	
	private Pattern skipedUrlPattern;
	
	public UrlSkipHandler(String skipedUrlRegx, boolean isCaseSensitive) {
		if (StrKit.isBlank(skipedUrlRegx)) {
			throw new IllegalArgumentException("The para excludedUrlRegx can not be blank.");
		}
		skipedUrlPattern = isCaseSensitive ? Pattern.compile(skipedUrlRegx) : Pattern.compile(skipedUrlRegx, Pattern.CASE_INSENSITIVE);
	}
	
	public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {
		if (skipedUrlPattern.matcher(target).matches()) {
			return ;
		} else {
			next.handle(target, request, response, isHandled);
		}
	}
}

DruidStatViewHandler

Druid内置提供了一个StatViewServlet用于展示Druid的统计信息。 这个StatViewServlet的用途包括:

  • 提供监控信息展示的html页面
  • 提供监控信息的JSON API 注意:使用StatViewServlet,建议使用druid 0.2.6以上版本。
  1. 传统配置web.xml

StatViewServlet是一个标准的javax.servlet.http.HttpServlet,需要配置在你web应用中的WEB-INF/web.xml中。(密码,白名单等都可以在这里配置)

代码语言:javascript
复制
<servlet>
      <servlet-name>DruidStatView</servlet-name>
      <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>DruidStatView</servlet-name>
      <url-pattern>/druid/*</url-pattern>
  </servlet-mapping>

根据配置中的url-pattern来访问内置监控页面,如果是上面的配置,内置监控页面的首页是/druid/index.html

  1. jfinal提供优雅的方式
代码语言:javascript
复制
 /**
 * 配置处理器
 */
public void configHandler(Handlers me) {
    me.add(new DruidStatViewHandler("/druid",new DruidStatViewAuthImpl()));
}
    
public class DruidStatViewAuthImpl implements IDruidStatViewAuth{
    /**
     * 具体的认证逻辑实现
     * @param request
     * @return
     */
    @Override
    public boolean isPermitted(HttpServletRequest request) {
        return false;
    }
}

ContextPathHandler

提供全局的上下文路径

代码语言:javascript
复制
/**
 * 配置处理器
 */
public void configHandler(Handlers me) {
    me.add(new ContextPathHandler("ctx"));
}

那么在页面中就可以这么使用了
<img src="${BASE_PATH}/images/logo.png" />

代码实现很简单
public class ContextPathHandler extends Handler {
	
    private String contextPathName;
	
    public ContextPathHandler() {
	    contextPathName = "CONTEXT_PATH";
    }
	
    public ContextPathHandler(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) {
    	request.setAttribute(contextPathName, request.getContextPath());
    	next.handle(target, request, response, isHandled);
    }
}

ServerNameRedirect301Handler

代码语言:javascript
复制
301重定向到新服务器名称

/**
 * 配置处理器
 */
public void configHandler(Handlers me) {
    me.add(new ServerNameRedirect301Handler("http://localhost","http://127.0.0.1"));
}

访问:http://localhost/xxx  重定向到http://127.0.0.1/xxx

FakeStaticHandler

伪静态化配置

代码语言:javascript
复制
 /**
 * 配置处理器
 */
public void configHandler(Handlers me) {
    me.add(new FakeStaticHandler(".html"));
}

请求 localhost/xxx.html 实际对应的路由是 xxx
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • UrlSkipHandler
  • DruidStatViewHandler
  • ContextPathHandler
  • ServerNameRedirect301Handler
  • FakeStaticHandler
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档