Ckeditor一種很方便的文本編輯器


ckeditor官網: http://ckeditor.com/

這里介紹ckeditor的其中一個的用法,自己做小項目練手非常的適合,上手非常的快。

首先去官網下載這個東西,鏈接:http://pan.baidu.com/s/1nuXePuD 密碼:rrr0,需要特別說明一下,這個東西需要配置,但是具體配置我也不是很清楚,所以你看到着篇博客,練習的話,最后使用上面上傳的這個東西,目錄呢,如圖所示。


 1:首先將這個ckeditor文件夾放到webcontent目錄下面,然后進行開發。

  使用這個文本編輯器的最重要需要引入的一句話是:

  <script type="text/javascript" src="resource/ckeditor/ckeditor.js"></script>

  然后在需要使用的地方引入這個:class="ckeditor",如下所示:

  <textarea  class="ckeditor" id="newsContent"  name="newsContent"   rows="15"   placeholder="請輸入內容">            </textarea>

完整的代碼如下所示,文件名是news.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%
 4     //獲取絕對路徑路徑 ,開發項目一定要使用絕對路徑,不然肯定出錯
 5     String path = request.getContextPath();
 6     String basePath = request.getScheme() + "://"
 7                 + request.getServerName() + ":" + request.getServerPort()
 8                 + path + "/";
 9 %> 
10 <!DOCTYPE html >
11 <html>
12 <head>
13 <base href="<%=basePath%>">
14 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
15 <title>新聞發布</title>
16 
17 <!-- 新 Bootstrap 核心 CSS 文件 -->
18 <link rel="stylesheet" href="resource/css/bootstrap.min.css">
19 
20 <!-- jQuery文件。務必在bootstrap.min.js 之前引入 -->
21 <script src="resource/js/jquery.min.js"></script>
22 
23 <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
24 <script src="resource/js/bootstrap.min.js"></script>
25 
26 <script type="text/javascript" src="resource/ckeditor/ckeditor.js"></script>
27 </head>
28 <body>
29     <div class="container">
30         <h1 class="page-header">新聞發布</h1>
31         <form class="form-horizontal" action="newsServlet" method="post">
32             <div class="form-group">
33                 <label for="newsMan" class="col-sm-1 control-label">發布人</label>
34                 <div  class="col-sm-4">
35                     <input  class="form-control " name="newsMan" id="newsMan" placeholder="請輸入發布人"/>
36                 </div>
37             </div>
38             <div class="form-group">
39                 <label for="newsTitle" class="col-sm-1 control-label">新聞標題</label>
40                 <div  class="col-sm-6">
41                     <input  class="form-control " name="newsTitle" id="newsTitle" placeholder="請輸入新聞標題"/>
42                 </div>
43             </div>
44             <div class="form-group">
45                 <label for="newsContent" class="col-sm-1 control-label">新聞內容</label>
46                 <div  class="col-sm-11">
47                     <textarea  class="ckeditor" id="newsContent"  name="newsContent"   rows="15"   placeholder="請輸入內容"></textarea>
48                 </div>
49             </div>
50             
51             <div class="form-group">
52                 <div  class="col-sm-4 col-sm-offset-1">
53                     <input type="submit" value="發 布 新 聞 " class="btn btn-success btn-lg"/>
54                 </div>
55             </div>
56         </form>    
57     </div>
58 </body>
59 </html>

2:這個jsp提交到servlet這個頁面,完整代碼如下所示,當然了,這里沒有設計到數據庫,因為涉及到數據庫,更難理解和講解,這里學會使用,自己摸索不是很難哦。

文件名是:NewsServlet.java 

 1 package com.liu.servlet;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.annotation.WebServlet;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 @WebServlet("/newsServlet")
11 public class NewsServlet extends HttpServlet {
12     
13 
14     private static final long serialVersionUID = 1L;
15 
16     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
17         //設置字符編碼格式
18         request.setCharacterEncoding("utf-8");
19         
20         //獲取從前台傳來的參數
21         String newsMan = request.getParameter("newsMan");
22         String newsTitle = request.getParameter("newsTitle");
23         String newsContent = request.getParameter("newsContent");
24         
25         //控制台輸出測試內容
26         System.out.println("newsMan:"+newsMan);
27         System.out.println("newsTitle:"+newsTitle);
28         System.out.println("newsContent:"+newsContent);
29         
30         //將獲取的參數保存到request域中
31         request.setAttribute("newsMan", newsMan);
32         request.setAttribute("newsTitle", newsTitle);
33         request.setAttribute("newsContent", newsContent);
34         
35         //重定向操作
36         request.getRequestDispatcher("/index.jsp").forward(request, response);
37     }
38 
39 }

