diff --git a/.idea/dataSources/f9a4aa96-34a3-4893-85e5-538605dcfc4e.xml b/.idea/dataSources/f9a4aa96-34a3-4893-85e5-538605dcfc4e.xml index fa533b143a01eb947888dbde4467c7ad8849bd5c..695fe69afa5c399579c7a2f155b7187ab8f99f61 100644 --- a/.idea/dataSources/f9a4aa96-34a3-4893-85e5-538605dcfc4e.xml +++ b/.idea/dataSources/f9a4aa96-34a3-4893-85e5-538605dcfc4e.xml @@ -20,45 +20,116 @@ InnoDB utf8_general_ci - + + InnoDB + utf8_general_ci +
+ + InnoDB + utf8_general_ci +
+ 3 int(11)|0s 1 1 - + varchar(20)|0s 1 2 - + varchar(20)|0s 1 3 - + varchar(20)|0s '123456' 1 4 - + userID btree 1 - + email btree 1 - + 1 1 PRIMARY - + email + + int(11)|0s + 1 + 1 + + + varchar(50)|0s + 1 + 2 + + + datetime|0s + 3 + + + char(255)|0s + '详情请查看' + 1 + 4 + + + 1 + int(11)|0s + 1 + 5 + + + userID + userID + user + + + textID + btree + 1 + + + userID + btree + + + 1 + 1 + PRIMARY + + + int(11)|0s + 1 + 1 + + + text|0s + 2 + + + textID + textID + user_blog + + + textID + btree + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000000000000000000000000000000000000..2b63946d5b31084bbb7dda418ceb3d75eb686373 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/webContexts.xml b/.idea/webContexts.xml index 91eb9e87e854eae2587d77a69de686c57bb5513a..03c0f3e78122815d3dc1eb9d68f379164887de78 100644 --- a/.idea/webContexts.xml +++ b/.idea/webContexts.xml @@ -3,6 +3,7 @@ diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 32b045c43c06dee54250ca6fa0cb6b5d8c778f87..591e96da1fe25a6e47a130b1b379ecd0b1c23f52 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,14 +1,21 @@ + + + + + - + - - - + + + + + @@ -34,6 +41,13 @@ + + @@ -63,7 +77,7 @@ "WebServerToolWindowFactoryState": "false", "last_opened_file_path": "D:/IntelliJ IDEA/Java_Workplace/blog/src/main/webapp/imgs", "list.type.of.created.stylesheet": "CSS", - "project.structure.last.edited": "Modules", + "project.structure.last.edited": "Artifacts", "project.structure.proportion": "0.15", "project.structure.side.proportion": "0.2", "settings.editor.selected.configurable": "MavenSettings", @@ -87,6 +101,133 @@ + + + + + + + + + + + + + + + + @@ -106,7 +247,23 @@ - + + + + + + + + + + + + + + + + + 1677144154839 @@ -143,7 +300,35 @@ - @@ -166,7 +351,9 @@ - diff --git a/pom.xml b/pom.xml index dce29a9f104c44ed44818b9574173fa5b1b583c6..f5eacb55811b39bb67d51cf12271e2a4a5d08d46 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.example blog - 1.0-SNAPSHOT + haha war @@ -29,9 +29,9 @@ - org.junit.jupiter - junit-jupiter-api - 5.3.0-M1 + junit + junit + 4.13.2 test diff --git a/src/main/java/cn/zyq/vesionone/dao/UserDAO.java b/src/main/java/cn/zyq/vesionone/dao/UserDAO.java index 9e4319489073cec883fea17d8abc9d34ca404d04..0a31b09fce69d6af3b0fc5b4c6ad5b367856ceb4 100644 --- a/src/main/java/cn/zyq/vesionone/dao/UserDAO.java +++ b/src/main/java/cn/zyq/vesionone/dao/UserDAO.java @@ -1,8 +1,11 @@ package cn.zyq.vesionone.dao; import cn.zyq.vesionone.pojo.User; +import cn.zyq.vesionone.pojo.UserBlog; import java.sql.*; +import java.util.ArrayList; +import java.util.List; public class UserDAO { private String dbUrl = "jdbc:mysql://127.0.0.1/myblog?useSSL=false&useServerPrepStmts=true"; @@ -93,4 +96,142 @@ public class UserDAO { return user; } + public void insertUserBlog(UserBlog userBlog) throws SQLException { + + String sql = "INSERT INTO user_blog(userID,title,publish_date,brief_info) VALUES (?,?,?,?)"; + + try( Connection connection = getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement(sql);){ + // 设置?的值 + preparedStatement.setInt(1,userBlog.getUserID()); + preparedStatement.setString(2,userBlog.getTitle()); + preparedStatement.setString(3,userBlog.getPublishDate()); + preparedStatement.setString(4,userBlog.getBriefInfo()); + +// // 有事务的前提下 +// // 在事务中的多个操作,要么都成功,要么都失败 +// connection.setAutoCommit(false); + + // 解释为什么不能用事务: +// user_blog_context有指向user_blog的外键约束,所以必须user_blog必须先存在某条数据,之后user_blog_context才能创建指向该数据的外键约束。 + + preparedStatement.execute(); + +// // 手动提交 +// connection.commit(); + + } + + } + + public void insertBlogContext(UserBlog userBlog) throws SQLException { + + String sql = "INSERT INTO user_blog_context(context) VALUES (?)"; + + try( Connection connection = getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement(sql);){ + // 设置?的值 + preparedStatement.setString(1,userBlog.getContext()); + +// // 有事务的前提下 +// // 在事务中的多个操作,要么都成功,要么都失败 +// connection.setAutoCommit(false); + + // 解释为什么不能用事务: +// user_blog_context有指向user_blog的外键约束,所以必须user_blog必须先存在某条数据,之后user_blog_context才能创建指向该数据的外键约束。 + + preparedStatement.execute(); + +// // 手动提交 +// connection.commit(); + + } + + } + + public List getUserBlogs(int userID) throws SQLException { + + List userBlogs = new ArrayList<>(); + + String sql = "SELECT userID,t1.title,publish_date,brief_info,t2.context,t1.textID FROM" + + " user_blog t1 INNER JOIN user_blog_context AS t2 ON t1.textID = t2.textID" + + " WHERE t1.userID = ?"; + + try ( Connection connection = getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement(sql)){ + + // 设置?的值 + preparedStatement.setInt(1,userID); + + // 执行查询操作 + ResultSet resultSet = preparedStatement.executeQuery(); + + while ( resultSet.next() ){ + UserBlog userBlog = new UserBlog(); + userBlog.setUserID(resultSet.getInt(1)); + userBlog.setTitle(resultSet.getString(2)); + userBlog.setPublishDate(resultSet.getString(3)); + userBlog.setBriefInfo(resultSet.getString(4)); + userBlog.setContext(resultSet.getString(5)); + userBlog.setTextID(resultSet.getInt(6)); + userBlogs.add(userBlog); + } + + } + + return userBlogs; + } + + public UserBlog getUserBlog(int textID) throws SQLException { + + UserBlog userBlog = null; + + String sql = "SELECT userID,t1.title,publish_date,brief_info,t2.context FROM" + + " user_blog t1 INNER JOIN user_blog_context AS t2 ON t1.textID = t2.textID" + + " WHERE t1.textID = ?"; + + try ( Connection connection = getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement(sql)){ + + // 设置?的值 + preparedStatement.setInt(1,textID); + + // 执行查询操作 + ResultSet resultSet = preparedStatement.executeQuery(); + + if ( resultSet.next() ){ + userBlog = new UserBlog(); + userBlog.setUserID(resultSet.getInt(1)); + userBlog.setTitle(resultSet.getString(2)); + userBlog.setPublishDate(resultSet.getString(3)); + userBlog.setBriefInfo(resultSet.getString(4)); + userBlog.setContext(resultSet.getString(5)); + } + + } + + return userBlog; + } + + public String getBlogContext(int textID) throws SQLException { + String context = null; + + String sql = "SELECT context FROM user_blog_context WHERE textID=?"; + + try( Connection connection = getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement(sql);){ + + preparedStatement.setInt(1,textID); + + ResultSet resultSet = preparedStatement.executeQuery(); + + if (resultSet.next() ){ + context = resultSet.getString(1); + } + } + + return context; + } + + } diff --git a/src/main/java/cn/zyq/vesionone/filter/CheckFilter.java b/src/main/java/cn/zyq/vesionone/filter/CheckFilter.java new file mode 100644 index 0000000000000000000000000000000000000000..1e200cd19844fc8f2220ed6f4e82918b65cbbd21 --- /dev/null +++ b/src/main/java/cn/zyq/vesionone/filter/CheckFilter.java @@ -0,0 +1,56 @@ +package cn.zyq.vesionone.filter; + +import javax.servlet.*; +import javax.servlet.annotation.*; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import java.io.IOException; + +@WebFilter("*.html") +public class CheckFilter extends HttpServlet implements Filter{ + + public void init(FilterConfig config) throws ServletException { + + int a = 3; + + } + + public void destroy() { + int a = 3; + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { + // 放行前 + // 从session中获取数据 + // 1、获取session对象 + HttpServletRequest req = (HttpServletRequest) request; + HttpServletResponse resp = (HttpServletResponse) response; + HttpSession session = req.getSession(false); + +// 3.在Servlet中取得路径: +// +// (1)得到工程目录:request.getSession().getServletContext().getRealPath("") 参数可具体到包名。 +// 结果:E:/Tomcat/webapps/TEST +// (2)得到IE地址栏地址:request.getRequestURL() +// 结果:http://localhost:8080/TEST/test +// (3)得到相对地址:request.getRequestURI() +// 结果:/TEST/test + + String uri = req.getRequestURI(); + +// System.out.println(uri); + + if ( session == null && !"/blog/betterUIForLR.html".equals(uri) ){ +// 重定向 + String contextPath = req.getContextPath(); + resp.sendRedirect(contextPath+"/betterUIForLR.html"); + } + + // 放行 + chain.doFilter(request, response); + // 放行后 + } +} diff --git a/src/main/java/cn/zyq/vesionone/filter/SolveChineseTransFilter.java b/src/main/java/cn/zyq/vesionone/filter/SolveChineseTransFilter.java new file mode 100644 index 0000000000000000000000000000000000000000..1ca58cb773af051aa0b24013a2ab5af1475e23df --- /dev/null +++ b/src/main/java/cn/zyq/vesionone/filter/SolveChineseTransFilter.java @@ -0,0 +1,27 @@ +package cn.zyq.vesionone.filter; + +import javax.servlet.*; +import javax.servlet.annotation.*; +import java.io.IOException; + +@WebFilter("/*") +public class SolveChineseTransFilter implements Filter { + public void init(FilterConfig config) throws ServletException { + int a = 3; + } + + public void destroy() { + int a = 3; + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { + + //解决乱码: POST getReader() + //设置字符输入流的编码,设置的字符集要和页面保持一致 + request.setCharacterEncoding("UTF-8"); + + chain.doFilter(request, response); + + } +} diff --git a/src/main/java/cn/zyq/vesionone/pojo/UserBlog.java b/src/main/java/cn/zyq/vesionone/pojo/UserBlog.java new file mode 100644 index 0000000000000000000000000000000000000000..318183242d5117aa1cd7199ff7190e2eea0f05d2 --- /dev/null +++ b/src/main/java/cn/zyq/vesionone/pojo/UserBlog.java @@ -0,0 +1,75 @@ +package cn.zyq.vesionone.pojo; + +public class UserBlog { + + private int userID; + private String title; + private String publishDate; + private String briefInfo; + private int textID; + + private String context; + + public UserBlog() { + } + + public int getUserID() { + return userID; + } + + public void setUserID(int userID) { + this.userID = userID; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getPublishDate() { + return publishDate; + } + + public void setPublishDate(String publishDate) { + this.publishDate = publishDate; + } + + public String getBriefInfo() { + return briefInfo; + } + + public void setBriefInfo(String briefInfo) { + this.briefInfo = briefInfo; + } + + public int getTextID() { + return textID; + } + + public void setTextID(int textID) { + this.textID = textID; + } + + public String getContext() { + return context; + } + + public void setContext(String context) { + this.context = context; + } + + @Override + public String toString() { + return "UserBlog{" + + "userID=" + userID + + ", title='" + title + '\'' + + ", publishDate='" + publishDate + '\'' + + ", briefInfo='" + briefInfo + '\'' + + ", textID=" + textID + + ", context='" + context + '\'' + + '}'; + } +} diff --git a/src/main/java/cn/zyq/vesionone/pojo/UserBlogContext.java b/src/main/java/cn/zyq/vesionone/pojo/UserBlogContext.java new file mode 100644 index 0000000000000000000000000000000000000000..4d474408aa7de531a25778aff00feecac8cb336c --- /dev/null +++ b/src/main/java/cn/zyq/vesionone/pojo/UserBlogContext.java @@ -0,0 +1,34 @@ +package cn.zyq.vesionone.pojo; + +public class UserBlogContext { + + private int textID; + private String context; + + public UserBlogContext() { + } + + public int getTextID() { + return textID; + } + + public void setTextID(int textID) { + this.textID = textID; + } + + public String getContext() { + return context; + } + + public void setContext(String context) { + this.context = context; + } + + @Override + public String toString() { + return "UserBlogContext{" + + "textID=" + textID + + ", context='" + context + '\'' + + '}'; + } +} diff --git a/src/main/java/cn/zyq/vesionone/service/UserService.java b/src/main/java/cn/zyq/vesionone/service/UserService.java index 60ac31580b61b4f350bf72ad60d40991cd8309c0..6f49f51ead65528d66656e88e5d09baf708e50f6 100644 --- a/src/main/java/cn/zyq/vesionone/service/UserService.java +++ b/src/main/java/cn/zyq/vesionone/service/UserService.java @@ -1,6 +1,9 @@ package cn.zyq.vesionone.service; import cn.zyq.vesionone.pojo.User; +import cn.zyq.vesionone.pojo.UserBlog; + +import java.util.List; public interface UserService { public void register(User user); @@ -8,4 +11,11 @@ public interface UserService { public User login(String email); public User getUserByEmailAndPassword(String email,String password); + + public void insertNewContext(UserBlog userBlog); + + public List getUserBlogs(int userID); + + public String getBlogContext(int textID); + public UserBlog getUserBlog(int textID); } diff --git a/src/main/java/cn/zyq/vesionone/service/impl/UserServiceImpl.java b/src/main/java/cn/zyq/vesionone/service/impl/UserServiceImpl.java index 0c9ae938ebb13291d83d65f895a65d80d6240465..82ea0fb2feada83b8820775dddcf6d3400a3aa3e 100644 --- a/src/main/java/cn/zyq/vesionone/service/impl/UserServiceImpl.java +++ b/src/main/java/cn/zyq/vesionone/service/impl/UserServiceImpl.java @@ -2,9 +2,11 @@ package cn.zyq.vesionone.service.impl; import cn.zyq.vesionone.dao.UserDAO; import cn.zyq.vesionone.pojo.User; +import cn.zyq.vesionone.pojo.UserBlog; import cn.zyq.vesionone.service.UserService; import java.sql.SQLException; +import java.util.List; public class UserServiceImpl implements UserService { UserDAO userDAO = new UserDAO(); @@ -38,4 +40,43 @@ public class UserServiceImpl implements UserService { return user; } + public void insertNewContext(UserBlog userBlog){ + try { + userDAO.insertUserBlog(userBlog); + userDAO.insertBlogContext(userBlog); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public List getUserBlogs(int userID){ + List userBlogs; + try { + userBlogs = userDAO.getUserBlogs(userID); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return userBlogs; + } + + public String getBlogContext(int textID){ + String context = null; + try { + context = userDAO.getBlogContext(textID); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return context; + } + + public UserBlog getUserBlog(int textID){ + UserBlog userBlog = null; + try { + userBlog = userDAO.getUserBlog(textID); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return userBlog; + } + } diff --git a/src/main/java/cn/zyq/vesionone/utils/Utils.java b/src/main/java/cn/zyq/vesionone/utils/Utils.java new file mode 100644 index 0000000000000000000000000000000000000000..81eec52d01b2ab955e73738cb6bbad2229ee2aa5 --- /dev/null +++ b/src/main/java/cn/zyq/vesionone/utils/Utils.java @@ -0,0 +1,28 @@ +package cn.zyq.vesionone.utils; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; + +public class Utils { + + public static String getCurrentTime(){ + DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月和小时的格式为两个大写字母 + Date date = new Date();//获得当前时间 + String currentTime = df.format(date);//将当前时间转换成特定格式的时间字符串,这样便可以插入到数据库中 + + return currentTime; + } + + public static String getBriefInfo(String context){ + String str = ""; + if ( context.length() > 100 ) + str = context.substring(0,100); + else { + str = context.substring(0,context.length()); + } + + return str; + } + +} diff --git a/src/main/java/cn/zyq/vesionone/web/DetailBlogContextServlet.java b/src/main/java/cn/zyq/vesionone/web/DetailBlogContextServlet.java new file mode 100644 index 0000000000000000000000000000000000000000..d25f4c107574601f0a42afa836e8778784d1d23b --- /dev/null +++ b/src/main/java/cn/zyq/vesionone/web/DetailBlogContextServlet.java @@ -0,0 +1,45 @@ +package cn.zyq.vesionone.web; + +import cn.zyq.vesionone.pojo.UserBlog; +import cn.zyq.vesionone.service.UserService; +import cn.zyq.vesionone.service.impl.UserServiceImpl; +import com.alibaba.fastjson.JSON; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; + +@WebServlet("/detailBlogContextServlet") +public class DetailBlogContextServlet extends HttpServlet { + + private UserService userService= new UserServiceImpl(); + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + System.out.println("/detailBlogContextServlet"); + // 获取session对象 + HttpSession session = request.getSession(); + + int textID = Integer.parseInt((String) session.getAttribute("textID")); + + // 使单次有效 + session.removeAttribute("textID"); + + UserBlog userBlog = userService.getUserBlog(textID); + + // 将对象转换为json对象 + String jsonString = JSON.toJSONString(userBlog); + +// System.out.println(jsonString); + + //响应数据 + response.setContentType("text/json;charset=utf-8"); + response.getWriter().write(jsonString); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + this.doGet(request, response); + } +} diff --git a/src/main/java/cn/zyq/vesionone/web/DetailBlogInfoServlet.java b/src/main/java/cn/zyq/vesionone/web/DetailBlogInfoServlet.java new file mode 100644 index 0000000000000000000000000000000000000000..ceb297669c787eedf41a15f69a3adf2bde7e6aaf --- /dev/null +++ b/src/main/java/cn/zyq/vesionone/web/DetailBlogInfoServlet.java @@ -0,0 +1,39 @@ +package cn.zyq.vesionone.web; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; + +@WebServlet("/detailBlogInfoServlet") +public class DetailBlogInfoServlet extends HttpServlet { + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + +// System.out.println("/detailBlogInfoServlet"); + + // 获取session对象 + HttpSession session = request.getSession(false); + +// System.out.println(session); + + String textID = request.getParameter("textID"); +// System.out.println(textID); + // 存储数据 + session.setAttribute("textID",textID); + + response.getWriter().write("http://localhost:8080/blog/details.html"); +// // 服务器内跳转 +// request.getRequestDispatcher("/edit.html").forward(request,response); +// // 重定向 +// String contextPath = request.getContextPath(); +// System.out.println(contextPath); +// response.sendRedirect(contextPath+"/details.html"); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + this.doGet(request, response); + } +} diff --git a/src/main/java/cn/zyq/vesionone/web/EditBlogServlet.java b/src/main/java/cn/zyq/vesionone/web/EditBlogServlet.java new file mode 100644 index 0000000000000000000000000000000000000000..0165cc88a071c39ef17f20144ef679e7bd3b0349 --- /dev/null +++ b/src/main/java/cn/zyq/vesionone/web/EditBlogServlet.java @@ -0,0 +1,63 @@ +package cn.zyq.vesionone.web; + +import cn.zyq.vesionone.pojo.User; +import cn.zyq.vesionone.pojo.UserBlog; +import cn.zyq.vesionone.service.UserService; +import cn.zyq.vesionone.service.impl.UserServiceImpl; +import cn.zyq.vesionone.utils.Utils; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; + + +@WebServlet("/editBlogServlet") +public class EditBlogServlet extends HttpServlet { + private UserService userService= new UserServiceImpl(); + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + // 1、获取session对象 + HttpSession session = request.getSession(); + // 2、获取数据 + User loginUser = (User)session.getAttribute("user"); + + // 解决乱码: POST getReader() + //设置字符输入流的编码,设置的字符集要和页面保持一致 + request.setCharacterEncoding("UTF-8"); + + // 获取编辑页面提交的数据 + String title = request.getParameter("title"); + String category = request.getParameter("category"); + String editContext = request.getParameter("my-editormd-html-code"); + // 测试用 +// System.out.println(title); +// System.out.println(category); +// System.out.println(editContext); + + // 实例化pojo + UserBlog newUserBlog = new UserBlog(); + + newUserBlog.setUserID(loginUser.getUserID()); + newUserBlog.setTitle(title); + newUserBlog.setPublishDate(Utils.getCurrentTime()); + newUserBlog.setBriefInfo(Utils.getBriefInfo(editContext)); + newUserBlog.setContext(editContext); + + + userService.insertNewContext(newUserBlog); + + +// // 服务器内跳转 +// request.getRequestDispatcher("/List.html").forward(request,response); + // 重定向 + String contextPath = request.getContextPath(); + response.sendRedirect(contextPath+"/List.html"); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + this.doGet(request, response); + } +} diff --git a/src/main/java/cn/zyq/vesionone/web/LoadUserBlogsServlet.java b/src/main/java/cn/zyq/vesionone/web/LoadUserBlogsServlet.java new file mode 100644 index 0000000000000000000000000000000000000000..d15a842d54fd4c65e65cc8385aeaadc09b742d93 --- /dev/null +++ b/src/main/java/cn/zyq/vesionone/web/LoadUserBlogsServlet.java @@ -0,0 +1,46 @@ +package cn.zyq.vesionone.web; + +import cn.zyq.vesionone.pojo.User; +import cn.zyq.vesionone.pojo.UserBlog; +import cn.zyq.vesionone.service.UserService; +import cn.zyq.vesionone.service.impl.UserServiceImpl; +import com.alibaba.fastjson.JSON; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.util.List; + +@WebServlet("/loadUserBlogsServlet") +public class LoadUserBlogsServlet extends HttpServlet { + + private UserService userService= new UserServiceImpl(); + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 处理用户博客内容加载 + + // 从session中获取数据 + // 1、获取session对象 + HttpSession session = request.getSession(); + // 2、获取数据 + User loginUser = (User) session.getAttribute("user"); + + // 调用service查询 + List blogs = userService.getUserBlogs(loginUser.getUserID()); + + // 将集合转换为json数据 序列化 + String jsonString = JSON.toJSONString(blogs); +// System.out.println(jsonString); // 测试 + + // 响应数据 + response.setContentType("text/json;charset=utf-8"); + response.getWriter().write(jsonString); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + this.doGet(request, response); + } +} diff --git a/src/main/java/cn/zyq/vesionone/web/LogOffServlet.java b/src/main/java/cn/zyq/vesionone/web/LogOffServlet.java new file mode 100644 index 0000000000000000000000000000000000000000..c9e6bb547b2609b3814a97df7da913772038888e --- /dev/null +++ b/src/main/java/cn/zyq/vesionone/web/LogOffServlet.java @@ -0,0 +1,31 @@ +package cn.zyq.vesionone.web; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; + +@WebServlet("/logOffServlet") +public class LogOffServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + //注销就是通过session对象删除保存的用户信息 + HttpSession session = request.getSession(false); + if(session != null){ + // 销毁session + session.invalidate(); + } + + +// // 服务器内跳转 +// request.getRequestDispatcher("/betterUIForLR.html").forward(request,response); + // 重定向 + String contextPath = request.getContextPath(); + response.sendRedirect(contextPath+"/betterUIForLR.html"); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + this.doGet(request, response); + } +} diff --git a/src/main/java/cn/zyq/vesionone/web/RedirectServlet.java b/src/main/java/cn/zyq/vesionone/web/RedirectServlet.java new file mode 100644 index 0000000000000000000000000000000000000000..c60a4964d4599b0c34a935a8268905b22c670520 --- /dev/null +++ b/src/main/java/cn/zyq/vesionone/web/RedirectServlet.java @@ -0,0 +1,27 @@ +package cn.zyq.vesionone.web; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; + +@WebServlet("/redirectServlet") +public class RedirectServlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + +// String requestURI = request.getRequestURI(); + String target = request.getParameter("target"); + System.out.println(target); + // // 服务器内跳转 +// request.getRequestDispatcher("/betterUIForLR.html").forward(request,response); + // 重定向 + String contextPath = request.getContextPath(); + response.sendRedirect(contextPath+"/"+target); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + this.doGet(request, response); + } +} diff --git a/src/main/resources/development_process.txt b/src/main/resources/development_process.txt index b250d79a057a0780219740f4073fd16c7cb89578..ea230e5578df1dcc21c482d198b4c793586604eb 100644 --- a/src/main/resources/development_process.txt +++ b/src/main/resources/development_process.txt @@ -1,7 +1,17 @@ -前端界面 done -登录功能 done -注册功能 done -servlet和静态页面通过session和Ajax进行数据交互 done -利用cookie实现记住登录功能 done -完善用户写博客功能 to do -完善忘记密码功能 to do +version 1 + 前端界面 done + 登录功能 done + 注册功能 done + servlet和静态页面通过session和Ajax进行数据交互 done + 利用cookie实现记住登录功能 done + 完成注销功能 done + 完成忘记密码功能 done + 完成户写博客功能 done + 完成未登录访问或许页面拦截器 done + 提交编辑但是中文乱码 done + 查询当前用户博客信息功能 done + 用户列表界面加载博客功能 to do + + + + diff --git a/src/main/webapp/List.html b/src/main/webapp/List.html index c5a4cb8cf42f9b321fcb248465a8e7c369f37822..943987811adb6e5ab6f0fd4e99d6195ecb60aff4 100644 --- a/src/main/webapp/List.html +++ b/src/main/webapp/List.html @@ -16,7 +16,7 @@
@@ -26,7 +26,7 @@
游客
@@ -39,7 +39,7 @@
-
+

我的第一篇博客

2022-08-09 09:31:28

@@ -48,14 +48,7 @@ similique quaerat vel. Facere,et.

-
-

我的第二篇博客

-

2022-08-11 16:31:35

-

从今天起, 我要认真敲代码. Lorem ipsum, dolor sit amet consectetur adipisicing elit. - Cum distinctio ullam eum ut veroab laborum numquam tenetur est in dolorum a sint, assumenda adipisci - similique quaerat vel. Facere,et.

- -
+
@@ -69,15 +62,64 @@ method: "get", url:"http://localhost:8080/blog/loadUserInfoServlet", }).then(function (resp){ + // 加载 let currentLoginUser = resp.data; document.getElementById("currentUserName").innerHTML = currentLoginUser.userName; - }) + }); + + axios({ + method: "get", + url: "http://localhost:8080/blog/loadUserBlogsServlet", + }).then( function (resp){ + // 加载 + let blogs = resp.data; + let blogHtml = ""; + for ( let i = 0; i < blogs.length; i++){ + let blog = blogs[i]; + blogHtml += "\n"+ + "
" + + "

"+ blog.title +"

" + + "

"+ blog.publishDate +"

" + + "

" + blog.briefInfo + "

"+ + "
"+ + " 查看全文"+ + "
"+ + "
"+ + "

"+ + "\n"; + } + // 设置块内数据 + document.getElementById("showBlogs").innerHTML = blogHtml; + }); + + // 从新加载css 不管用 + // let link = document.createElement("link"); + // link.rel = "stylesheet"; + // // link.type = "text/css"; + // link.href = "./css/List.css"; + // document.getElementsByTagName("head")[0].appendChild(link); } + + + + \ No newline at end of file diff --git a/src/main/webapp/betterUIForLR.html b/src/main/webapp/betterUIForLR.html index 9153687ecbd8f5a05ba5d84f32e59be365e94fcf..ef3a626144b4c700014c0223bd24166eab8e6286 100644 --- a/src/main/webapp/betterUIForLR.html +++ b/src/main/webapp/betterUIForLR.html @@ -131,7 +131,7 @@ else { document.getElementById("LoginEmail").value = returnData.email; document.getElementById("LoginPassword").value = returnData.password; - document.getElementById("loginForm").submit(); + // document.getElementById("loginForm").submit(); } } ) } diff --git a/src/main/webapp/details.html b/src/main/webapp/details.html index a45e7c805ed0ab386d046310b05716d1884e7909..52433591ce1b610284e76f4b1da46c72aeb0949c 100644 --- a/src/main/webapp/details.html +++ b/src/main/webapp/details.html @@ -6,6 +6,7 @@ +
@@ -15,8 +16,9 @@
@@ -24,7 +26,7 @@
-
小熊吃糖
+
小熊吃糖
@@ -40,28 +42,49 @@
-
-

我的第一篇博客

-

2022-08-09 09:31:28

-

-

- 从今天起我要好好敲代码.Lorem ipsum dolor sit amet, - consectetur adipisicing elit. Aut recusandae omnis natus ut! Autem alias ullam sit facilis ipsa dolore, - molestiae neque aperiam in a facere dolor mollitia dolorum animi. - Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aut recusandae omnis natus ut! Autem alias - ullam sit facilis ipsa dolore, molestiae neque aperiam in a facere dolor mollitia dolorum animi. Lorem - ipsum dolor sit amet, consectetur adipisicing elit. - Aut recusandae omnis natus ut! Autem alias ullam sit facilis ipsa dolore, molestiae neque aperiam in a - facere dolor mollitia dolorum animi.

-

- Lorem ipsum dolor sit amet consectetur adipisicing elit. Laudantium sint accusantium, enim iste - corrupti itaque, omnis alias maiores nemo quae rerum deleniti facere officiis iure velit. - Blanditiis pariatur delectus perferendis. Lorem ipsum dolor sit amet consectetur adipisicing elit. - Laudantium sint accusantium, enim iste corrupti itaque, - omnis alias maiores nemo quae rerum deleniti facere officiis iure velit. Blanditiis pariatur delectus - perferendis.

+
+
+ + + + + + + + \ No newline at end of file diff --git a/src/main/webapp/edit.html b/src/main/webapp/edit.html index dfd51d4dd15349f3c7c49398090cdd18a376cc7d..56632ff9cce0341688a970f3ef7915ca21b36d3e 100644 --- a/src/main/webapp/edit.html +++ b/src/main/webapp/edit.html @@ -15,25 +15,27 @@ 我的博客系统
- -
+
- - - + 博客标题: + 博客分类: +
-
- +
+ + +
+ @@ -41,16 +43,72 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/webapp/js/utils.js b/src/main/webapp/js/utils.js new file mode 100644 index 0000000000000000000000000000000000000000..a5e27b243ec4eccffa2d61785c0f77427c11740c --- /dev/null +++ b/src/main/webapp/js/utils.js @@ -0,0 +1,27 @@ +// import './axios-0.18.0' + +function haha(){ + alert("hhaa") +} + +function backToListDotHTML(){ + alert("jjj") + //1. 创建核心对象 + var xhttp; + if (window.XMLHttpRequest) { + xhttp = new XMLHttpRequest(); + } else { + // code for IE6, IE5 + xhttp = new ActiveXObject("Microsoft.XMLHTTP"); + } + //2. 发送请求 + xhttp.open("GET", "http://localhost:8080/blog/redirectServlet?target=List.html"); + xhttp.send(); + //3. 获取响应 + xhttp.onreadystatechange = function() { + if (this.readyState == 4 && this.status == 200) { + alert(this.responseText); + } + }; + +} diff --git a/src/test/java/Test.java b/src/test/java/Test.java new file mode 100644 index 0000000000000000000000000000000000000000..5f9cda99349ef5cceb7527618f5dc639712a564d --- /dev/null +++ b/src/test/java/Test.java @@ -0,0 +1,15 @@ +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; + +public class Test { + + @org.junit.Test + public void m1(){ + DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月和小时的格式为两个大写字母 + Date date = new Date();//获得当前时间 + String birthday = df.format(date);//将当前时间转换成特定格式的时间字符串,这样便可以插入到数据库中 + System.out.println(birthday); + } + +}