jfinal 内置的handler功能

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


image

UrlSkipHandler

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

/**
 * 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中。(密码,白名单等都可以在这里配置)

<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提供优雅的方式
 /**
 * 配置处理器
 */
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

提供全局的上下文路径

/**
 * 配置处理器
 */
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

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

伪静态化配置

 /**
 * 配置处理器
 */
public void configHandler(Handlers me) {
    me.add(new FakeStaticHandler(".html"));
}

请求 localhost/xxx.html 实际对应的路由是 xxx

转载于:https://my.oschina.net/giegie/blog/837439

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值