JFinal整合CKFinder

早过忘川 提交于 2019-12-05 05:52:30

    使用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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!