富文本kindeditor

41 篇文章 0 订阅

kindeditor来做富文本。

springmvc+kindeditor

以前也用过,但是没有很清楚的了解kindeditor的适用方法,今天花了大量时间弄它。

首先,kindeditor能够帮助我们实现富文本的操作。

他怎么实现的呢?

看下例子就可以知道,比方说example中的simple.html:

<!doctype html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>Simple Examples</title>
		<style>
			form {
				margin: 0;
			}
			textarea {
				display: block;
			}
		</style>
		<link rel="stylesheet" href="../themes/default/default.css" />
		<script charset="utf-8" src="../kindeditor-min.js"></script>
		<script charset="utf-8" src="../lang/zh_CN.js"></script>
		<script>
			var editor;
			KindEditor.ready(function(K) {
				editor = K.create('textarea[name="content"]', {
					resizeType : 1,
					allowPreviewEmoticons : false,
					allowImageUpload : false,
					items : [
						'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
						'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
						'insertunorderedlist', '|', 'emoticons', 'image', 'link']
				});
			});
		</script>
	</head>
	<body>
		<h3>默认模式</h3>
		<form>
			<textarea name="content" style="width:700px;height:200px;visibility:hidden;">KindEditor</textarea>
		</form>
	</body>
</html>

首先先把改用的js加载进去:

<link rel="stylesheet" href="../themes/default/default.css" />
		<script charset="utf-8" src="../kindeditor-min.js"></script>
		<script charset="utf-8" src="../lang/zh_CN.js"></script>

然后再确定哪个textarea需要设置为富文本:

<textarea id="content" name="content" style="width:700px;height:300px;"></textarea>

是这个。然后就进行设置:

<script>
			var editor;
			KindEditor.ready(function(K) {
				editor = K.create('textarea[name="content"]', {
					resizeType : 1,
					allowPreviewEmoticons : false,
					allowImageUpload : false,
					items : [
						'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
						'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
						'insertunorderedlist', '|', 'emoticons', 'image', 'link']
				});
			});
		</script>


就是这么个流程,这只是最基础的。而如果复杂一点,要将图片上传到某个地方,并且自动插入到textarea标签中,怎么做呢?

在前面我做过一个图片、视频上传的一个功能:图片上传。这里就需要用到它。

先来处理上传位置的问题:

upload_json.jsp中:

//文件保存目录路径
String savePath = pageContext.getServletContext().getRealPath("/") + "attached/";
//文件保存目录URL
String saveUrl  = request.getContextPath() + "/attached/";

将其改下路劲就行,我这里改成的是绝对路径。"D:/upload/"。两个都改成这。

接下来解决插入图片:

在没有改变的时候,会报错,通过火狐可以看具体路径。

仔细看upload_json.jsp,会发现有个url,obj.put("url", saveUrl+newFileName);

JSONObject设置了url,就会运行该url对应的方法。我是这样设置的:

String path = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
		String picturePath = basePath+"/comment/getImage?path="+saveUrl + newFileName;
		obj.put("url", picturePath);

然后匹配url:

@RequestMapping(value = "/getImage",params = {"path"},method = RequestMethod.GET)
    public void getImage(HttpServletResponse response,String path) throws IOException {
        File file = new File(path);
        commentService.getImage(file, response);
    }

@Override
	public void getImage(File file,HttpServletResponse response) throws IOException {
        
        FileInputStream inputStream = new FileInputStream(file);
        byte[] data = new byte[(int)file.length()];
        inputStream.read(data);
        inputStream.close();
        
        String[] f =  file.getPath().split("\\.");
        String contentType = f[f.length-1].toLowerCase();
        //contentType = MIMEPropertiesReader.getProperty(contentType);
        response.setContentType(contentType);
        
        OutputStream stream = response.getOutputStream();
        stream.write(data);
        stream.flush();
        stream.close();
    }

这样就整个完成了



<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

path IS: /jspSmartUpLoad

basePath IS: http://yangm1203.oicp.net:8080/jspSmartUpLoad/

request.getScheme() IS: http

request.getServerName() IS: yangm1203.oicp.net

request.getServerPort() IS: 8080

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值