富文本编辑器-Ueditor传值

     前两天研究了一下富文本编辑器Ueditor的使用和配置,并且我们已经可以正常的在页面上编辑内容到富文本编辑器中,那么我们如何将输入的内容传到数据库中呢 ? Listen carefully.

    首先介绍一下环境配置:

              JDK-9.0.1

              MySql的数据库

              Tomcat 8.0

              Eclipse

    包结构

    (ps:那个报错对项目没有影响)

    在前几天的基础上,我们要对Ueditor的配置按照如下修改

UEditor的配置:

    在Eclipse中新建一个Web项目,将utf8-jsp复制到项目的WebContent(或WebRoot)下面;

    将utf8-jsp/jsp/lib下的所有jar包复制到WebContent/WEB-INF/lib中

    修改utf8-jsp/jsp/config.json文件,配置如下:

        "imageUrlPrefix": "/Ueditor", /* 图片访问路径前缀  一般使用:/项目名称 */
        "imagePathFormat": "/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */

    修改utf8-jsp/ueditor.config.js文件,配置如下:

        最开始添加一行命令:window.UEDITOR_HOME_URL = "/Ueditor/utf8-jsp/";

        通过配置toolbars属性来设置用户可以使用的功能。

BUG修改:(1)修改utf8-jsp/ueditor.config.js中第285行左右(scaleEnabled的默认值无效):

	,scaleEnabled:true//开启scaleEnabled,自动长高失效以固定编辑器的高度,当编辑内容超过时,会自动出现滚动条。

    接着我们在数据库中创建一个news的表

CREATE TABLE `news` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `title` VARCHAR(255) DEFAULT NULL,
  `content` TEXT,
  `publishtime` DATETIME DEFAULT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;

    然后我们在WebContent下创建ut.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>新闻发布</title>
<style type="text/css">
.left {
	float: left;
}

.wd10 {
	width: 10%;
}

.wd90 {
	width: 90%;
}

.he40 {
	height: 40px;
	line-height: 40px;
}

.he500 {
	height: 500px;
}
</style>
<script type="text/javascript" charset="utf-8"
	src="utf8-jsp/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8"
	src="utf8-jsp/ueditor.all.min.js">
	
</script>
<script type="text/javascript" charset="utf-8"
	src="utf8-jsp/lang/zh-cn/zh-cn.js"></script>
<script type="text/javascript" charset="utf-8" src="js/jquery-2.0.2.js"></script>
</head>
<body>
	<div class="he40"
		style="background: blue; color: white; margin-bottom: 20px;">发布新闻</div>
	<div>
		<div class="left wd10 he40">新闻标题:</div>
		<div class="left wd90 he40">
			<input type="text" id="title" value=""
				style="width: 800px; height: 35px;" />
		</div>
	</div>
	<div>
		<div class="left wd10 he500">新闻内容:</div>
		<div class="left wd90 he500">
			<script type="text/plain" id="content"
				style="width: 800px; height: 350px;"></script>
		</div>
	</div>
	<br>
	<br>
	<br>
	<br>
	<div style="margin-top: 100px;">
		<div class="left he40" style="width: 100%; text-align: center;">
			<input type="button" id="tjbtn" value="提交"
				style="width: 80px; height: 35px;" />
		</div>
	</div>
</body>
<script type="text/javascript">
	var ue = UE.getEditor('content');
	$('#tjbtn').click(
			function() {
				$.ajax({
					url : "Publish",
					type : 'post',
					data : "title=" + $('#title').val() + "&content="
							+ encodeURIComponent(ue.getContent()),
					dataType : "html",
					success : function(re) {
						$('#title').val('');
						ue.execCommand('cleardoc');
						alert(re);
					}
				});
			});
</script>
</html>

    上述代码中,我们在html页面上引入了Ueditor,我们在js代码块中,对提交(tjbtn)按钮进行了ajax异步请求处理。

    其次我们在src目录中创建一个Servlet文件,名为:Publish 基于publish包下

     Publish.java

package publish;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Publish")
public class Publish extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String driver="com.mysql.jdbc.Driver";
	private static final String url="jdbc:mysql://localhost:3306/mybatis";
	private static final String user="root";
	private static final String password="root";   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Publish() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String title=request.getParameter("title").trim();
		String content=request.getParameter("content").trim();
		System.out.println("title:"+title+"\ncontent:"+content);
		String restr="";
		Connection con;
		try{
			Class.forName(driver);
			con=DriverManager.getConnection(url,user,password);
			Statement st=con.createStatement();
			String sql="INSERT INTO news SET title='"+title+"',content='"+content+"',publishtime=NOW();";
			System.out.println(sql);
			st.executeUpdate(sql);
			restr="上传成功";
			st.close();
			con.close();
		}catch(Exception e){
			e.printStackTrace();
			restr="上传失败";
		}
		response.setContentType("text/html;charset=UTF-8;");
		PrintWriter out=response.getWriter();
		out.print(restr);
		out.flush();
		out.close();
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}         

     在上述代码中,我们先连接了数据库,然后在Get请求中,我们用request.getParameter方法获取了从页面传过来的Title和Content的值。然后用sql的插入语句将这两个值传入到数据库中,最后关闭数据库连接。

      运行效果:

       

    我们在新闻标题里写上:Back_YeJing is good boys,在新闻内容里面写上"琵琶行",然后点击提交。这时候会弹出提交成功的警告框。然后我们去数据库中查看

    

   总结

       从数据库中的只来看,我们在Ueditor中传值这个功能是成功的,但是在Content中的内容会加入一些html的tag。这是因为Ueditor可以自动将你插入的文字转换成html代码,所以当content里面的内容传入到数据库中时,这些tag也会随之传入数据库中。

猜你喜欢

转载自www.cnblogs.com/Black-YeJing/p/9147120.html