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时可以设置以下参数。
['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']
{ 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' : [] }注:filterMode为true时有效。3.4版本开始属性可设置style,保留所有inline样式。