3:上面的servlet頁面又重定向到了index.jsp,如下所示:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%
 4     //獲取絕對路徑路徑 ,開發項目一定要使用絕對路徑,不然肯定出錯
 5     String path = request.getContextPath();
 6     String basePath = request.getScheme() + "://"
 7                 + request.getServerName() + ":" + request.getServerPort()
 8                 + path + "/";
 9 %> 
10 <!DOCTYPE html >
11 <html>
12 <head>
13 <base href="<%=basePath%>">
14 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
15 <title>新聞發布</title>
16 
17 <!-- 新 Bootstrap 核心 CSS 文件 -->
18 <link rel="stylesheet" href="resource/css/bootstrap.min.css">
19 
20 <!-- jQuery文件。務必在bootstrap.min.js 之前引入 -->
21 <script src="resource/js/jquery.min.js"></script>
22 
23 <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
24 <script src="resource/js/bootstrap.min.js"></script>
25 
26 <script type="text/javascript" src="resource/ckeditor/ckeditor.js"></script>
27 </head>
28 <body>
29     <div class="container">
30     <h1 class="page-header">查看新聞</h1>
31     <form class="form-horizontal" action="newsServlet" method="post">
32             <div class="form-group">
33                 <label for="newsMan" class="col-sm-1 control-label">發布人</label>
34                 <div  class="col-sm-4">
35                     <p class="form-control-static">${newsMan }</p>
36                 </div>
37             </div>
38             <div class="form-group">
39                 <label for="newsTitle" class="col-sm-1 control-label">新聞標題</label>
40                 <div  class="col-sm-6">
41                     <p class="form-control-static">${newsTitle }</p>
42                 </div>
43             </div>
44             <div class="form-group">
45                 <label for="newsContent" class="col-sm-1 control-label">新聞內容</label>
46                 <div  class="col-sm-11">
47                     <p class="form-control-static">${newsContent }</p>
48                 </div>
49             </div>
50         </form>
51     </div>
52 </body>
53 </html>

4:特比需要注意的是下面這個servlet,只要復制粘貼會使用即可,可先不用看代碼;

 1 package com.liu.servlet;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 import java.util.UUID;
 6 
 7 import javax.servlet.ServletException;
 8 import javax.servlet.annotation.MultipartConfig;
 9 import javax.servlet.annotation.WebServlet;
10 import javax.servlet.http.HttpServlet;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13 import javax.servlet.http.Part;
14 
15 @WebServlet("/upLoad")
16 @MultipartConfig
17 public class UpLoadServlet extends HttpServlet {
18 
19 
20     private static final long serialVersionUID = 1L;
21 
22     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
23         request.setCharacterEncoding("utf-8");
24         
25         //獲取文件的part
26         Part part = request.getPart("upload");
27         
28         //獲取請求的信息
29         String requestinfo = part.getHeader("content-disposition");
30         System.out.println(requestinfo);
31         
32         //獲取文件的后綴名
33         String str = requestinfo.substring(requestinfo.lastIndexOf("."),requestinfo.length()-1);
34         System.out.println("后綴名:"+str);
35         
36         //獲取上傳文件的目錄
37         String root = request.getServletContext().getRealPath("//upload");
38         System.out.println(root);
39         
40         //重新創建文件名
41         String filename = UUID.randomUUID().toString()+str;
42         String url = root+"\\"+filename;
43         System.out.println(url);
44         part.write(url);
45         
46         
47         //響應
48         PrintWriter out = response.getWriter();
49         //獲取路徑
50         String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();
51 
52         String callback = request.getParameter("CKEditorFuncNum");
53         
54         out.print("<script>window.parent.CKEDITOR.tools.callFunction("+callback+",'"+basePath+request.getContextPath()+"/upload/"+filename+"') </script>");
55         
56         out.flush();
57         out.close();
58     }
59 
60 }

5:最后說一下需要注意的地方:

其一就是如下圖所示的config.js文件,這里需要修改一下文件內容

如下圖所示,將ckeditor修改為自己的項目名稱即可;

演示效果:

還有完善的空間,繼續加油哦。別先生


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM