Eclipse或Myeclipse中搭建KindEditor環境並測試

       最近在學習KindEditor,按照官方手冊在Eclipse中搭建KindEditor的環境時出現了問題,後來解決了,在我的博客裏寫出來希望對其他的人有幫助。

最後實現的效果應該是這樣的:

(1)可以輸入文字,可以上傳文件,可以上傳照片。



(2)點擊提交以後是跳轉顯示頁面,顯示出剛纔的文字,圖片,並可以下載文檔




實現步驟


1、在KindEditor的官方網站下載http://www.kindsoft.net/ 下載插件。我使用的是KindEditor-4.1.7


2、解壓下載的KindEditor,因爲我是用Java編程,所以可以刪除asp、asp.net、php、examples文件夾。


3、創建一個JavaEE工程,將目錄kindeditor-4.1.7\jsp\lib下面的三個Jar包json_simple-1.1.jar、commons-io-1.4.jar、commons-fileupload-1.2.1.jar


4、如圖所示,在src下面的webapp複製粘貼剛纔解壓的kindeditor-4.1.7,有的Eclipse會報錯有的Eclipse不會報錯,原因是因爲裏面的JavaScript的書寫規範不符合要求,但是不會影響到程序的正常運行。


除去報錯的方法。在項目的物理路徑下面找到文件 .project,用記事本打開,或者用UE打開。找到下面一段代碼並刪除。接着將Eclipse關閉,並重新打開。然後此時找到報錯的文件,全選-->剪切-->保存-->粘貼-->保存

		<buildCommand>
			<name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
			<arguments>
			</arguments>
		</buildCommand>

<nature>org.eclipse.wst.jsdt.core.jsNature</nature>

5、在目錄src\main\webapp\kindeditor-4.1.7\jsp下找到文件demo.jsp。將demo.jsp賦值粘貼到src\main\webapp\下面方便測試。並將demo.jsp的代碼進行修改。

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
String htmlData = request.getParameter("content1") != null ? request.getParameter("content1") : "";
%>
<!doctype html>
<html>
<head>
	<meta charset="utf-8" />
	<title>KindEditor JSP</title>
	<link rel="stylesheet" href="kindeditor-4.1.7/themes/default/default.css" />
	<link rel="stylesheet" href="kindeditor-4.1.7/plugins/code/prettify.css" />
	<script charset="utf-8" src="kindeditor-4.1.7/kindeditor.js"></script>
	<script charset="utf-8" src="kindeditor-4.1.7/lang/zh_CN.js"></script>
	<script charset="utf-8" src="kindeditor-4.1.7/plugins/code/prettify.js"></script>
	<script>
		KindEditor.ready(function(K) {
			var editor1 = K.create('textarea[name="content1"]', {
				cssPath : 'kindeditor-4.1.7/plugins/code/prettify.css',
				uploadJson : 'kindeditor-4.1.7/jsp/upload_json.jsp',
				fileManagerJson : 'kindeditor-4.1.7/jsp/file_manager_json.jsp',
				allowFileManager : true,
				afterCreate : function() {
					var self = this;
					K.ctrl(document, 13, function() {
						self.sync();
						document.forms['example'].submit();
					});
					K.ctrl(self.edit.doc, 13, function() {
						self.sync();
						document.forms['example'].submit();
					});
				}
			});
			prettyPrint();
		});
	</script>
</head>
<body>
	<form name="example" method="post" action="show.jsp">
		<textarea name="content1" cols="100" rows="8" style="width:700px;height:200px;visibility:hidden;"><%=htmlspecialchars(htmlData)%></textarea>
		<br />
		<input type="submit" name="button" value="提交內容" /> (提交快捷鍵: Ctrl + Enter)
	</form>
</body>
</html>
<%!
private String htmlspecialchars(String str) {
	str = str.replaceAll("&", "&");
	str = str.replaceAll("<", "<");
	str = str.replaceAll(">", ">");
	str = str.replaceAll("\"", """);
	return str;
}
%>

6、再建立起一個顯示頁面show.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
String htmlData = request.getParameter("content1") != null ? request.getParameter("content1") : "";
%>
<!doctype html>
<html>
<head>
	<meta charset="utf-8" />
	<title>KindEditor JSP</title>
</head>
<body>
	<%=htmlData%>
</body>
</html>

7、修改src\main\webapp\kindeditor-4.1.7\jsp目錄下的 upload_json.jsp和file_manager_json.jsp文件中一段代碼


String saveUrl  = request.getContextPath() + "attached/"; 給成

String saveUrl  = request.getContextPath() + "/attached/";多加一個斜槓


8、並在webapp下面創建文件夾attached


9、啓動工程,輸入網址:localhost:8080/kindeditor/demo.jsp 進行測試




發佈了38 篇原創文章 · 獲贊 119 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章