1. 前端开发
对于前端页面,通过对原页面的不断观察与探索,最终模仿出了原页面(包含JS动画)。
1.1 开发过程
学院网站开发
1.2 结果展示
模仿_西南石油大学 - 计科院
2. 后台开发
2.1 主页中显示数据库中的新闻
首先,要获取各类型的新闻列表,那么需要在WebService(MVC中我的service对应的类,详情见我博文:JSP显示新闻列表)中添加相应逻辑的代码,纵观逻辑其实和前面的WebService.getNews()方法差不多,只不过sql脚本需要添加一个判断条件而已。为了提高代码的复用性,减少代码冗余,我将WebService.getNews()方法进行了一点修改:
/** * 获取指定类型的所有的新闻 * * @param type 新闻的类型 * @return 新闻列表 */ public static ListgetNews(String type) { String sql; // 判断是否查询所有的新闻 if (type.equals("*")) { sql = "select * from News"; } else { sql = "select * from News where _type='" + type + "' and _state='Done'"; } DB db = new DB(); List newsList = new ArrayList<>(); ResultSet rs = db.executeQuery(sql); try { while (rs.next()) { News news = new News(); news.setId(rs.getString("id")); news.setTitle(rs.getString("title")); news.setContent(rs.getString("content")); news.setType(News.Type.valueOf(rs.getString("_type"))); news.setDate(rs.getString("_date")); news.setState(News.State.valueOf(rs.getString("_state"))); newsList.add(news); } } catch (Exception e) { System.out.println(DB.getCurrentTime() + ",WebService->获取新闻处(getNews)引发了异常:\n" + e.getMessage()); } finally { db.closeConnection(); return newsList; } }
有了Model层的代码,只需要在Controller中进行相关的调用即可,在indi.haveadate.swpu.controller包下添加ShowHomepageServlet:
package indi.haveadate.swpu.controller; import indi.haveadate.swpu.entity.News; import indi.haveadate.swpu.service.WebService; 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 java.io.IOException; import java.util.List; /** * 前端显示新闻列表所对应的Servlet * * @author haveadate * @version 1.0 */ @WebServlet(urlPatterns = "/showHomePage") public class ShowHomepageServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 放到doGet方法中统一处理 doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置编码方式 request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); // 获取所有图片新闻模块新闻列表并传递给viewer ListimgNewsList = WebService.getNews(News.Type.Img.toString()); request.setAttribute("imgNewsList", imgNewsList); // 获取所有学术交流模块新闻列表并传递给viewer List academicNewsList = WebService.getNews(News.Type.Academic.toString()); request.setAttribute("academicNewsList", academicNewsList); // 获取所有新闻速递模块新闻列表并传递给viewer List expressNewsList = WebService.getNews(News.Type.Express.toString()); request.setAttribute("expressNewsList", expressNewsList); // 获取所有党建速递模块新闻列表并传递给viewer List partyNewsList = WebService.getNews(News.Type.Party.toString()); request.setAttribute("partyNewsList", partyNewsList); // 获取所有通知公告模块新闻列表并传递给viewer List announcementNewsList = WebService.getNews(News.Type.Announcement.toString()); request.setAttribute("announcementNewsList", announcementNewsList); // 获取所有专题列表模块新闻列表并传递给viewer List themeNewsList = WebService.getNews(News.Type.Theme.toString()); request.setAttribute("themeNewsList", themeNewsList); request.getRequestDispatcher("index.jsp").forward(request, response); } }
将前面写的学院网页复制到index.jsp,请注意,jsp页面默认引用路径都是web的子目录,所以对于学院原网页的相关引用要修改相对路径,在页面中用到了EL表达式中对字符串的截取[1],index.jsp文件如下:
<%-- Created by IntelliJ IDEA. User: 22934 Date: 2020/6/23 Time: 10:07 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>西南石油大学 - 计算机科学学院 <%--jsp默认路径都是在web子目录下--%>class="header center-block">class="h-top">class="h-nav">class="clearfix">
- 网站首页
- class="mainmenu"> 学院概况
- class="mainmenu"> 本科生教育
- class="mainmenu"> 研究生教育
class="submenu">
- 招生简章
- 计算机科学与技术一级学科
- 软件工程一级学科
- 网络空间安全一级学科
- 研究生导师
- 研究生奖助体系
- 通知公告
- 资料下载
- class="mainmenu"> 科学研究
- class="mainmenu"> 学生工作
- class="mainmenu"> 招生工作
- class="mainmenu"> 实验中心
- class="mainmenu"> 党建之窗
- class="h-nav-distress"> 抗击疫情
class="h-body">class="h-body-imgs">
- class="first">
class="h-body-last">class="h-body-next">class="h-body-switch clearfix">
- class="first">
class="content center-block">class="clearfix">
- class="c-img-news">
class="module-content clearfix">class="left">class="top">class="num_list clearfix c-img-news-switch">
- class="first">1
- 2
- 3
- 4
- 5
- 6
class="bottom"> 邹正伟老师获得“2019年度四川省优秀物联网教师”...class="right common_list_style">- class="c-academic-communication">
class="module-content common_list_style">- class="c-news-express">
class="module-content common_list_style">思政教育“不下线”,计科院成立湖北籍学生临时团支部
“我宣誓,听从召唤,坚持抗疫,为打赢疫情防控阻击战贡献青春力量!”日前,一场特殊的临时团支部成立仪式在网络上举行。 计算机科学学院63名湖北籍青年学子,对着电脑屏幕,庄严宣誓。 据了…[<a href="">详细信息]
- class="c-party-dynamic">
class="module-content common_list_style">- class="c-information-announcement">
class="module-content common_list_style">- class="c-theme-list">
class="module-content common_list_style">class="footer">Copyright© 2018 All Rights Reserved. 西南石油大学计算机科学学院
显示新闻效果如下:
2.2 JSP生成静态页面
暂时还未解决。
2.3 实现新闻编辑功能——UEditor工具的使用
首先,下载UEditor:http://ueditor.baidu.com/website/download.html,我下载的是JSP最新版
然后,将减压后的文件导入项目的web目录下:
通过引用UEditor的JS文件,创建一个简单的用于修改和添加新闻的前端页面[2]:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>添加新闻title>
<link rel="stylesheet" href="css/common.css">
<link rel="stylesheet" href="css/editNews.css">
<script src="js/jquery-3.4.1.min.js">script>
<script src="js/editNews.js">script>
<script src="ueditor1_4_3_3-utf8-jsp/ueditor.config.js">script>
<script src="ueditor1_4_3_3-utf8-jsp/ueditor.all.js">script>
head>
<body>
<form action="http://localhost:8080/swpu/addNews">
<input type="submit" value="提交" id="submit">
<h2 class="center-block" id="lbl_id">
ID
h2>
<input type="text" class=" center-block" id="id" placeholder="自动生成,无需编辑" disabled>
<h2 class="center-block" id="lbl_type">
类型
h2>
<select name="type" id="type" class=" center-block">
<option value="">请选择新闻类型option>
<option value="img">图片新闻option>
<option value="academic">学术交流option>
<option value="express">新闻速递option>
<option value="party">党建动态option>
<option value="announcement">通知公告option>
<option value="theme">专题列表option>
select>
<h2 class="center-block" id="lbl_title">
标题
h2>
<input type="text" class=" center-block" id="title" placeholder="请输入新闻标题标题">
<h2 class="center-block" id="lbl_content">
内容
h2>
<div>
<textarea name="content" id="content" class=" center-block" cols="30" rows="10">textarea>
div>
<script>
var ue = UE.getEditor("content");
setTimeout(function() {
ue.execCommand("drafts")
}, 2000);
script>
form>
body>
html>
editNews.css
/* * @Author: havedate * @Date: 2020-06-25 11:01:33 * @Last Modified by: mikey.zhaopeng * @Last Modified time: 2020-06-25 12:11:03 * @Funtion: 添加/编辑新闻页面css */ #lbl_id, #lbl_title, #lbl_content, #lbl_type { margin: 30px auto 10px; } #type, #id, #title { display: block; height: 40px; margin-bottom: 30px; line-height: 40px; font-size: 16px; text-indent: 10px; outline: none; } #content { display: block; height: 400px; outline: none; margin-bottom: 30px; } /* 提交按钮 */ #submit { position: fixed; right: 35px; bottom: 35px; width: 100px; height: 100px; font-size: 22px; line-height: 100px; text-align: center; color: #fff; background-color: rgb(255, 206, 68); border: 0; outline: none; cursor: pointer; border-radius: 50%; } #submit:hover { background-color: rgb(8, 122, 67); }
editNews.js暂时不做处理,紧接着后续功能进行完善。
2.4 实现添加新闻功能
如果要添加一条新闻,大家可以想象一下流程,应该是:点击添加按钮 -> editNews.html -> AddNewsServlet ->ShowNewsServlet->showNews.jsp,有了工作流程,那么我们就要着手开始写代码了。
首先是将我们前面写的editNews.html引入项目,并完善editNews.js,因为我们这里是直接提交表单信息,没有涉及到前后端交互,所以可以不用AJAX,即editNews.js可以不使用。
前端的工作完成了,剩下的就是添加AddNewsServlet:
package indi.haveadate.swpu.controller; import indi.haveadate.swpu.entity.News; import indi.haveadate.swpu.service.WebService; 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 java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; @WebServlet(urlPatterns = "/addNews") public class AddNewsServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 放到doGet方法中统一处理 doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置编码方式 request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); News news = new News(); news.setId(getCurrentTime()); news.setTitle(request.getParameter("title")); news.setContent(request.getParameter("content")); // 匹配新闻类型 switch (request.getParameter("type")){ case "img": news.setType(News.Type.Img); break; case "academic": news.setType(News.Type.Academic); break; case "express": news.setType(News.Type.Express); break; case "party": news.setType(News.Type.Party); break; case "announcement": news.setType(News.Type.Announcement); break; case "theme": news.setType(News.Type.Theme); break; } news.setDate(""); news.setState(News.State.Do); // 调用WebService处理复杂的逻辑 WebService.addNews(news); request.getRequestDispatcher("showNews").forward(request, response); } /*** * 获取当前时间(yyyyMMddHHmmss) * @return 当前时间 */ public static String getCurrentTime() { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); Date date = new Date(); return sdf.format(date); } }
Servlet只是一个“总管”(Controller),具体复杂的业务逻辑需要在WebService中,向WebService添加AddNews方法:
/** * 添加新闻 * * @param news 新闻对象 * @return 添加结果 */ public static boolean addNews(News news) { String sql = "insert into News values('" + news.getId() + "','" + news.getTitle() + "','" + news.getContent() + "','" + news.getType().toString() + "','" + news.getDate() + "','" + news.getState().toString() + "')"; DB db = new DB(); boolean result = db.executeUpdate(sql); db.closeConnection(); return result; }
可以看到,添加新闻的功能我们大致实现了:
从数据库中我们可以看到,UEditor自动为我们生成了标签,这极大的提高了后续新闻详细显示的开发效率!
2.5 实现删除新闻功能
同理,删除的流程大致为:点击删除按钮->DeleteNewsServlet->ShowNewsServlet->showNews.jsp,所以我们需要创建一个DeleteNewsServlet并对showNews.jsp做出一些调整。
同样,复杂的逻辑放在service中:
原jsp用的是input元素,不能直接跳转页面,可以采用JS代码弥补。
在WebService中添加的代码:
/** * 删除新闻 * @param id 新闻id * @return 删除结果 */ public static boolean deleteNews(String id) { String sql = "delete from News where id = '" + id + "'"; DB db = new DB(); boolean result = db.executeUpdate(sql); db.closeConnection(); return result; }
DeleteNewsServlet:
package indi.haveadate.swpu.controller; import indi.haveadate.swpu.service.WebService; 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 java.io.IOException; @WebServlet(urlPatterns = "/deleteNews") public class DeleteNewsServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 放到doGet方法中统一处理 doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置编码方式 request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String id = request.getParameter("id"); WebService.deleteNews(id); request.getRequestDispatcher("showNews").forward(request, response); } }
展示效果:
2.6 实现修改新闻功能
让我们来一起想象一下修改新闻的流程,大致是点击编辑按钮->GetANewsServlet->editNews.jsp->UpdateNewsServlet->ShowNewsServlet->showNews.jsp。有了流程,我们就开始编辑代码了。
首先需要添加一个GetANewsServlet,通过新闻id获取新闻信息,再传递给editNews.jsp:
WebService.getANews(String id):
/** * 获取指定的一条新闻 * @param id 新闻id * @return 指定的新闻 */ public static News getANews(String id) { String sql = "select * from News where id='" + id + "'"; DB db = new DB(); News news = new News(); ResultSet rs = db.executeQuery(sql); try { if (rs.next()) { news.setId(rs.getString("id")); news.setTitle(rs.getString("title")); news.setContent(rs.getString("content")); news.setType(News.Type.valueOf(rs.getString("_type"))); news.setDate(rs.getString("_date")); news.setState(News.State.valueOf(rs.getString("_state"))); } } catch (Exception e) { System.out.println(DB.getCurrentTime() + ",WebService->获取特定新闻处(getANews)引发了异常:\n" + e.getMessage()); } finally { db.closeConnection(); return news; } }
GetANewsServlet:
package indi.haveadate.swpu.controller; import indi.haveadate.swpu.entity.News; import indi.haveadate.swpu.service.WebService; 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 java.io.IOException; @WebServlet(urlPatterns = "/getANews") public class GetANewsServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 放到doGet方法中统一处理 doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置编码方式 request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String id = request.getParameter("id"); News news = WebService.getANews(id); request.setAttribute("news", news); request.getRequestDispatcher("scs_back/editNews.jsp").forward(request, response); } }
以editNews.html为基础,创建editNews.jsp(注意jsp默认引用文件位置是web目录的子目录):
editNews.jsp:
<%-- Created by IntelliJ IDEA. User: 22934 Date: 2020/6/25 Time: 14:18 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>添加新闻
复杂逻辑层WebService中添加updateNews方法:
package indi.haveadate.swpu.service; import indi.haveadate.swpu.dal.DB; import indi.haveadate.swpu.entity.News; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.GetMethod; import java.io.*; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; /** * 处理复杂的业务逻辑——服务逻辑层 * * @author haveadate * @version 2.0 */ public class WebService { /** * 用户登录 * * @param account 用户名 * @param password 密码 * @return 登录结果 */ public static boolean login(String account, String password) { String sql = "select * from users where id='" + account + "' and pwd='" + password + "'"; DB db = new DB(); ResultSet rs = db.executeQuery(sql); boolean result = false; // 判断数据库中是否有记录 try { if (rs.next()) { result = true; } } catch (SQLException e) { System.out.println(DB.getCurrentTime() + ",WebService->登录处(login)引发了异常:\n" + e.getMessage()); } finally { // 关闭数据库连接 db.closeConnection(); return result; } } /** * 获取指定类型的所有的新闻 * * @param type 新闻的类型 * @return 新闻列表 */ public static ListgetNews(String type) { String sql; // 判断是否查询所有的新闻 if (type.equals("*")) { sql = "select * from News order by _Type asc, ID desc"; } else { sql = "select * from News where _type='" + type + "' and _state='Done'"; } DB db = new DB(); List newsList = new ArrayList<>(); ResultSet rs = db.executeQuery(sql); try { while (rs.next()) { News news = new News(); news.setId(rs.getString("id")); news.setTitle(rs.getString("title")); news.setContent(rs.getString("content")); news.setType(News.Type.valueOf(rs.getString("_type"))); news.setDate(rs.getString("_date")); news.setState(News.State.valueOf(rs.getString("_state"))); newsList.add(news); } } catch (Exception e) { System.out.println(DB.getCurrentTime() + ",WebService->获取新闻处(getNews)引发了异常:\n" + e.getMessage()); } finally { db.closeConnection(); return newsList; } } /** * @basePath url host信息 * @target 需要转换的链接 * @path 文件要保存的真实路径 * @name 文件要保存的名字 * @extension 文件要保存的扩展名.html/.htm */ public static boolean jspToHtml(String basePath, String target, String path, String name, String extension) { boolean result = true; HttpClient client = new HttpClient(); GetMethod getMethod = new GetMethod(basePath + "/" + target); try { client.executeMethod(getMethod); File filePath = new File(path); if (!filePath.exists()) { filePath.mkdirs(); } File file = new File(path + name + extension); //FileWriter 无法指定编码格式,此处替代使用防止存储的文件乱码 Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")); out.write(getMethod.getResponseBodyAsString()); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); result = false; } return result; } /** * 添加新闻 * * @param news 新闻对象 * @return 添加结果 */ public static boolean addNews(News news) { String sql = "insert into News values('" + news.getId() + "','" + news.getTitle() + "','" + news.getContent() + "','" + news.getType().toString() + "','" + news.getDate() + "','" + news.getState().toString() + "')"; DB db = new DB(); boolean result = db.executeUpdate(sql); db.closeConnection(); return result; } /** * 删除新闻 * * @param id 新闻id * @return 删除结果 */ public static boolean deleteNews(String id) { String sql = "delete from News where id = '" + id + "'"; DB db = new DB(); boolean result = db.executeUpdate(sql); db.closeConnection(); return result; } /** * 获取指定的一条新闻 * * @param id 新闻id * @return 指定的新闻 */ public static News getANews(String id) { String sql = "select * from News where id='" + id + "'"; DB db = new DB(); News news = new News(); ResultSet rs = db.executeQuery(sql); try { if (rs.next()) { news.setId(rs.getString("id")); news.setTitle(rs.getString("title")); news.setContent(rs.getString("content")); news.setType(News.Type.valueOf(rs.getString("_type"))); news.setDate(rs.getString("_date")); news.setState(News.State.valueOf(rs.getString("_state"))); } } catch (Exception e) { System.out.println(DB.getCurrentTime() + ",WebService->获取特定新闻处(getANews)引发了异常:\n" + e.getMessage()); } finally { db.closeConnection(); return news; } } /** * 更改新闻 * * @param news 新闻对象 * @return 更改结果 */ public static boolean updateNews(News news) { String sql = "update News set title='" + news.getTitle() + "',content='" + news.getContent() + "',_type='" + news.getType().toString() + "' where id='" + news.getId() + "'"; DB db = new DB(); boolean result = db.executeUpdate(sql); db.closeConnection(); return result; } }
UpdateNewsServlet:
package indi.haveadate.swpu.controller; import indi.haveadate.swpu.entity.News; import indi.haveadate.swpu.service.WebService; 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 java.io.IOException; @WebServlet(urlPatterns = "/updateNews") public class UpdateNewsServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 放到doGet方法中统一处理 doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置编码方式 request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); News news = new News(); news.setId(request.getParameter("id")); news.setTitle(request.getParameter("title")); news.setContent(request.getParameter("content")); // 匹配新闻类型 switch (request.getParameter("type")) { case "img": news.setType(News.Type.Img); break; case "academic": news.setType(News.Type.Academic); break; case "express": news.setType(News.Type.Express); break; case "party": news.setType(News.Type.Party); break; case "announcement": news.setType(News.Type.Announcement); break; case "theme": news.setType(News.Type.Theme); break; } news.setDate(""); news.setState(News.State.Do); // 调用WebService处理复杂的逻辑 WebService.updateNews(news); request.getRequestDispatcher("showNews").forward(request, response); } }
查看效果:
2.7 实现新闻上架和下架功能
我对于新闻后台管理定制了上架和下架功能,新闻可以先保存,只有上架以后,主页才会看到,流程大致为点击上架/下架按钮->UpNewsServlet/DownNewsServlet->ShowNewsServlet。首先需要修改news.jsp代码:
对于上架下架功能,我们只需要使用UpNewsServlet、DownNewsServlet简单改变新闻表中State属性即可。
在WebService中添加的方法upNews和downNews:
/** * 新闻上架 * * @param id 新闻id * @return 新闻上架操作结果 */ public static boolean upNews(String id) { String sql = "update News set _state='Done' where id='" + id + "'"; DB db = new DB(); boolean result = db.executeUpdate(sql); db.closeConnection(); return result; } /** * 新闻下架 * * @param id 新闻id * @return 新闻下架操作结果 */ public static boolean downNews(String id) { String sql = "update News set _state='Do' where id='" + id + "'"; DB db = new DB(); boolean result = db.executeUpdate(sql); db.closeConnection(); return result; }
UpNewsServlet:
package indi.haveadate.swpu.controller; import indi.haveadate.swpu.service.WebService; 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 java.io.IOException; @WebServlet(urlPatterns = "/upNews") public class UpNewsServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 放到doGet方法中统一处理 doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置编码方式 request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String id = request.getParameter("id"); WebService.upNews(id); request.getRequestDispatcher("showNews").forward(request, response); } }
DownNewsServlet:
package indi.haveadate.swpu.controller; import indi.haveadate.swpu.service.WebService; 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 java.io.IOException; @WebServlet(urlPatterns = "/downNews") public class DownNewsServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 放到doGet方法中统一处理 doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置编码方式 request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String id = request.getParameter("id"); WebService.downNews(id); request.getRequestDispatcher("showNews").forward(request, response); } }
效果展示:
3. 源代码及效果展示
https://gitee.com/ihaveadate/Java-EE -> SWPU
模仿——西南石油大学 - 计算机科学学院 (账号:20173106001,密码:123456)
4. 附录
[1] lpftobetheone. EL表达式中如何截取字符串[J/OL]. CSDN. (2014-07-02)[2020-06-25]. https://blog.csdn.net/lpftobetheone/article/details/36439383
[2] 小破孩楼主. jsp中如何使用Ueditor[J/OL]. 博客园. (2018-08-30)[2020-06-25]. https://www.cnblogs.com/zouhong/p/9562219.html