jquery ajax发帖-仿知乎个人网站fourask(二)

该博客介绍了如何使用富文本编辑器UEDITOR结合jQuery AJAX实现帖子的发布功能,通过JSP与MYSQL数据库进行交互。首先,下载并配置UEDITOR,然后在JSP中设置路径,最后展示编辑器的使用界面。
摘要由CSDN通过智能技术生成

(1)使用富文本编辑器UEDITOR实现帖子的发布和提交

UEDITOR下载地址 : http://ueditor.baidu.com/website/download.html


选择 UTF8-JSP版本

第一步:下载后引入JS,解压后将jsp/lib文件夹内所有JAR包放入WEB-INF文件夹lib内,并build path


第二步:打开项目内的ueditor.congig.js文件,配置路径(你的路径)



HTML代码

<form id="question" method="post" title="提问"> 
<p> 
   <label for="user">问题名称:</label> 
   <input type="text" name="title" class="text" style="width:800px" id="title"/>
   <span class="star">*</span>
</p>
<p> <textarea class="uEditorCustom" name="content" id="myEditor" style="width:800px;height:270px">请填写问题描述!</textarea></p>
</form>

JQuery代码

	$('#question_button').click(function(){ 
		  if($.cookie('username')){ 
		   $('#question').dialog('open');}
		  else{ $('#error').dialog('open');
		        setTimeout(function(){ 
		          $('#error').dialog('close');
		          $('#login').dialog('open');},1000);}
		   });	
	
	$('#question').dialog({ 
		  autoOpen:false,
		  modal:true,
		  resizable:false,
		  width:850,
		  height:620,
		  buttons:{ 
		    '发布':function(){ 
		    	$(this).ajaxSubmit({ 
		    		  url:'add.jsp',
		    		  data:{ 
		    			  user: $.cookie('username'), 
		    		  }, 
		    		  beforeSubmit:function(formData,jqForm,options){ 
		    		        $('#loading').dialog('open'); 
		    		        }, 
		    		  success:function(responseText,statusText){ 
		    		     if(responseText){ 
		    		        $('#loading').css('background','url(imag/success.png)no-repeat 20px center').html('提问成功发布'); 
		    		        setTimeout(function(){ 
			    		       $('#loading').css('background','url(imag/loading.gif) no-repeat 20px center').html('发布提问中...');
		    		           $('#loading').dialog('close');
		    		           $('#question').dialog('close');
		    		           $('#question').resetForm(); 
                                },1000);}},});
		    }
	}});


JSP代码(MYSQL数据库)

    <%@ page language="java" import="java.util.*,java.io.*,java.util.Date" %>
    <%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
    <%@ page import ="java.sql.*,java.text.*" %>
    <%@ page import="com.mysql.jdbc.Driver" %>
<%  
   request.setCharacterEncoding("UTF-8");  
   response.setCharacterEncoding("UTF-8");  
   response.setContentType("text/html; charset=utf-8");  
%>  
<html> 
<head> 
<title>add content into table </TITLE> 
</head> 
<body> 
<% 
String title=request.getParameter("title"); //从表单获得 
String content=request.getParameter("content"); //从表单获得 
String user=request.getParameter("user"); //从表单获得 
Date date=new Date();
DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time=format.format(date);

try 
{ 
/** 连接数据库参数 **/ 
String driverName = "com.mysql.jdbc.Driver"; //驱动名称 
String DBUser = "root"; //mysql用户名 
String DBPasswd = ""; //mysql密码 
String DBName = "zhiwen"; //数据库名 

String connUrl = "jdbc:mysql://localhost/" + DBName + "?user=" + DBUser + "&password=" + DBPasswd + "&useUnicode=true&characterEncoding=UTF-8"; 
Class.forName(driverName).newInstance(); 
Connection conn = DriverManager.getConnection(connUrl); 
Statement stmt = conn.createStatement(); 
String insert_sql = "insert into question(title,content,user,date) values('"+title+"','"+content+"','"+user+"','"+time+"')"; 
String query_sql = "select * from question"; 

try { 
stmt.execute(insert_sql); 
}catch(Exception e) { 
e.printStackTrace(); 
} 
try { 
ResultSet rs = stmt.executeQuery(query_sql); 
while(rs.next()) { 
%> 
title:<%=rs.getString("title")%> </br> 
content:<%=rs.getString("content")%> </br> 
user:<%=rs.getString("user")%> </br> 
time:<%=rs.getString("date")%> </br> 
<% 
} 
}catch(Exception e) { 
e.printStackTrace(); 
} 
//rs.close(); 
stmt.close(); 
conn.close(); 
}catch (Exception e) { 
e.printStackTrace(); 
} 
%> 
</body> 
</html> 

编辑器界面如下图所示


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值