添加文章
(一) 在我的文章列表中添加跳转链接
<a href="addarticle.html">添加文章</a>
<hr>
(二) 编写添加文章页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>我的文章添加</title>
<script src="jquery-1.9.1.min.js"></script>
<script>
function mysub(type) {
var title = jQuery("#title");
var content = jQuery("#content");
if(type == 2) {
//清空
if(confirm("确定要清空吗?")) {
title.val("");
content.val("");
return false;
}
}
if (type == 1) {
//非空效验
if(title.val().trim() == "") {
alert("请先输入标题");
title.focus();
return false;
}
if(content.val().trim() == "") {
alert("请先输入正文");
content.focus();
return true;
}
jQuery.getJSON("add",{
"title":title.val(),
"content":content.val()
},function (data) {
if(data != null && data.succ == 1) {
alert("恭喜,添加文章成功!");
}else {
alert("抱歉,文章添加失败:"+ data.msg);
}
});
}
}
</script>
</head>
<body>
<div style="margin-left: 50px;margin-top: 30px">
<h1>添加文章</h1>
标题:<input id="title" type="text"><p></p>
正文:<textarea id="content" style="width: 60%;height: 300px"></textarea>
<input value="提交" type="button" onclick="mysub(1)">
<input value="清空" type="button" onclick="mysub(2)">
</div>
</body>
</html>
(三) 操作数据库进行文章添加
在ArticleInfoDao中添加addArticle方法
/**
* 添加文章标题,内容
* @param title
* @param content
* @param uid
* @return
* @throws SQLException
*/
public int addArticle(String title, String content,int uid) throws SQLException {
int result = 0;
Connection connection = DBUtils.getConnection();
String sql = "insert into articleinfo(title,content,uid) values(?,?,?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1,title);
statement.setString(2,content);
statement.setInt(3,uid);
result = statement.executeUpdate();
DBUtils.close(null,statement,connection);
return result;
}
(四) 编写servlet把处理好的数据返回前端页面
注意这里需要获取到session中的userInfo对象,然后获取到对用文章的作者uid。
package services;
import dao.ArticleInfoDao;
import models.UserInfo;
import utils.ResultJSONUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
/**
* Created with IntelliJ IDEA.
* Description:添加文章
* User: starry
* Date: 2021 -04 -12
* Time: 22:17
*/
@WebServlet("/add")
public class AddArticle extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int succ = -1; // succ=1 表示操作成功
String msg = ""; // 错误说明信息
// 1.从前端获取参数
String title = request.getParameter("title");
String content = request.getParameter("content");
// 2.调用数据库执行相应的业务逻辑
HttpSession session = request.getSession(false);
if(session != null && session.getAttribute("userInfo")!=null) {
UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
ArticleInfoDao dao = new ArticleInfoDao();
try {
succ = dao.addArticle(title,content,userInfo.getId());
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}else {
msg = "非登录状态,请先登录";
}
// 3.将上一步操作的结果返回给前端
HashMap<String, Object> result = new HashMap<>();
result.put("succ", succ);
result.put("msg", msg);
ResultJSONUtils.write(response, result);
}
}
最后的效果就是这样的