目 录
1、进入管理员模块
①编写管理员页面代码
②在NewsController.java新闻控制类在前面的基础上后面加上以下代码语句
③效果图
2、添加新闻
①编写添加页面代码
②前往添加新闻页面在NewsController里面编写代码
③效果图
3、修改新闻
①编写修改页面代码
②前往添加新闻页面在NewsController里面编写代码
③效果图
4、修改新闻
①前往添加新闻页面在NewsController里面编写代码
②service代码写在NewsServiceImpl中
③效果图
1、进入管理员模块
首页查询出新闻的列表信息提供给管理员进行管理
①编写管理员页面代码
admin.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>新闻管理系统</title>
<link rel="stylesheet" href="/css/admin.css" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >
<link href="/css/bootstrap.min.css" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" rel="stylesheet">
</head>
<body>
<!-- 头部 -->
<div th:insert="common/common::admin-top"></div>
<div id="main">
<!-- 左侧菜单栏 -->
<div id="left" th:insert="common/common::leftMenu"></div>
<div id="opt_area">
<script type="text/javascript">
function clickdel(nid) {
if (confirm("此新闻的相关评论也将删除,确定删除吗?"))
window.location = "/news/deleteById/" + nid;
}
</script>
<ul class="classlist">
<li th:each="news : ${page.getRecords()} ">
[[${news.ntitle}]]
<span> 作者:[[${news.nauthor}]]
<a class="btn-sm btn btn-info" th:href='@{/news/edit/}+${news.nid}'>修改</a>
<!-- <a class="btn-sm btn btn-danger" href='javascript:;' th:onclick="clickdel([[${news.nid}]]">删除</a></span>-->
<a class="btn-sm btn btn-danger" href='javascript:;' th:onclick="clickdel([[${news.nid}]])">删除</a></span>
<!-- <a class="btn-sm btn btn-danger" href='javascript:;' th:onclick="clickdel([[${news.nid}]]">删除</a></span>-->
</li>
</ul>
<p align="right">
当前页数:[<span th:text="${page.current}">1</span>/<span th:text="${page.pages}">2</span>]
<span th:if="${page.current>1}">
<a th:href="@{/news/manager(pageNo=1)}" target="_blank" rel="external nofollow" >首页</a>
<a th:href="@{/news/manager(pageNo=${page.current - 1})}" target="_blank" rel="external nofollow" >上一页</a>
</span>
<span th:if="${page.current<=1}">
首页
上一页
</span>
<span th:if="${page.current<page.pages}">
<a th:href="@{/news/manager(pageNo=${page.current + 1})}" target="_blank" rel="external nofollow" >下一页</a>
<a th:href="@{/news/manager(pageNo=${page.pages})}" target="_blank" rel="external nofollow" >末页</a>
</span>
<span th:if="${page.current>=page.pages}">
下一页
末页
</span>
</p>
</div>
</div>
<div id="footer" th:insert="common/common::footer">
</div>
</body>
</html>
②在NewsController.java新闻控制类在前面的基础上后面加上以下代码语句
NewsController.java
@RequestMapping("/manager")
public String manager(NewsQueryVo newsQueryVo,Model model){
//管理员模块主页只需要查询出新闻列表信息就ok
IPage<News> page = new Page<>(newsQueryVo.getPageNo(),newsQueryVo.getPageSize());
newsService.getNewsListByPage(page,newsQueryVo);
model.addAttribute("page",page);
return "admin";
}
③效果图
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiI2EzX4xSZz91ZsAzNfRHLGZkRGZkRfJ3bs92YsAjMfVmepNHL9EUN2d2XyBXcUllcoBVRzNERq10TPFmNzEWW102TZRTQClGVF5UMR9Fd4VGdsATNfd3bkFGazxSUhxGatJGbwhFT1Y0Mk9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnLkZDZxgTYmdDO1Y2Y2QWN5cTZmRzNzkDO2ITZ3UDMmN2Lc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
2、添加新闻
①编写添加页面代码
add.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>新闻管理系统</title>
<link rel="stylesheet" href="/css/admin.css" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >
<link rel="stylesheet" href="/css/bootstrap.min.css" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >
</head>
<body>
<!-- 头部 -->
<div th:insert="common/common::admin-top"></div>
<div id="main">
<!-- 左侧菜单栏 -->
<div id="left" th:insert="common/common::leftMenu"></div>
<div id="opt_area">
<h1 id="opt_type"> 添加新闻: </h1>
<form role="form" th:action="@{/news/add}" method="post" enctype="multipart/form-data">
<div class="form-group">
<p>
<label>主题</label>
<select class="form-control" name="ntid">
<option value="">请选择主题</option>
<option th:each="topic : ${topicList}" th:value='${topic.tid}'>[[${topic.tname}]]</option>
</select>
</p>
</div>
<p>
<label>标题</label>
<input name="ntitle" type="text" class="opt_input"/>
</p>
<p>
<label>作者</label>
<input name="nauthor" type="text" class="opt_input"/>
</p>
<p>
<label>摘要</label>
<textarea name="nsummary" cols="40" rows="3"></textarea>
</p>
<p>
<label>内容</label>
<textarea name="ncontent" cols="70" rows="10"></textarea>
</p>
<p>
<label>上传图片</label>
<input name="file" type="file" class="btn btn-success"/>
</p>
<input class="btn-sm btn btn-info" type="submit" value="提交" />
<input class="btn-sm btn btn-danger" type="reset" value="重置"/>
</form>
</div>
</div>
<div id="footer" th:insert="common/common::footer"></div>
</body>
</html>
②前往添加新闻页面在NewsController里面编写代码
NewsController.java
@RequestMapping("/add")
public String add(News news,@PathVariable("file")MultipartFile file){
try {
System.out.println(file);
//先判断文件是否为空
if (!file.isEmpty()){
System.out.println("文件不为空,开始提交上传");
//文件不为空,开始提交上传
//设置一个文件的提交路径
String path = "F:/temp/upload/";
//获取旧文件名
String originalFilename = file.getOriginalFilename();
//获取文件拓展名
String extension = FilenameUtils.getExtension(originalFilename);
//定义新文件名
String newFileName = UUID.randomUUID().toString().replace("'", "") + "." + extension;
//为了解决同一个文件夹下面文件过多的问题,我们使用日期作为文件夹管理器
String dataPath = new SimpleDateFormat("yyyyMMdd").format(new Date());
//组装最后的文件名
String finalName = dataPath + "/" +newFileName;
//创建文件对象
File destFile = new File(path, finalName);
//判断文件夹是否存在 不存在则创建对应的文件夹
if (!destFile.getParentFile().exists()){
destFile.getParentFile().mkdir();
}
//将文件保存到磁盘
file.transferTo(destFile);
//设置图片的存放路径
news.setNpicPath(finalName);
}
}catch (Exception e){
e.printStackTrace();
}news.setNcreateDate(new Date());
if ((newsService.save(news))){
return "redirect:/news/manager";
}
return "redirect:/news/add.html";
}
③效果图
3、修改新闻
①编写修改页面代码
edit.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>新闻管理系统</title>
<link rel="stylesheet" href="/css/admin.css" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >
<link rel="stylesheet" href="/css/bootstrap.min.css" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >
<script src="/js/jquery.js"></script>
</head>
<body>
<!-- 头部 -->
<div th:insert="common/common::admin-top"></div>
<div id="main">
<!-- 左侧菜单栏 -->
<div id="left" th:insert="common/common::leftMenu"></div>
<div id="opt_area">
<h1 id="opt_type"> 修改新闻: </h1>
<form th:action="@{/news/edit}" method="post" enctype="multipart/form-data">
<p>
<input type="hidden" name="nid" th:value="${news.nid}">
<label>主题</label>
<select name="ntid" id="ntid">
<option value="">请选择主题</option>
<option th:each="topic : ${topicList}" th:value='${topic.tid}'>[[${topic.tname}]]</option>
</select>
</p>
<p>
<label>标题</label>
<input name="ntitle" type="text" class="opt_input" th:value="${news.ntitle}"/>
</p>
<p>
<label>作者</label>
<input name="nauthor" type="text" class="opt_input" th:value="${news.nauthor}"/>
</p>
<p>
<label>摘要</label>
<textarea name="nsummary" cols="40" rows="3">[[${news.nsummary}]]</textarea>
</p>
<p>
<label>内容</label>
<textarea name="ncontent" cols="70" rows="10">[[${news.ncontent}]]</textarea>
</p>
<p>
<label>上传图片</label>
<input name="file" type="file" class="btn btn-success" th:value="${news.npicPath}"/>
</p>
<input class="btn btn-info btn-sm" type="submit" value="提交"/>
<input class="btn btn-info btn-sm" type="reset" value="重置"/>
</form>
</div>
</div>
<div id="footer" th:insert="common/common::footer"></div>
<script>
$("#ntid").val([[${news.ntid}]]);
</script>
</body>
</html>
②前往添加新闻页面在NewsController里面编写代码
NewsController.java
//修改新闻信息 获取新闻 然后根据新闻编号进行修改
@RequestMapping("/edit/{nid}")
public String editPage(@PathVariable("nid") Integer nid, Model model){
List<Topic> topicList=topicService.list();
model.addAttribute("topicList", topicList);
News news=newsService.getById(nid);
model.addAttribute("news",news);
return "news/edit";
}
@RequestMapping("/edit")
public String edit(News news, MultipartFile file){
uploadPic(news,file);
news.setNmodifyDate(new Date());
if(newsService.updateById(news)){
return "redirect:/news/manager";
}
return "redirect:/news/edit"+news.getNid();
}
private void uploadPic(News news, MultipartFile file){
try {
//判断是否有文件
if(!file.isEmpty()){
String path = "D:/xinwenguanli/";
//获取旧名称
String originalFilename = file.getOriginalFilename();
//获取扩展名
String extension = FilenameUtils.getExtension(originalFilename);
//重命名文件名
String newFileName = UUID.randomUUID().toString().replace("-","")+"."+ extension;
//为了解决同一个文件夹下文件过多的问题,使用日期作为文件夹管理
String datePath = new SimpleDateFormat("yyyyMMdd").format(new Date());
//组装最终的文件名
String finalName = datePath + "/" + newFileName;
//创建文件对象
File destFile = new File(path,finalName);
//判断文件夹是否存在,文件夹不存在则创建该文件夹
if(!destFile.getParentFile().exists()){
destFile.getParentFile().mkdirs();
}
//将文件保存到磁盘
file.transferTo(destFile);
//图片路径
news.setNpicPath(finalName);
}
} catch (IOException e) {
e.printStackTrace();
}
}
③效果图
4、修改新闻
①前往添加新闻页面在NewsController里面编写代码
NewsController.java
@RequestMapping("/deleteById/{nid}")
public void deleteById(@PathVariable Integer nid, HttpServletResponse response){
try {
response.setCharacterEncoding("GBK");
if(newsService.deleteById(nid)){
response.getWriter().print("<script>" +
"alert('删除成功');" +
"location.href='/news/manager';" +
"</script>");
}
response.getWriter().print("<script>" +
"alert('删除失败');" +
"location.href='/news/manager';" +
"</script>");
} catch (IOException e) {
e.printStackTrace();
}
}
②service代码写在NewsServiceImpl中
NewsServiceImpl.java
@Override
public boolean deleteById(Integer nid) {
//根据新闻编号 来删除新闻信息 但是首先得确保 新闻的评论也被干掉了
QueryWrapper<Comments> wrapper = new QueryWrapper<>();
wrapper.eq("cnid",nid);
Integer count = commentsMapper.selectCount(wrapper);
//如果有评论就删除评论
if (count>0){
int i = commentsMapper.delete(wrapper);
}
//根据新闻编号删除 新闻信息
int delete = baseMapper.deleteById(nid);
if (delete>0){
return true;
}
return false;
}