package com.gxw.controller; import java.util.Date; import java.util.HashMap; import java.util.Map; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import com.gxw.page.Page; import com.gxw.pojo.News; import com.gxw.service.NewsService; /** *新闻控制器 *完成增删改查,以及分页 *@author GXWadmin * */ @RequestMapping("/news") @Controller public class NewsController { @Autowired private NewsService newsService; /** * 新闻管理列表页 * @param model * @return */ @RequestMapping(value="/list",method=RequestMethod.GET) public ModelAndView list(ModelAndView model){ model.setViewName("news/news_list"); return model; } /** * 获取新闻信息列表 * @param nTitle * @param page * @return */ @RequestMapping(value="/get_list",method=RequestMethod.POST) @ResponseBody public Map<String, Object> getList( @RequestParam(value="nTitle",required=false,defaultValue="") String nTitle, Page page ){ Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> queryMap = new HashMap<String, Object>(); queryMap.put("nTitle", "%"+nTitle+"%"); queryMap.put("offset", page.getOffset()); queryMap.put("pageSize", page.getRows()); map.put("rows", newsService.findList(queryMap)); map.put("total", newsService.getTotal(queryMap)); return map; } /** * 删除新闻操作 * @param news * @return */ @RequestMapping(value="/delete",method=RequestMethod.POST) @ResponseBody public Map<String, String> delete( @RequestParam(value="nids[]",required=true) Long[] nids ){ Map<String, String> ret = new HashMap<String, String>(); if(nids == null){ ret.put("type", "error"); ret.put("msg", "请选择要删除的数据!"); return ret; } String idsString = ""; for(Long nid:nids){ idsString += nid + ","; } idsString = idsString.substring(0,idsString.length()-1); if(newsService.delete(idsString) <= 0){ ret.put("type", "error"); ret.put("msg", "删除失败!"); return ret; } ret.put("type", "success"); ret.put("msg", "修改成功!"); return ret; } /** * 编辑新闻操作 * @param news * @return */ @RequestMapping(value="/edit",method=RequestMethod.POST) @ResponseBody public Map<String, String> edit(News news){ Map<String, String> map = new HashMap<String, String>(); if(news == null){ map.put("type", "error"); map.put("msg", "数据绑定出错,请联系后台管理员!"); return map; } if(StringUtils.isEmpty(news.getnTitle())){ map.put("type", "error"); map.put("msg", "标题不能为空!"); return map; } if(StringUtils.isEmpty(news.getnContent())){ map.put("type", "error"); map.put("msg", "描述不能为空!"); return map; } News existNews = newsService.findByTitle(news.getnTitle()); if(existNews != null){ if(news.getNid() != existNews.getNid()){ map.put("type", "error"); map.put("msg", "该标题已经存在!"); return map; } } if(newsService.edit(news) <= 0){ map.put("type", "error"); map.put("msg", "修改失败!"); return map; } map.put("type", "success"); map.put("msg", "修改成功!"); return map; } /** * 添加新闻操作 * @param news * @return */ @RequestMapping(value="/add",method=RequestMethod.POST) @ResponseBody public Map<String, String> add(News news){ Map<String, String> ret = new HashMap<String, String>(); if(news == null){ ret.put("type", "error"); ret.put("msg", "数据绑定出错,请联系开发作者!"); return ret; } if(StringUtils.isEmpty(news.getnTitle())){ ret.put("type", "error"); ret.put("msg", "标题不能为空!"); return ret; } if(StringUtils.isEmpty(news.getnContent())){ ret.put("type", "error"); ret.put("msg", "内容不能为空!"); return ret; } News existNews = newsService.findByTitle(news.getnTitle()); news.setnDate(new Date()); if(existNews != null){ ret.put("type", "error"); ret.put("msg", "该标题已经存在!"); return ret; } if(newsService.add(news) <= 0){ ret.put("type", "error"); ret.put("msg", "添加失败!"); return ret; } ret.put("type", "success"); ret.put("msg", "添加成功!"); return ret; } }
最近下载更多
tangguo666666 LV4
2023年6月5日
zxc131313 LV12
2022年10月23日
AlanLi LV18
2022年6月17日
109683670 LV9
2022年4月18日
java代写 LV7
2022年3月15日
wanglinddad LV54
2022年2月15日
lilong007 LV20
2021年12月20日
893213895 LV18
2021年12月16日
deck222 LV9
2021年12月12日
KSY247 LV5
2021年11月10日
最近浏览更多
爱丽淇 LV5
3月19日
a318888331 LV13
3月10日
FF加菲猫 LV4
2月15日
iiiiiiixiiiiii LV1
2023年12月28日
ziv5466123 LV7
2023年12月15日
cyz521213 LV1
2023年12月9日
mukoooo LV2
2023年12月5日
内心向阳 LV4
2023年11月8日
lyq6666666 LV5
2023年10月25日
沉默66666
2023年10月5日
暂无贡献等级