一、功能
二、效果如下
三、代码如下
User.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>普通用户功能页</title> 8 <link rel="stylesheet" href=".//layui/css/layui.css" media="all"> 9 <script src=".//layui/layui.all.js"></script> 10 <script src=".//layui/layui.js"></script> 11 <script type="text/javascript"> 12 //JavaScript代码区域 13 layui.use('element', function() { 14 var element = layui.element; 15 }); 16 </script> 17 </head> 18 <body class="layui-layout-body"> 19 20 <div class="layui-layout layui-layout-admin"> 21 <div class="layui-header"> 22 <div class="layui-logo"> 23 <% 24 String identity = (String) session.getAttribute("identity"); 25 out.print(identity); 26 %> 27 功能页欢迎 28 <% 29 String username = (String) session.getAttribute("username"); 30 out.print(username); 31 %> 32 </div> 33 <!-- 水平导航 --> 34 <ul class="layui-nav layui-layout-right"> 35 <li class="layui-nav-item "><a href="index.jsp">退出</a></li> 36 </ul> 37 </div> 38 39 <div class="layui-side layui-bg-black"> 40 <div class="layui-side-scroll"> 41 <!-- 左侧导航 --> 42 <ul class="layui-nav layui-nav-tree"> 43 <li class="layui-nav-item"><a href="UserServlet?method=find" target="frame">浏览稿件</a></li> 44 </ul> 45 </div> 46 </div> 47 <div class="layui-body"> 48 <!-- 内容主体区域 --> 49 <iframe name="frame" width="100%" height="100%" 50 style="border: 1px solid #CCC;"></iframe> 51 </div> 52 <div> 53 <div> 54 <input type="text" id="date" /> 55 </div> 56 </div> 57 </div> 58 59 </body> 60 </html>
User_OpposeOpinion.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>提出反对意见</title> 8 <link rel="stylesheet" href=".//layui/css/layui.css" media="all"> 9 <script src=".//layui/layui.all.js"></script> 10 <script src=".//layui/layui.js"></script> 11 </head> 12 <body> 13 <form class="layui-form" action="UserServlet?method=update_user_opinion" 14 method="post"> 15 <div class="layui-form-item layui-form-text"> 16 <label class="layui-form-label">意见</label> 17 <div class="layui-input-block"> 18 <input type="text" name="user_oppose_opinion" required lay-verify="required" 19 placeholder="请输入您的意见" autocomplete="off" class="layui-input"> 20 </div> 21 </div> 22 <div class="layui-form-item"> 23 <div class="layui-input-block"> 24 <button class="layui-btn" lay-submit>提交</button> 25 <button type="reset" class="layui-btn layui-btn-primary">重置</button> 26 </div> 27 </div> 28 </form> 29 </body> 30 </html>
User_liulangaojian.jsp
1 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>浏览稿件</title> 9 <link rel="stylesheet" href=".//layui/css/layui.css" media="all"> 10 <script src=".//layui/layui.all.js"></script> 11 <script src=".//layui/layui.js"></script> 12 </head> 13 <script> 14 layui.use('laydate', function() { 15 var laydate = layui.laydate; 16 17 //执行一个laydate实例 18 laydate.render({ 19 elem : '#time' //指定元素 20 }); 21 }); 22 </script> 23 <body> 24 <form class="layui-form" action="UserServlet?method=Chaxun" 25 method="post"> 26 <div class="layui-form-item"> 27 <label class="layui-form-label">时间</label> 28 <div class="layui-inline"> 29 <!-- 注意:这一层元素并不是必须的 --> 30 <input type="text" class="layui-input" id="time" name="time"> 31 </div> 32 <div class="layui-inline"> 33 <button class="layui-btn" lay-submit>查询</button> 34 </div> 35 </div> 36 </form> 37 <table class="layui-table"> 38 <thead> 39 <tr> 40 <th>id</th> 41 <th>中文标题</th> 42 <th>中文摘要</th> 43 <th>中文关键字</th> 44 <th>操作</th> 45 </tr> 46 </thead> 47 <tbody> 48 <c:forEach items="${list}" var="story" varStatus="status"> 49 <tr> 50 <td>${ status.index + 1}</td> 51 <td>${story.ctitle}</td> 52 <td>${story.cabstract}</td> 53 <td>${story.ckey}</td> 54 <td><button class="layui-btn layui-btn-normal" 55 onclick="window.location.href='UserServlet?method=showAll&ctitle=${story.ctitle}'">查看详情</button></td> 56 </tr> 57 </c:forEach> 58 </tbody> 59 </table> 60 </body> 61 </html>
User_liulangaojian_check.jsp
1 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>浏览稿件</title> 9 <link rel="stylesheet" href=".//layui/css/layui.css" media="all"> 10 <script src=".//layui/layui.all.js"></script> 11 <script src=".//layui/layui.js"></script> 12 </head> 13 <body> 14 <c:forEach items="${list}" var="story"> 15 <div class="layui-form-item"> 16 <label class="layui-form-label">中文标题</label> 17 <div class="layui-input-block"> 18 <input type="text" name="ctitle" required lay-verify="required" 19 placeholder="请输入中文标题" autocomplete="off" class="layui-input" 20 value="${story.ctitle}"> <input type="text" 21 name="judgectitle" required lay-verify="required" 22 placeholder="请输入中文标题" autocomplete="off" class="layui-input" 23 value="${story.ctitle}"> 24 </div> 25 <div class="layui-form-mid layui-word-aux">(限100字以内)</div> 26 </div> 27 <div class="layui-form-item layui-form-text"> 28 <label class="layui-form-label">英文标题</label> 29 <div class="layui-input-block"> 30 <input type="text" name="etitle" required lay-verify="required" 31 placeholder="请输入英文标题" autocomplete="off" class="layui-input" 32 value="${story.etitle}"> 33 </div> 34 <div class="layui-form-mid layui-word-aux">(限400个字符以内)</div> 35 </div> 36 <div class="layui-form-item"> 37 <label class="layui-form-label">中文摘要</label> 38 <div class="layui-inline"> 39 <input type="text" name="cabstract" required lay-verify="required" 40 placeholder="请输入中文摘要" autocomplete="off" class="layui-input" 41 value="${story.cabstract}"> 42 </div> 43 <div class="layui-form-mid layui-word-aux">(限300字以内)</div> 44 </div> 45 <div class="layui-form-item"> 46 <label class="layui-form-label">英文摘要</label> 47 <div class="layui-inline"> 48 <input type="text" name="eabstract" required lay-verify="required" 49 placeholder="请输入英文摘要" autocomplete="off" class="layui-input" 50 value="${story.eabstract}"> 51 </div> 52 <div class="layui-form-mid layui-word-aux">(限3000字符以内)</div> 53 </div> 54 <div class="layui-form-item"> 55 <label class="layui-form-label">中文关键字</label> 56 <div class="layui-inline"> 57 <input type="text" name="ckey" required lay-verify="required" 58 placeholder="请输入中文关键字" autocomplete="off" class="layui-input" 59 value="${story.ckey}"> 60 </div> 61 <div class="layui-form-mid layui-word-aux">(限100字以内)</div> 62 </div> 63 <div class="layui-form-item"> 64 <label class="layui-form-label">英文关键字</label> 65 <div class="layui-inline"> 66 <input type="text" name="ekey" required lay-verify="required" 67 placeholder="请输入英文关键字" autocomplete="off" class="layui-input" 68 value="${story.ekey}"> 69 </div> 70 <div class="layui-form-mid layui-word-aux">(限200个字符以内)</div> 71 </div> 72 <div class="layui-form-item layui-form-text"> 73 <label class="layui-form-label">稿件正文</label> 74 <div class="layui-input-block"> 75 <textarea name="content" placeholder="请输入稿件正文" 76 class="layui-textarea">${story.content}</textarea> 77 </div> 78 </div> 79 <div class="layui-form-item"> 80 <label class="layui-form-label">投稿人</label> 81 <div class="layui-input-block"> 82 <input type="text" name="name" required lay-verify="required" 83 placeholder="请输入您的账号" autocomplete="off" class="layui-input" 84 value="${story.name}"> 85 </div> 86 </div> 87 <div class="layui-form-item"> 88 <div class="layui-input-block"> 89 <button class="layui-btn layui-btn-normal" 90 onclick="window.location.href='UserServlet?method=find'">返回</button> 91 <button class="layui-btn layui-btn-normal" 92 onclick="window.location.href='User_OpposeOpinion.jsp'">点击此处,可提反对有意见</button> 93 </div> 94 </div> 95 </c:forEach> 96 </body> 97 </html>
UserServlet(在servlet文件下建立此java文件)
1 package servlet; 2 3 import java.io.IOException; 4 import java.util.List; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 import javax.servlet.http.HttpSession; 12 import javax.swing.JOptionPane; 13 14 import dao.UserDao; 15 import util.Story; 16 17 /** 18 * Servlet implementation class UserServlet 19 */ 20 @WebServlet("/UserServlet") 21 public class UserServlet extends HttpServlet { 22 private static final long serialVersionUID = 1L; 23 24 /** 25 * @see HttpServlet#HttpServlet() 26 */ 27 public UserServlet() { 28 super(); 29 // TODO Auto-generated constructor stub 30 } 31 32 UserDao dao = new UserDao(); 33 34 /** 35 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse 36 * response) 37 */ 38 protected void doGet(HttpServletRequest request, HttpServletResponse response) 39 throws ServletException, IOException { 40 // TODO Auto-generated method stub 41 // response.getWriter().append("Served at: ").append(request.getContextPath()); 42 doPost(request, response); 43 } 44 45 /** 46 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse 47 * response) 48 */ 49 protected void doPost(HttpServletRequest request, HttpServletResponse response) 50 throws ServletException, IOException { 51 // TODO Auto-generated method stub 52 // doGet(request, response); 53 request.setCharacterEncoding("utf-8"); 54 response.setCharacterEncoding("utf-8"); 55 56 System.out.println("进入ContributeServlet"); 57 String method = request.getParameter("method"); 58 System.out.println(method); 59 if ("find".equals(method)) { 60 find(request, response);// 浏览稿件 61 } else if ("showAll".equals(method)) { 62 showAll(request, response);// 查看详情 63 } else if ("update_user_opinion".equals(method)) { 64 update_user_opinion(request, response);// 提出反对意见 65 }else if("Chaxun".equals(method)) { 66 Chaxun(request, response);// 通过时间查询 67 } 68 } 69 70 public void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 71 response.setCharacterEncoding("UTF-8"); 72 request.setCharacterEncoding("UTF-8"); 73 74 List<Story> list = dao.find(); 75 request.setAttribute("list", list); 76 request.getRequestDispatcher("User_liulangaojian.jsp").forward(request, response); 77 78 } 79 80 public void showAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 81 response.setCharacterEncoding("UTF-8"); 82 request.setCharacterEncoding("UTF-8"); 83 84 String ctitle = request.getParameter("ctitle"); 85 86 // 使用request对象的getSession()获取session,如果session不存在则创建一个 87 HttpSession session = request.getSession(); 88 // 将数据存储到session中 89 session.setAttribute("ctitle", ctitle); 90 91 List<Story> list = dao.showAll(ctitle); 92 request.setAttribute("list", list); 93 request.getRequestDispatcher("User_liulangaojian_check.jsp").forward(request, response); 94 95 } 96 97 public void update_user_opinion(HttpServletRequest request, HttpServletResponse response) 98 throws ServletException, IOException { 99 response.setCharacterEncoding("UTF-8"); 100 request.setCharacterEncoding("UTF-8"); 101 102 String user_oppose_opinion = request.getParameter("user_oppose_opinion"); 103 104 // 使用request对象的getSession()获取session,如果session不存在则创建一个 105 HttpSession session = request.getSession(); 106 // 将数据取出 107 String ctitle = (String) session.getAttribute("ctitle"); 108 109 if (dao.update_user_opinion(ctitle, user_oppose_opinion)) { 110 JOptionPane.showMessageDialog(null, "提交成功!", "SUCCESS", JOptionPane.PLAIN_MESSAGE); 111 request.getRequestDispatcher("UserServlet?method=find").forward(request, response); 112 } else { 113 JOptionPane.showMessageDialog(null, "提交失败!请重新提交!", "FAIL", JOptionPane.PLAIN_MESSAGE); 114 request.getRequestDispatcher("User_OpposeOpinion.jsp").forward(request, response); 115 } 116 117 } 118 119 public void Chaxun(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 120 response.setCharacterEncoding("UTF-8"); 121 request.setCharacterEncoding("UTF-8"); 122 123 String time = request.getParameter("time"); 124 125 List<Story> list = dao.Chaxun(time); 126 request.setAttribute("list", list); 127 request.getRequestDispatcher("User_liulangaojian.jsp").forward(request, response); 128 129 } 130 131 }
UserDao(在dao文件下建立此java文件)
1 package dao; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.util.ArrayList; 7 import java.util.List; 8 9 import databse.Database; 10 import util.Story; 11 12 public class UserDao { 13 /** 14 * 浏览稿件 15 * 16 * @return 17 */ 18 public List<Story> find() { 19 List<Story> list = new ArrayList<>(); 20 Connection conn = null; 21 ResultSet rs = null; 22 PreparedStatement pstm = null; 23 Story story = null; 24 try { 25 conn = Database.getConnection(); 26 String sql = "select * from story where receiver=?"; 27 pstm = conn.prepareStatement(sql); 28 pstm.setString(1, "已签发"); 29 rs = pstm.executeQuery(); 30 while (rs.next()) { 31 String ctitle = rs.getString("ctitle"); 32 String etitle = rs.getString("etitle"); 33 String cabstract = rs.getString("cabstract"); 34 String eabstract = rs.getString("eabstract"); 35 String ckey = rs.getString("ckey"); 36 String ekey = rs.getString("ekey"); 37 String content = rs.getString("content"); 38 String receiver = rs.getString("receiver"); 39 String status = rs.getString("status"); 40 String name = rs.getString("name"); 41 story = new Story(ctitle, etitle, cabstract, eabstract, ckey, ekey, content, receiver, status, name); 42 list.add(story); 43 } 44 } catch (Exception e) { 45 e.printStackTrace(); 46 } finally { 47 Database.close(conn, pstm, rs); 48 } 49 return list; 50 } 51 52 /** 53 * 查看详情 54 * 55 * @param ctitle 56 * @return 57 */ 58 public List<Story> showAll(String ctitle) { 59 List<Story> list = new ArrayList<>(); 60 Connection conn = null; 61 ResultSet rs = null; 62 PreparedStatement pstm = null; 63 Story story = null; 64 try { 65 conn = Database.getConnection(); 66 String sql = "select * from story where ctitle=?"; 67 pstm = conn.prepareStatement(sql); 68 pstm.setString(1, ctitle); 69 rs = pstm.executeQuery(); 70 while (rs.next()) { 71 String ctitlee = rs.getString("ctitle"); 72 String etitle = rs.getString("etitle"); 73 String cabstract = rs.getString("cabstract"); 74 String eabstract = rs.getString("eabstract"); 75 String ckey = rs.getString("ckey"); 76 String ekey = rs.getString("ekey"); 77 String content = rs.getString("content"); 78 String receiver = rs.getString("receiver"); 79 String status = rs.getString("status"); 80 String name = rs.getString("name"); 81 story = new Story(ctitlee, etitle, cabstract, eabstract, ckey, ekey, content, receiver, status, name); 82 list.add(story); 83 } 84 } catch (Exception e) { 85 e.printStackTrace(); 86 } finally { 87 Database.close(conn, pstm, rs); 88 } 89 return list; 90 } 91 92 /** 93 * 提交反对意见 94 * 95 * @param ctitle 96 * @param user_oppose_opinion 97 * @return 98 */ 99 public boolean update_user_opinion(String ctitle, String user_oppose_opinion) { 100 Connection conn = null; 101 PreparedStatement pstm = null; 102 PreparedStatement pstm1 = null; 103 ResultSet rs = null; 104 boolean judge = false; 105 String user_oppose_opinion_update=""; 106 try { 107 conn = Database.getConnection(); 108 // 通过SQL语句查询该稿件所对应的反对意见 109 String findOpinion = "select user_oppose_opinion from storysituation where ctitle=?"; 110 pstm = conn.prepareStatement(findOpinion); 111 pstm.setString(1, ctitle); 112 rs = pstm.executeQuery(); 113 while (rs.next()) { 114 user_oppose_opinion_update = rs.getString("user_oppose_opinion"); 115 } 116 if(user_oppose_opinion_update.equals("null")) { 117 user_oppose_opinion_update=user_oppose_opinion; 118 }else { 119 user_oppose_opinion_update = user_oppose_opinion_update + "~~" + user_oppose_opinion; 120 } 121 122 // SQL 追加 123 String sql = "update storysituation set user_oppose_opinion=? where ctitle=?"; 124 pstm1 = conn.prepareStatement(sql); 125 pstm1.setString(1, user_oppose_opinion_update); 126 pstm1.setString(2, ctitle); 127 128 // 执行插入操作 129 int num = pstm1.executeUpdate(); 130 if (num > 0) { 131 System.out.println("提交反对意见成功"); 132 judge = true; 133 } else { 134 System.out.println("提交反对意见失败"); 135 judge = false; 136 } 137 } catch (Exception e) { 138 e.printStackTrace(); 139 } finally { 140 // SQL执行完成后释放相关资源 141 Database.close(conn, pstm); 142 } 143 return judge; 144 } 145 146 /** 147 * 通过时间查询稿件 148 * 149 * @param time 150 * @return 151 */ 152 public List<Story> Chaxun(String time) { 153 List<Story> list = new ArrayList<>(); 154 Connection conn = null; 155 ResultSet rs = null; 156 PreparedStatement pstm = null; 157 Story story = null; 158 try { 159 conn = Database.getConnection(); 160 String sql = "select * from story where time=?"; 161 pstm = conn.prepareStatement(sql); 162 pstm.setString(1, time); 163 rs = pstm.executeQuery(); 164 while (rs.next()) { 165 String ctitle = rs.getString("ctitle"); 166 String etitle = rs.getString("etitle"); 167 String cabstract = rs.getString("cabstract"); 168 String eabstract = rs.getString("eabstract"); 169 String ckey = rs.getString("ckey"); 170 String ekey = rs.getString("ekey"); 171 String content = rs.getString("content"); 172 String receiver = rs.getString("receiver"); 173 String status = rs.getString("status"); 174 String name = rs.getString("name"); 175 story = new Story(ctitle, etitle, cabstract, eabstract, ckey, ekey, content, receiver, status, name); 176 list.add(story); 177 } 178 } catch (Exception e) { 179 e.printStackTrace(); 180 } finally { 181 Database.close(conn, pstm, rs); 182 } 183 return list; 184 } 185 }