kindeditor整合到struts项目中

时间:2022-12-14 19:55:39

1.下载下来,贴到webcontent

2.jsp中找到demo.jsp

具体代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="../include.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>邮件发送页面</title>
<link href ="${path}styles/system_manage.css" rel="stylesheet" />
<link rel="stylesheet" href="${path}kindeditor-4.1.10/themes/default/default.css" />
<link rel="stylesheet" href="${path}kindeditor-4.1.10/plugins/code/prettify.css" />
<script charset="utf-8" src="${path}kindeditor-4.1.10/kindeditor.js"></script>
<script charset="utf-8" src="${path}kindeditor-4.1.10/lang/zh_CN.js"></script>
<script charset="utf-8" src="${path}kindeditor-4.1.10/plugins/code/prettify.js"></script>
<script>
KindEditor.ready(function(K) {
var editor1 = K.create('textarea[name="content"]', {
cssPath : '${path}kindeditor-4.1.10/plugins/code/prettify.css',
uploadJson : 'image!upload.action',
fileManagerJson : 'image!upload.action',
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>
<script type="text/javascript">
function check0(){
var subject = document.getElementById("subject").value;
if(subject == null || subject == ""){
document.getElementById('msg1').innerHTML ='主题不能为空';
//alert("你好");
document.getElementById("all").type = 'button';
}else{
document.getElementById('msg1').innerHTML ='';
document.getElementById("all").type = 'submit';
}
}
function check1(){
var subject = document.getElementById("email").value;
if(subject == null || subject == ""){
document.getElementById('msg2').innerHTML ='邮件地址不能为空';
document.getElementById("all").type = "button";
}else{
document.getElementById('msg2').innerHTML ='';
document.getElementById("all").type = "submit";
}
//alert(subject)
var isemail=/^\w+([-\.]\w+)*@\w+([\.-]\w+)*\.\w{2,4}$/;
var s = subject.split("\n");
for(var i = 0;i < s.length; i++){
if(!isemail.test(s[i])){
document.getElementById("all").type = "button";
alert("有邮件格式不正确");
break;
}else{
document.getElementById("all").type = "submit";
}
}

}

function check(){
check0();
check1();
var c = document.getElementById("all").type;
if( c == "submit"){
if(confirm("确认发送邮件吗?") == true){
document.getElementById('m').innerHTML ='邮件正在发送中···请不要关闭窗口!';
window.editor.sync();//跨域
}else{
document.getElementById("all").type = "button";
}
}
}
</script>
<style type="text/css">
.a{color:red;font-size:25px;}
</style>
</head>
<body>
<input type="hidden" id="active_idx" value="4" />
<input type="hidden" id="active_button" value="button0" />
<table class="centerTab" cellspacing="0">
<tr>
<td colspan="2"><jsp:include page="../head.jsp"></jsp:include></td>
</tr>
<tr>
<td class="firstTd" valign="top"><jsp:include page="../left_bar.jsp"></jsp:include></td>
<td class="secondTd" valign="top">
<div class="centerContent">
<%
request.setCharacterEncoding("UTF-8");
String htmlData = request.getParameter("content") != null ? request.getParameter("content") : "";
%>
<%=htmlData%>

<div align="center">

<p>${msg1}</p>
<span id="m" class="a"></span>
<form name="example" method="post" id="editor_id" action="<s:url value='email!sendEmail.action' />">
邮件主题:<span id = "msg1" class="a"></span><br><input type="text" name="subject" style="width:700px" id="subject" onblur="check0()" value="${subject}"/><br><p></p>
邮件地址:<span id = "msg2" class="a"></span><br><textarea name="email" style="width:700px;height:100px;" id="email" onblur="check1()" >${email}</textarea><br>
邮件内容:<textarea name="content" cols="100" rows="80" style="width:700px;height:300px;visibility:hidden;"><%=htmlspecialchars(htmlData)%></textarea>
<br />
<input type="submit" name="button" value="提交内容" onclick="check()" id ="all"/> (提交快捷键: Ctrl + Enter)
</form>
<%!
private String htmlspecialchars(String str) {
str = str.replaceAll("&", "&amp;");
str = str.replaceAll("<", "&lt;");
str = str.replaceAll(">", "&gt;");
str = str.replaceAll("\"", "&quot;");
return str;
}
%>
</div>
</div>
</td>
</tr>
</table>
</body>
</html>

3.struts对应的类,代码如下

package com.renderincloud.bsm.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

import com.curender.dal.bean.Images;
import com.curender.dal.client.FocusImgsOpera;
import com.curender.dal.util.ConstantsDal;
import com.renderincloud.bsm.util.*;


import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.json.simple.JSONObject;

public class ImageAction extends BaseAction {

private static final long serialVersionUID = 1L;
private static final int BUFFER_SIZE = 16 * 1024; //一次读取16kb
private File imgFile;//文件

private String imgFileFileName;//名字

private String imgFileContentType;//类型

public String getImgFileFileName() {
return imgFileFileName;
}

public void setImgFileFileName(String imgFileFileName) {
this.imgFileFileName = imgFileFileName;
}

public String getImgFileContentType() {
return imgFileContentType;
}

public void setImgFileContentType(String imgFileContentType) {
this.imgFileContentType = imgFileContentType;
}

public File getImgFile() {
return imgFile;
}

public void setImgFile(File imgFile) {
this.imgFile = imgFile;
}

public void upload() throws Exception {
String imageFileName = new Date().getTime() + getExtention(imgFileFileName);
//设置上传图片的路径
String path = request.getSession().getServletContext().getRealPath(
"/attached/image/")+"\\"+imageFileName;
String saveUrl = request.getContextPath()+"/attached/image/"+imageFileName;

//System.out.println(saveUrl);
// String path = "/opt/tomcat-6.0.35-bsm/webapps/curender-bsm-1.x/" + imageFileName;
File imageFile = new File(path);
//File imageFile = new File(request.getContextPath()+"/picture"+"/"+imageFileName);
copy(imgFile, imageFile);
//System.out.println(path);

//copy(pro, severFile);
//Runtime.getRuntime().exec("scp "+path+ConstantsDal.APACHE_IP+" :/opt/apache/htdocs/img/focus");

//System.out.println(path);

JSONObject obj = new JSONObject();
obj.put("error", 0);
obj.put("url", saveUrl);

// ServletActionContext.getRequest().setAttribute("msg",obj.toJSONString());
response.getWriter().write(obj.toJSONString());
//return SUCCESS;
}
/**
* 将src中的文件拷贝到dst中
* @param src
* @param dst
*/
private static void copy(File src, File dst) {
try {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
dst.createNewFile();
out = new BufferedOutputStream(new FileOutputStream(dst), BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
while (in.read(buffer) > 0) {
out.write(buffer);
}
} finally {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 获取上传图片的格式
* @param fileName
* @return
*/
private static String getExtention(String fileName) {
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}

}