效果:
1、kindeditor下載下傳:http://www.kindsoft.net/down.php
2、a:kindeditor安裝:解壓下載下傳好的檔案,把整個檔案夾拷貝到工程目錄下面,如:/WebRoot/kindeditor
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="kindeditor/kindeditor.js" charset="utf-8" ></script>
<title>Kindeditor執行個體</title>
<script type="text/javascript">
KE.show({
id:'content', //下面的textarea的ID
height:'300px',
resizeMode:0,
skinType:'default',
autoOnsubmitMode:'true',
imageUploadJson:'system/topic-uploadImage.action' //上傳圖檔的伺服器處理位址,3.4版本以後支援
});
</script>
</head>
<body>
Kindeditor執行個體:
<textarea id="content" name="topicVO.content" cols="60" rows="25"></textarea>
<input type="button" id="publish_btn" value="送出">
</body>
</html>
圖檔上傳處理:
public class TopicAction{
/**
* 圖檔對象
*/
private File imgFile;
/**
* 圖檔寬度
*/
private String imgWidth;
/**
* 圖檔高度
*/
private String imgHeight;
/**
* 圖檔對齊方式
*/
private String align;
/**
* 圖檔标題
*/
private String imgTitle;
public File getImgFile() {
return imgFile;
}
public void setImgFile(File imgFile) {
this.imgFile = imgFile;
}
public String getImgWidth() {
return imgWidth;
}
public void setImgWidth(String imgWidth) {
this.imgWidth = imgWidth;
}
public String getImgHeight() {
return imgHeight;
}
public void setImgHeight(String imgHeight) {
this.imgHeight = imgHeight;
}
public String getAlign() {
return align;
}
public void setAlign(String align) {
this.align = align;
}
public String getImgTitle() {
return imgTitle;
}
public void setImgTitle(String imgTitle) {
this.imgTitle = imgTitle;
}
/**
* 上傳圖檔
* @return
*/
public String uploadImage(){
UserVO user = (UserVO) session.getAttribute(Constants.USER_KEY);
String imageName = user.getId()+"_topic_"+new Date().getTime()+Math.random()*10000+".jpg";
File dirPath =new File(request.getSession().getServletContext().getRealPath("/")+"\\upload\\articleimage\\");
//如果檔案夾不存在,建立它
if(!dirPath.exists()){
dirPath.mkdirs();
}
//檔案存放路徑
String newPath = dirPath+"\\"+imageName;
//上傳檔案
try {
copy(imgFile, newPath);
} catch (Exception e1) {
e1.printStackTrace();
}
String id = "content";
String url = "http://" + request.getServerName() + ":"+ request.getServerPort() + request.getContextPath()
+"/upload/articleimage/" + imageName;
String border = "0";
String result =
"<script type=\"text/javaScript\">parent.KE.plugin[\"image\"].insert(\""
+ id
+ "\",\""
+ url
+ "\",\""
+ imgTitle
+ "\",\""
+ imgWidth
+ "\",\""
+ imgHeight
+ "\",\""
+ border + "\""
+");</script>";
PrintWriter out = null;
try {
out = encodehead(request, response);
} catch (IOException e) {
e.printStackTrace();
}
out.write(result);
out.close();
return null;
}
/**
* 拷貝檔案
*
* @param upload檔案流
* @param newPath新檔案路徑和名稱
* @throws Exception
*/
private void copy(File upload, String newPath) throws Exception {
FileOutputStream fos = new FileOutputStream(newPath);
FileInputStream fis = new FileInputStream(upload);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
fis.close();
}
/**
* Ajax輔助方法 擷取 PrintWriter
* @return
* @throws IOException
* @throws IOException
* request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
*/
private PrintWriter encodehead(HttpServletRequest request,HttpServletResponse response) throws IOException{
request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
return response.getWriter();
}
}
注意:上面action中的imgFile、imgWidth、imgHeight、align、imgTitle名字不要改,因為在struts2中需要這裡跟form表單中的name屬性一緻,如果你非要改,那麼也得把kindeditor中的image.html中的屬性改一下,最好不要改源碼!
附錄:http://www.kindsoft.net/doc.php?cmd=config
調用KE.show或KE.init時可以設定以下參數。
-
id
TEXTAREA輸入框的ID,必須設定。
資料類型:String
-
items
配置編輯器的工具欄,其中"-"表示換行,"|"表示分隔符。
資料類型:Array
預設值:
['source', '|', 'fullscreen', 'undo', 'redo', 'print', 'cut', 'copy', 'paste', 'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright', 'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript', 'superscript', '|', 'selectall', '-', 'title', 'fontname', 'fontsize', '|', 'textcolor', 'bgcolor', 'bold', 'italic', 'underline', 'strikethrough', 'removeformat', '|', 'image', 'flash', 'media', 'table', 'hr', 'emoticons', 'link', 'unlink', '|', 'about']
-
width
編輯器的寬度,可以設定px或%,比TEXTAREA輸入框樣式表寬度優先度高。
資料類型:String
預設值:TEXTAREA輸入框的寬度
注: 3.2版本開始支援。
-
height
編輯器的高度,隻能設定px,比TEXTAREA輸入框樣式表高度優先度高。
資料類型:String
預設值:TEXTAREA輸入框的高度
注: 3.2版本開始支援。
-
minWidth
資料類型:Int
指定編輯器最小寬度,機關為px。
預設值:200
-
minHeight
資料類型:Int
指定編輯器最小高度,機關為px。
預設值:100
-
filterMode
資料類型:Boolean
true時過濾HTML代碼,false時允許輸入任何代碼。
預設值:false
注: 3.4以前版本的filterMode預設值為true。
-
htmlTags
指定要保留的HTML标記和屬性。哈希數組的key為HTML标簽名,value為HTML屬性數組,"."開始的屬性表示style屬性。
資料類型:Object
預設值:
注:filterMode為true時有效。3.4版本開始屬性可設定style,保留所有inline樣式。{ font : ['color', 'size', 'face', '.background-color'], span : ['style'], div : ['class', 'align', 'style'], table: ['class', 'border', 'cellspacing', 'cellpadding', 'width', 'height', 'align', 'style'], 'td,th': ['class', 'align', 'valign', 'width', 'height', 'colspan', 'rowspan', 'bgcolor', 'style'], a : ['class', 'href', 'target', 'name', 'style'], embed : ['src', 'width', 'height', 'type', 'loop', 'autostart', 'quality', 'style', 'align', 'allowscriptaccess', '/'], img : ['src', 'width', 'height', 'border', 'alt', 'title', 'align', 'style', '/'], hr : ['class', '/'], br : ['/'], 'p,ol,ul,li,blockquote,h1,h2,h3,h4,h5,h6' : ['align', 'style'], 'tbody,tr,strong,b,sub,sup,em,i,u,strike' : [] }
-
resizeMode
2或1或0,2時可以拖動改變寬度和高度,1時隻能改變高度,0時不能拖動。
資料類型:Int
預設值:2
-
skinType
風格類型,default或tinymce。
資料類型:String
預設值:default
注:3.4以前版本和3.4.1版本開始預設值為default,3.4版本的預設值為oxygen。
-
wyswygMode
可視化模式或代碼模式
資料類型:Boolean
預設值:true
-
cssPath
指定編輯器iframe document的CSS,用于設定可視化區域的樣式。
資料類型:String或Array
預設值:空
注:3.4.1版本開始可指定多個CSS檔案。例如:cssPath : ['a.css', 'b.css']
-
skinsPath
指定編輯器的skins目錄,skins目錄存放風格的css檔案和gif圖檔。
資料類型:String
預設值:KE.scriptPath + 'skins/'
-
pluginsPath
指定編輯器的plugins目錄。
資料類型:String
預設值:KE.scriptPath + 'plugins/'
-
minChangeSize
undo/redo文字輸入最小變化長度,當輸入的文字變化小于這個長度時不會添加到undo堆棧裡。
資料類型:Int
預設值:5
-
loadStyleMode
true時自動加載編輯器的CSS。
資料類型:Boolean
預設值:true
注: 3.4版本開始支援。
-
urlType
改變站内本地URL,可設定空、relative、absolute、domain。空為不修改URL,relative為相對路徑,absolute為絕對路徑,domain為帶域名的絕對路徑。
資料類型:String
預設值:空
注: 3.4版本開始支援,3.4.1版本開始預設值為空。
-
newlineTag
設定回車換行标簽,可設定p、br。
資料類型:String
預設值:br
注: 3.4版本開始支援。
-
afterCreate
設定編輯器建立後執行的回調函數。
資料類型:Function
預設值:無
-
afterDialogCreate
設定彈出浮動框建立後執行的回調函數。
資料類型:Function
預設值:無
注: 3.4.3版本開始支援。
-
allowUpload
true或false,true時顯示上傳圖檔标簽。
資料類型:Boolean
預設值:true
注: 3.4版本開始支援。
-
allowFileManager
true或false,true時顯示浏覽伺服器圖檔功能。
資料類型:Boolean
預設值:false
注: 3.4版本開始支援。
-
referMethod
設定referMethod後上傳圖檔的POST參數裡有referMethod。
資料類型:String
預設值:空
注: 3.4版本開始支援。
-
dialogAlignType
設定彈出框(dialog)的對齊類型,可設定page和空,指定page時按目前頁面居中,指定空時按編輯器居中。
資料類型:String
預設值:page
注: 3.4.1版本開始支援。
-
imageUploadJson
指定上傳圖檔的伺服器端程式。
資料類型:String
預設值:../../php/upload_json.php
注: 3.4.1版本開始支援。
-
fileManagerJson
指定浏覽遠端圖檔的伺服器端程式。
資料類型:String
預設值:../../php/file_manager_json.php
注: 3.4.1版本開始支援。