ueditor1_4_3-utf8-jsp配置及自定义,结合struts2

时间:2023-01-10 13:10:56

1、ueditor.config.js 中,第一行

window.UEDITOR_HOME_URL = "/bss/js/ueditor/";

指定根目录

2、自定义Filter

package com.baidu.ueditor.filter;

import javax.servlet.FilterChain;  
import javax.servlet.ServletRequest;  
import javax.servlet.ServletResponse;  
import javax.servlet.http.HttpServletRequest;  
  
import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter;  
  
public class UEditorFilter extends StrutsPrepareAndExecuteFilter{  
  
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) {  
        HttpServletRequest request = (HttpServletRequest) req;  
        String url = request.getRequestURI();  
        try{  
            if (url.length() > 10 && "imageUp.jsp".equals(url.substring(url.length()-11))) {//umeditor  
                chain.doFilter(req, res);  
            }if (url.contains("/bss/js/ueditor/jsp/controller.jsp")) {  //ueditor
                chain.doFilter(req, res);  
            } else {  
                super.doFilter(req, res, chain);  
            }  
        }catch(Exception e){  
            e.printStackTrace();  
        }  
    }  
 }  

3、配置web.xml

    <filter> 
        <filter-name>StrutsPrepareFilter</filter-name> 
        <filter-class> 
            com.baidu.ueditor.filter.UEditorFilter
        </filter-class> 
    </filter> 

    <filter-mapping> 
        <filter-name>StrutsPrepareFilter</filter-name> 
        <url-pattern>/*</url-pattern>
    </filter-mapping> 

经过以上配置富文本编辑器 就可以用了
4、使用示例

<html>
<head>
    <title>My JSP 'ueditordemo.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <script type="text/javascript" charset="utf-8" src="/bss/js/ueditor/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="/bss/js/ueditor/ueditor.all.min.js"> </script>
    <!--建议手动加在语言,避免在ie下有时因为加载语言失败导致编辑器加载失败-->
    <!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型,比如你在配置项目里配置的是英文,这里加载的中文,那最后就是中文-->
    <script type="text/javascript" charset="utf-8" src="/bss/js/ueditor/lang/zh-cn/zh-cn.js"></script>
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

</head>
  
<body>
    <script id="editor" type="text/plain" style="width:1024px;height:500px;"></script>
    <script type="text/javascript">
        //实例化编辑器
        var ue = UE.getEditor('editor');
    </script>
</body>
</html>


5、自定义返回信息

在com.baidu.ueditor.define.AppInfo.java 中

public static final int FULL_DISK = 801;

//磁盘空间已满
put(AppInfo.FULL_DISK, "\u8d85\u51fa\u78c1\u76d8\u7a7a\u95f4\u989d\u5ea6\uff0c\u4e0d\u5141\u8bb8\u4e0a\u4f20\u3002");

使用时:

//磁盘空间已满,返回信息
return new BaseState(false, AppInfo.FULL_DISK);

6、自定义配置属性并读取

在config.json自定义属性

/* 自定义配置属性 add by xxx */
"userConfigPath": false,

在com.baidu.ueditor.ConfigManager.java中读取配置

    public Map<String, Object> getConfig ( int type ) {
        
        Map<String, Object> conf = new HashMap<String, Object>();
        String savePath = null;
        
        
        conf.put( "savePath", savePath );
        conf.put( "rootPath", this.rootPath );
        conf.put( "userConfigPath", this.jsonConfig.getBoolean( "userConfigPath" ));//add by xxx
        
        return conf;
        
    }


7、自定义文件存储路径

文件存储路径可以在配置文件中修改,也可以自定义

com.baidu.ueditor.upload.BinaryUploader.java

savePath = PathFormat.parse(savePath, originFileName);//返回到页面中的图片url

String physicalPath = (String) conf.get("rootPath") + savePath;//图片存储的物理路径

根据自己的需求修改这两项的值,可以完成自定义。