天天看點

百度UEditor的介紹和圖檔上傳的使用(java)

UEditor是由百度web前端研發部開發所見即所得富文本web編輯器,并且是基于BSD協定的開源産品,允許自由使用和修改,使用起來十分友善。如果你是用來輸出編排好格式的文章或者其他文本,使用UEditor絕對是一個很不錯的選擇。

UEditor使用起來十分簡單,然而java版本在struts2跟原生的兩種情況下又有點不一樣。首先說一下建立原生java項目(不包含任何架構)的配置吧。
           

不包含任何架構:

步驟一:

先到UEditor官網([UEditor](http://ueditor.baidu.com/website/download.html))下載下傳UEditor1.4.3_jsp版本。
           
百度UEditor的介紹和圖檔上傳的使用(java)

下載下傳完成之後解壓,複制到你新建立的 java web項目的 WebContent 的目錄下。剛複制到項目中的時候可能會出現錯誤,你隻需要把該項目的編碼設定成 utf8 錯誤就會沒有了。

步驟二:

建立一個index.jsp

百度UEditor的介紹和圖檔上傳的使用(java)

正确引入js檔案之後,再在後面加上

<script type="text/javascript">
    UE.getEditor('myEditor2');
</script>
           

注意:jsp中的

<script id="myEditor2" name="content" type="text/plain">
    這裡寫你的初始化内容
</script>
           

id=”myEditor2” 一定要跟

<script type="text/javascript">
    UE.getEditor('myEditor2');
</script>
           

中的myEditor2相同,否則不能顯示編輯器。

步驟三:

百度UEditor的介紹和圖檔上傳的使用(java)
百度UEditor的介紹和圖檔上傳的使用(java)

把 imageUrlPrefix 對應的參數修改成你項目的根目錄,預設是空的。

步驟四:

百度UEditor的介紹和圖檔上傳的使用(java)

修改ueditor.config.js中的 window.UEDITOR_HOME_URL,

window.UEDITOR_HOME_URL = “/xxxx/xxxx/”;

也就是以斜杠開頭的形如”/myProject/ueditor/”這樣的路徑。

現在,你運作index.jsp檔案百度編輯器應該就會出現了。而且圖檔上傳也沒問題。

還有一點要注意的,就是當你上傳圖檔的時候在檔案夾中是看不到的,但是他已經上傳到指定的檔案夾的了,不知道是為什麼,我現在還搞不明白。

**

使用了struts2的UEditor

**

首先第一步,配置跟上面是完全一樣的。

有一點非常重要,就是當你要上傳圖檔的時候你會發現,上傳不了,說找不到資源。原因是struts2中的攔截器把所有檔案都攔截了,需要把controller.jsp檔案過濾掉,不給攔截器攔截,這樣才能實作上傳功能。

這是預設的:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd http://java.sun.com/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee" id="WebApp_9" version="2.4">
  <display-name>Struts Blank</display-name>

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
   <url-pattern>*.action</url-pattern>
  </filter-mapping>
  </web-app>
           

修改後:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd http://java.sun.com/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee" id="WebApp_9" version="2.4">
  <display-name>Struts Blank</display-name>

 <filter>
    <filter-name>UeditorFilter</filter-name>
    <filter-class>com.filter.UeditorFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>UeditorFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
   <url-pattern>*.action</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
           

定義一個過濾器:

package com.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();
        System.out.println(url);
        try{  
            if (url.contains("controller.jsp")) {  
                System.out.println("123");
                chain.doFilter(req, res);  
            } else {  
                System.out.println("11111111111111");
                super.doFilter(req, res, chain);  
            }  
        }catch(Exception e){  
            e.printStackTrace();  
        }  
    }  
 }  
           

最後struts2上傳檔案的功能也完成了。