package com.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.CommentMapper;
import com.mapper.NewsMapper;
import com.mapper.UserMapper;
import com.pojo.Comment;
import com.pojo.News;
import com.pojo.Product;
import com.pojo.User;
import com.service.CommentService;
import com.service.NewsService;
import com.service.UserService;
import com.utils.FileNameUtil;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.*;
@Controller
@RequestMapping("news")
public class NewsController {
@Autowired
private UserService userService;
@Autowired
private UserMapper userMapper;
@Autowired
private NewsMapper newsMapper;
@Autowired
private NewsService newsService;
@Autowired
private CommentService commentService;
@Autowired
private CommentMapper commentMapper;
@RequestMapping("/findByPage")
public String findByPage(Integer pageNum, Integer pageSize, News news, Model model){
//校验参数,因为有可能传过来的参数可能是为null
if (pageNum==null||pageNum<1){
pageNum=1;
}
if (pageSize==null||pageSize<1){
pageSize=5;
}
//保存条件,用于展示在页面上,进行回显
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("title",news.getTitle());
hashMap.put("author",news.getAuthor());
hashMap.put("category",news.getCategory());
//分页查询
PageHelper.startPage(pageNum,pageSize);
List<News> condition = newsService.findByCondition(news);//先进行模糊查询 把查询出来的结果交给PageInfo进行分页处理
PageInfo<News> pageInfo = new PageInfo<>(condition);
model.addAttribute("map",hashMap);//用于回显数据
model.addAttribute("pageInfo",pageInfo);
return "news_index";
}
@RequestMapping("/findNewsByCategory")
public String findNewsByCategory(News news, Model model){
List<News> newsList = newsService.findByConditionUser(news);//先进行模糊查询 把查询的结果返回给user_index
model.addAttribute("newsList",newsList);
return "user_index";
}
/**
* 修改新闻上架状态
* @param news
* @return
*/
@RequestMapping("/changeStateById")
public String changeStateById(News news){
System.out.println("controller修改新闻状态方法执行了");
newsService.changeStateById(news);
return "redirect:/news/findByPage";
}
/**
* 删除选中
* @param nid
* @return
*/
@RequestMapping("/deleteCounts")
public String deleteCounts(String[] nid){
System.out.println(nid);
if (nid==null||nid.length==0) return "redirect:/news/findByPage"; //防止未选中
for (String id : nid) {
newsService.deleteById(Integer.parseInt(id));
}
return "redirect:/news/findByPage";
}
@RequestMapping("/addNews")
public String addNews(String title,String category,MultipartFile photo ,String content, HttpSession session, Model model, HttpServletRequest request) throws IOException {
System.out.println(photo);
String finalPath = uploadTest(photo, session);
News news = new News();
//将最终路径赋值给news
User user = (User) session.getAttribute("user");
news.setTitle(title);
news.setCategory(category);
news.setContent(content);
news.setCoverUrl(finalPath);
news.setAuthor(user.getUsername());
System.out.println(news);
Integer flag = newsService.addNews(news);
if (flag==1){
model.addAttribute("flag","发布成功");
}
else if (flag==2){
model.addAttribute("flag","标题未填写");
}
else if (flag==3){
model.addAttribute("flag","输入正文过短(至少五十字)");
}else {
model.addAttribute("flag","发布失败");
}
return "news_create";
}
public String uploadTest(MultipartFile photo, HttpSession session) throws IOException {
//获取上传的文件的文件名
String filename = photo.getOriginalFilename();
//获取上传文件的后缀名
//substring(beginIndex,endIndex) 截取参数范围的字符 截头不截尾 只有一个参数就从beginIndex开始截取结束
//lastIndexOf(str)找出这个str出现的最后的一次索引
String hzName = filename.substring(filename.lastIndexOf("."));
//获得uuid
String uuid = UUID.randomUUID().toString();
//拼接一个新的文件名
filename=uuid+hzName;
//------------------------以上是防止重名-------------------------------
//获取ServletContext对象
ServletContext servletContext = session.getServletContext();
//获取当前工程下photo目录的真实路径
String imgPath = servletContext.getRealPath("upload");
//创建imgPath所对应的file对象
File file = new File(imgPath);
//判断file所对应的目录是否存在
if (!file.exists()){
file.mkdirs();
}
//拼接最终上传路径
String finalPath=imgPath+File.separator+filename;
//上传文件
photo.transferTo(new File(finalPath));
finalPath="/upload/"+filename;
return finalPath;
}
/**
* 根据id查找新闻
* @param id 新闻id
* @param model 携带新闻信息传输给news_main.jsp
* @param session 获取携带的user信息 判断该跳那个网页
* @return 查出来的新闻对象
*/
@RequestMapping("/findNewsById")
public String findNewsById(Integer id,Model model,HttpSession session){
System.out.println("正在去查询新闻");
News news = newsService.findNewsById(id);
System.out.println(news);
model.addAttribute("news",news);
User user = (User) session.getAttribute("user");
//携带评论信息过去
List<Comment> commentList = commentService.selectRemarkByNid(id);
model.addAttribute("commentList",commentList);
if (user.getRole()==9){
return "news_update_main";
}
return "news_main";
}
@RequestMapping("/updateNewsById")
public String updateNewsById(News news,Model model){
Integer i = newsService.updateNewsById(news);
if (i!=0){
model.addAttribute("res","修改成功!");
}else {
model.addAttribute("res","修改失败!");
}
return "redirect:/news/findNewsById?id="+news.getId();
}
@ResponseBody
@RequestMapping("/showPhoto")
public HashMap showPhoto(HttpServletRequest request, HttpServletResponse response){
HashMap<String, Object> map = new HashMap<>();
String photo = request.getParameter("photo");
System.out.println(photo);
map.put("imgSrc",photo);
return map;
}
//异步ajax的图片回显功能
@ResponseBody
@RequestMapping("/ajaxImg")
public Object ajaximg(MultipartFile pimage,HttpServletRequest request){
System.out.println("图片回显功能调用了");
//提取生成文件名UUID+文件名后缀 保证图片可以重复
String saveFileName= FileNameUtil.getUUIDFileName()+FileN
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
项目标题:基于SSM框架的JSP新闻管理系统 项目简介: 本系统采用JavaScript、CSS、HTML、Java、PHP和Shell等多种编程语言开发,共包含798个文件。其中,JavaScript文件209个,图片资源包括PNG格式177个、GIF格式49个、JPG格式29个,样式文件CSS有92个,配置及数据文件JSON有37个,HTML页面28个,资源映射文件21个,Java后端代码18个,以及JSP页面18个。 核心框架:SSM(Spring、SpringMVC、MyBatis) 主要功能:实现新闻的发布、管理及数据库的交互。 项目背景: 此项目为大学二年级下学期实践课程作品,通过构建一个新闻发布系统,加深了对SSM框架及JSP技术的理解与运用。数据库部分可通过QQ号2579929750获取。 文件类型分布合理,结构清晰,为新闻管理系统的开发与学习提供了良好的案例和模板。
资源推荐
资源详情
资源评论
收起资源包目录
基于SSM框架的JSP新闻管理系统源码 (797个子文件)
._bootstrap 269B
._bundle 269B
._core 269B
._css 269B
._iconfont 269B
._js 269B
._LICENSE 269B
._LICENSE 269B
._react 269B
._svelte 269B
._swiper 269B
._vue 269B
theme.css 501KB
bootstrap.css 193KB
bootstrap.min.css 156KB
bootstrap.css 143KB
bootstrap.min.css 118KB
admin-forms.css 106KB
admin-forms.min.css 85KB
bootstrap-grid.css 66KB
animate.min.css 52KB
bootstrap-grid.min.css 50KB
ueditor.css 43KB
ueditor.min.css 34KB
iconfont.css 30KB
bootstrap-theme.css 26KB
bootstrap-theme.min.css 23KB
iconsweets.css 23KB
video-js.css 21KB
glyphicons-pro.css 19KB
image.css 18KB
icomoon.css 18KB
bootstrap-editable.css 17KB
zocial.css 16KB
swiper-bundle.css 15KB
ui.fancytree.min.css 15KB
video.css 15KB
attachment.css 14KB
swiper-bundle.min.css 13KB
video-js.min.css 11KB
fullcalendar.min.css 10KB
summernote.css 10KB
dropzone.css 10KB
octicons.css 9KB
core.css 8KB
demo.css 8KB
mapplic.css 8KB
ladda.min.css 8KB
dataTables.bootstrap.css 7KB
shCoreDefault.css 7KB
magnific-popup.css 6KB
daterangepicker.css 5KB
bootstrap-datetimepicker.css 5KB
bootstrap-reboot.css 5KB
bootstrap-reboot.min.css 4KB
scrawl.css 4KB
slick.css 4KB
nestable.css 4KB
bootstrap-colorpicker.min.css 3KB
index.css 3KB
editor.bootstrap.css 3KB
dataTables.editor.css 3KB
codemirror.css 3KB
charts.css 3KB
stateface.css 2KB
background.css 2KB
c3.min.css 2KB
googlecode.css 2KB
main.css 2KB
tagmanager.css 2KB
emotion.css 2KB
commentStyle.css 2KB
dialogbase.css 2KB
music.css 2KB
github.css 1KB
typeahead.js-bootstrap.css 1KB
edittable.css 1KB
test_01.css 1KB
bootstrap-tour.css 1KB
template.css 1KB
table.css 927B
main.css 790B
iconfont.css 589B
webuploader.css 552B
getSQLcomment.css 530B
webuploader.css 515B
theme-default.css 469B
base.css 422B
help.css 389B
html.css 272B
._bootstrap-grid.min.css 269B
._bootstrap.css 269B
._bootstrap-grid.css 269B
._bootstrap-reboot.min.css 269B
._bootstrap.min.css 269B
._bootstrap-reboot.css 269B
._swiper-bundle.min.css 269B
._swiper-bundle.css 269B
._iconfont.css 269B
._demo.css 269B
共 797 条
- 1
- 2
- 3
- 4
- 5
- 6
- 8
资源评论
沐知全栈开发
- 粉丝: 4745
- 资源: 3373
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功