項目地址:https://github.com/724888/lightnote_newjavascript
首先我參考了一個ueditor的demo。css
下載地址:http://download.csdn.net/download/lookthatgirl/5651763html
demo的項目結構前端
導入工程後配置一下build path將jar配置好便可運行,固然可能存在error,解決便可。java
可是要將這個編輯器加入到我本身的頁面中時遇到了點麻煩。git
首先 問題一:程序員
maven配置;github
其中一些jar包有maven倉庫地址,而另一些並無。web
io,json,upload這些能夠直接搜到,不過ueditor就沒有了,面試
這裏貼上全部的dependency地址。
<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20140107</version> </dependency> <dependency> <groupId>org.webjars.bower</groupId> <artifactId>ueditor</artifactId> <version>1.4.3</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.2</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec --> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.9</version> </dependency>
其中ueditor是mavenrepository裏提供的地址。
若是不肯意配置pom.xml,也能夠直接把jar包加入到build path,同樣能夠運行。
其次 問題二:
官方提供的工程存在bug,主要有幾個問題,
1是json文件中有註釋,可用正則表達式的替換來刪除註釋
2是show.html文件中的error,緣由是&符號不能被識別,須要改爲&便可解決。
3是jar包缺失致使的一些import錯誤,將jar包配置好應該能夠解決。
問題三:
jsp文件中已配置好文字編輯器,可是沒法成功顯示。幾經測試,與jar包無關。
後終於找到其緣由,資源文件沒有讀取到。
這是個人工程結構
ueditor.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="java.util.*" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <% String apath = request.getContextPath(); String abasePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+apath+"/"; out.print(abasePath+"dassa"); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.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 src="<%=abasePath%>ueditor/ueditor.config.js"></SCRIPT> <SCRIPT type=text/javascript src="<%=abasePath%>ueditor/ueditor.all.js"></SCRIPT> </head> <body> <form action="save.jsp" method="post"> <TEXTAREA id=myEditor name="mycontent"></TEXTAREA> <SCRIPT type=text/javascript> var editor = new UE.ui.Editor(); editor.render("myEditor"); //1.2.4之後可使用一下代碼實例化編輯器 //UE.getEditor('myEditor') </SCRIPT> <input name="submit" value="提交" type="submit"> </form> </body> </html>
仔細檢查了幾回,發現js目錄也沒有寫錯,可是就是顯示不出編輯器。
而jsp中只包含了js文件,只多是js出現問題,因而我開始尋找資源文件配置的問題。
因爲我使用的是ssm框架,如下是個人相關配置文件:
web.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> 3 <display-name>lightnote</display-name> 4 <filter> 5 <filter-name>encodingFilter</filter-name> 6 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 7 <init-param> 8 <param-name>encoding</param-name> 9 <param-value>UTF-8</param-value> 10 </init-param> 11 </filter> 12 <filter-mapping> 13 <filter-name>encodingFilter</filter-name> 14 <url-pattern>/*</url-pattern> 15 </filter-mapping> 16 <listener> 17 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 18 </listener> 19 <listener> 20 <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> 21 </listener> 22 <context-param> 23 <param-name>contextConfigLocation</param-name> 24 <param-value>classpath:/spring-mybatis/spring-mybatis.xml</param-value> 25 </context-param> 26 27
//此處通過修改 54 55 56 <servlet> 57 <servlet-name>springmvc</servlet-name> 58 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 59 <init-param> 60 <param-name>contextConfigLocation</param-name> 61 <param-value>classpath:/springmvc-servlet/springmvc-servlet.xml</param-value> 62 </init-param> 63 <load-on-startup>1</load-on-startup> 64 </servlet> 65 <servlet-mapping> 66 <servlet-name>springmvc</servlet-name> 67 <url-pattern>/</url-pattern> 68 </servlet-mapping> 69 70 71 72 73 <context-param> 74 <param-name>log4jConfigLocation</param-name> 75 <param-value>classpath:/properties/log4j.properties</param-value> 76 </context-param> 77 <listener> 78 <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 79 </listener> 80 <welcome-file-list> 81 <welcome-file>index.jsp</welcome-file> 82 <welcome-file>login.jsp</welcome-file> 83 <welcome-file>ueditor.jsp</welcome-file> 84 85 86 </welcome-file-list> 87 <context-param> 88 <param-name>webAppRoot</param-name> 89 <param-value>lightnote.root</param-value> 90 </context-param> 91 </web-app>
全部資源都會被dispatcherserlvet攔截。
而且在spring配置文件中會對靜態資源文件進行處理。
spring-servlet.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 12 <!-- 啓用srping mvc註解 --> 13 <!-- <mvc:annotation-driven />開啓設置能夠替代 --> 14 <!-- <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> 15 <bean id="handlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> --> 16 <mvc:annotation-driven/> 17 <!-- 靜態資源 --> 18 <mvc:resources mapping="/images/**" location="/images/"/> 19 <mvc:resources mapping="/js/**" location="/js/" /> 20 <mvc:resources mapping="/css/**" location="/css/" /> 21 <!-- 攔截器 --> 22 <!-- <mvc:interceptors></mvc:interceptors> --> 23 <!-- 自動掃描的包,使Spring支持自動檢測組件,如註解的Controller --> 24 <context:component-scan base-package="com.ruanku.lightnote"/> 25 26 <!--視圖解析器: 定義視圖的前綴後綴 --> 27 <bean id="resourceView" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 28 <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 29 <property name="prefix" value="/WEB-INF/jsp/"/> 30 <property name="suffix" value=".jsp"/> 31 </bean> 32 33 34 </beans>
可是因爲spring配置文件只對固定的三個資源文件夾進行掃描。沒法掃描到其餘的文件夾。
因此jsp頁面才加載不到ueditor文件夾裏的靜態資源。
因而我作了如下修改:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> 3 <display-name>lightnote</display-name> 4 <filter> 5 <filter-name>encodingFilter</filter-name> 6 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 7 <init-param> 8 <param-name>encoding</param-name> 9 <param-value>UTF-8</param-value> 10 </init-param> 11 </filter> 12 <filter-mapping> 13 <filter-name>encodingFilter</filter-name> 14 <url-pattern>/*</url-pattern> 15 </filter-mapping> 16 <listener> 17 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 18 </listener> 19 <listener> 20 <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> 21 </listener> 22 <context-param> 23 <param-name>contextConfigLocation</param-name> 24 <param-value>classpath:/spring-mybatis/spring-mybatis.xml</param-value> 25 </context-param> 26 27 28 <servlet-mapping> 29 <servlet-name>default</servlet-name> 30 <url-pattern>*.css</url-pattern> 31 </servlet-mapping> 32 <servlet-mapping> 33 <servlet-name>default</servlet-name> 34 <url-pattern>*.gif</url-pattern> 35 </servlet-mapping> 36 <servlet-mapping> 37 <servlet-name>default</servlet-name> 38 <url-pattern>*.jpg</url-pattern> 39 </servlet-mapping> 40 <servlet-mapping> 41 <servlet-name>default</servlet-name> 42 <url-pattern>*.png</url-pattern> 43 </servlet-mapping> 44 <servlet-mapping> 45 <servlet-name>default</servlet-name> 46 <url-pattern>*.js</url-pattern> 47 </servlet-mapping> 48 49 50 <servlet-mapping> 51 <servlet-name>default</servlet-name> 52 <url-pattern>*.html</url-pattern> 53 </servlet-mapping> 54 55 56 <servlet> 57 <servlet-name>springmvc</servlet-name> 58 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 59 <init-param> 60 <param-name>contextConfigLocation</param-name> 61 <param-value>classpath:/springmvc-servlet/springmvc-servlet.xml</param-value> 62 </init-param> 63 <load-on-startup>1</load-on-startup> 64 </servlet> 65 <servlet-mapping> 66 <servlet-name>springmvc</servlet-name> 67 <url-pattern>/</url-pattern> 68 </servlet-mapping> 69 70 71 72 73 <context-param> 74 <param-name>log4jConfigLocation</param-name> 75 <param-value>classpath:/properties/log4j.properties</param-value> 76 </context-param> 77 <listener> 78 <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 79 </listener> 80 <welcome-file-list> 81 <welcome-file>index.jsp</welcome-file> 82 <welcome-file>login.jsp</welcome-file> 83 <welcome-file>ueditor.jsp</welcome-file> 84 85 86 </welcome-file-list> 87 <context-param> 88 <param-name>webAppRoot</param-name> 89 <param-value>lightnote.root</param-value> 90 </context-param> 91 </web-app>
增長
28 <servlet-mapping> 29 <servlet-name>default</servlet-name> 30 <url-pattern>*.css</url-pattern> 31 </servlet-mapping> 32 <servlet-mapping> 33 <servlet-name>default</servlet-name> 34 <url-pattern>*.gif</url-pattern> 35 </servlet-mapping> 36 <servlet-mapping> 37 <servlet-name>default</servlet-name> 38 <url-pattern>*.jpg</url-pattern> 39 </servlet-mapping> 40 <servlet-mapping> 41 <servlet-name>default</servlet-name> 42 <url-pattern>*.png</url-pattern> 43 </servlet-mapping> 44 <servlet-mapping> 45 <servlet-name>default</servlet-name> 46 <url-pattern>*.js</url-pattern> 47 </servlet-mapping>
在dispatcherservlet截獲請求以前會讓defaultservlet對靜態資源進行處理。
並把spring-servlet.xml中的靜態資源配置代碼刪除。
<mvc:resources mapping="/images/**" location="/images/"/>
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/css/**" location="/css/" />
這樣便可以讓jsp頁面訪問到其餘文件夾的靜態資源了。
因而百度富文本編輯器就可使用了!
微信公衆號【黃小斜】大廠程序員,互聯網行業新知,終身學習踐行者。關注後回覆「Java」、「Python」、「C++」、「大數據」、「機器學習」、「算法」、「AI」、「Android」、「前端」、「iOS」、「考研」、「BAT」、「校招」、「筆試」、「面試」、「面經」、「計算機基礎」、「LeetCode」 等關鍵字能夠獲取對應的免費學習資料。