jfinal内置了以个handler ,其中RoutesHandler ActionHandler 是框架核心依赖的我们不考虑。主要研究下边的5个
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以上版本。
- 传统配置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
- 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