使用JFinal对之前的系统进行重构,老系统使用了CKEditor+CKFinder编辑器,发现在web.xml中配置ConnectorServlet会被JFinalFilter过滤处理,而且使用JFinal之后也不想在web.xml中增加额外的配置,所以尝试的写了JFinal整合CKFinder的Handler,由于我对CKEditor+CKFinder不熟悉,可能整合的并不完整,这里只当参考,欢迎提供更好的解决方案~~
package com.jfinal.ext.handler;
import java.util.Enumeration;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ckfinder.connector.ConnectorServlet;
import com.hikvision.util.base.PropertiesKit;
import com.jfinal.handler.Handler;
public class CKFinderHandler extends Handler
{
private String visitPath = "/ckfinder/core/connector/java/connector";
private ConnectorServlet servlet = new ConnectorServlet();
private Properties properties;
public CKFinderHandler() {}
public CKFinderHandler(String cfgPath)
{
this.properties = PropertiesKit.loadPropertyFile(cfgPath);
}
public CKFinderHandler(String visitPath, String cfgPath)
{
this.visitPath = visitPath;
this.properties = PropertiesKit.loadPropertyFile(cfgPath);
}
@Override
public void handle(String target, final HttpServletRequest request, HttpServletResponse response, boolean[] isHandled)
{
if (target.startsWith(visitPath)) {
isHandled[0] = true;
try {
servlet.init(new ServletConfig()
{
@Override
public String getServletName() {return null; }
@Override
public ServletContext getServletContext() {return request.getSession().getServletContext(); }
@SuppressWarnings("rawtypes")
@Override
public Enumeration getInitParameterNames() {return null; }
@Override
public String getInitParameter(String name)
{
return properties.getProperty(name);
}
});
servlet.service(request, response);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
else {
nextHandler.handle(target, request, response, isHandled);
}
}
}
@Override
public void configHandler(Handlers me)
{
me.add(new CKFinderHandler("classes/ckfinder.properties"));
}
ckfinder.properties
debug=false
XMLConfig=/WEB-INF/config.xml
来源:oschina
链接:https://my.oschina.net/u/866950/blog/130183