提交 ba634133 编写于 作者: shuzheng5201314's avatar shuzheng5201314

完成基本controller

上级 b3b60571
package com.zheng.cms.controller;
import com.zheng.cms.dao.model.CmsArticle;
import com.zheng.cms.dao.model.CmsArticleExample;
import com.zheng.cms.service.CmsArticleService;
import com.zheng.common.util.Paginator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
/**
* 文章控制器
* Created by shuzheng on 2016/11/14.
*/
@Controller
@RequestMapping("/article")
public class CmsArticleController extends BaseController {
private final static Logger _log = LoggerFactory.getLogger(CmsArticleController.class);
@Autowired
private CmsArticleService cmsArticleService;
/**
* 首页
* @return
*/
@RequestMapping("")
public String index() {
return "redirect:/article/list";
}
/**
* 列表
* @param page
* @param rows
* @param request
* @return
*/
@RequestMapping("/list")
public String list(
@RequestParam(required = false, defaultValue = "1", value = "page") int page,
@RequestParam(required = false, defaultValue = "20", value = "rows") int rows,
HttpServletRequest request, Model model) {
// 数据列表
CmsArticleExample cmsArticleExample = new CmsArticleExample();
cmsArticleExample.setOffset((page - 1) * rows);
cmsArticleExample.setLimit(rows);
cmsArticleExample.setOrderByClause("articleId desc");
List<CmsArticle> articles = cmsArticleService.getMapper().selectByExample(cmsArticleExample);
// 分页对象
long total = cmsArticleService.getMapper().countByExample(cmsArticleExample);
Paginator paginator = new Paginator(total, page, rows, request);
model.addAttribute("articles", articles);
model.addAttribute("paginator", paginator);
return "/article/list";
}
/**
* 新增get
* @return
*/
@RequestMapping(value = "/add", method = RequestMethod.GET)
public String add() {
return "/article/add";
}
/**
* 新增post
* @param cmsArticle
* @param binding
* @return
*/
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String add(@Valid CmsArticle cmsArticle, BindingResult binding) {
if (binding.hasErrors()) {
for (ObjectError error : binding.getAllErrors()) {
_log.error(error.getDefaultMessage());
}
return "/article/add";
}
cmsArticle.setCtime(System.currentTimeMillis());
cmsArticleService.getMapper().insertSelective(cmsArticle);
_log.info("新增记录id为:{}", cmsArticle.getArticleId());
return "redirect:/article/list";
}
/**
* 删除
* @param id
* @return
*/
@RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
public String delete(@PathVariable("id") int id) {
cmsArticleService.getMapper().deleteByPrimaryKey(id);
return "redirect:/article/list";
}
/**
* 修改get
* @param id
* @param model
* @return
*/
@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
public String update(@PathVariable("id") int id, Model model) {
model.addAttribute("article", cmsArticleService.getMapper().selectByPrimaryKey(id));
return "/article/update";
}
/**
* 修改post
* @param id
* @param cmsArticle
* @param binding
* @param model
* @return
*/
@RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
public String update(@PathVariable("id") int id, @Valid CmsArticle cmsArticle, BindingResult binding, Model model) {
if (binding.hasErrors()) {
model.addAttribute("errors", binding.getAllErrors());
return "/article/update/" + id;
}
cmsArticleService.getMapper().updateByPrimaryKeySelective(cmsArticle);
return "redirect:/article/list";
}
}
\ No newline at end of file
package com.zheng.cms.controller;
import com.zheng.cms.dao.model.CmsCategory;
import com.zheng.cms.dao.model.CmsCategoryExample;
import com.zheng.cms.service.CmsCategoryService;
import com.zheng.common.util.Paginator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
/**
* 类目控制器
* Created by shuzheng on 2016/11/14.
*/
@Controller
@RequestMapping("/category")
public class CmsCategoryController extends BaseController {
private final static Logger _log = LoggerFactory.getLogger(CmsCategoryController.class);
@Autowired
private CmsCategoryService cmsCategoryService;
/**
* 首页
* @return
*/
@RequestMapping("")
public String index() {
return "redirect:/category/list";
}
/**
* 列表
* @param page
* @param rows
* @param request
* @return
*/
@RequestMapping("/list")
public String list(
@RequestParam(required = false, defaultValue = "1", value = "page") int page,
@RequestParam(required = false, defaultValue = "20", value = "rows") int rows,
HttpServletRequest request, Model model) {
// 数据列表
CmsCategoryExample cmsCategoryExample = new CmsCategoryExample();
cmsCategoryExample.setOffset((page - 1) * rows);
cmsCategoryExample.setLimit(rows);
cmsCategoryExample.setOrderByClause("categoryId desc");
List<CmsCategory> categorys = cmsCategoryService.getMapper().selectByExample(cmsCategoryExample);
// 分页对象
long total = cmsCategoryService.getMapper().countByExample(cmsCategoryExample);
Paginator paginator = new Paginator(total, page, rows, request);
model.addAttribute("categorys", categorys);
model.addAttribute("paginator", paginator);
return "/category/list";
}
/**
* 新增get
* @return
*/
@RequestMapping(value = "/add", method = RequestMethod.GET)
public String add() {
return "/category/add";
}
/**
* 新增post
* @param cmsCategory
* @param binding
* @return
*/
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String add(@Valid CmsCategory cmsCategory, BindingResult binding) {
if (binding.hasErrors()) {
for (ObjectError error : binding.getAllErrors()) {
_log.error(error.getDefaultMessage());
}
return "/category/add";
}
cmsCategory.setCtime(System.currentTimeMillis());
cmsCategoryService.getMapper().insertSelective(cmsCategory);
_log.info("新增记录id为:{}", cmsCategory.getCategoryId());
return "redirect:/category/list";
}
/**
* 删除
* @param id
* @return
*/
@RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
public String delete(@PathVariable("id") int id) {
cmsCategoryService.getMapper().deleteByPrimaryKey(id);
return "redirect:/category/list";
}
/**
* 修改get
* @param id
* @param model
* @return
*/
@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
public String update(@PathVariable("id") int id, Model model) {
model.addAttribute("category", cmsCategoryService.getMapper().selectByPrimaryKey(id));
return "/category/update";
}
/**
* 修改post
* @param id
* @param cmsCategory
* @param binding
* @param model
* @return
*/
@RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
public String update(@PathVariable("id") int id, @Valid CmsCategory cmsCategory, BindingResult binding, Model model) {
if (binding.hasErrors()) {
model.addAttribute("errors", binding.getAllErrors());
return "/category/update/" + id;
}
cmsCategoryService.getMapper().updateByPrimaryKeySelective(cmsCategory);
return "redirect:/category/list";
}
}
\ No newline at end of file
package com.zheng.cms.controller;
import com.zheng.cms.dao.model.CmsComment;
import com.zheng.cms.dao.model.CmsCommentExample;
import com.zheng.cms.dao.model.CmsTag;
import com.zheng.cms.dao.model.CmsTagExample;
import com.zheng.cms.service.CmsCommentService;
import com.zheng.common.util.Paginator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
/**
* 评论控制器
* Created by shuzheng on 2016/11/14.
*/
@Controller
@RequestMapping("/comment")
public class CmsCommentController extends BaseController {
private final static Logger _log = LoggerFactory.getLogger(CmsCommentController.class);
@Autowired
private CmsCommentService cmsCommentService;
/**
* 首页
* @return
*/
@RequestMapping("")
public String index() {
return "redirect:/comment/list";
}
/**
* 列表
* @param page
* @param rows
* @param request
* @return
*/
@RequestMapping("/list")
public String list(
@RequestParam(required = false, defaultValue = "1", value = "page") int page,
@RequestParam(required = false, defaultValue = "20", value = "rows") int rows,
HttpServletRequest request, Model model) {
// 数据列表
CmsCommentExample cmsCommentExample = new CmsCommentExample();
cmsCommentExample.setOffset((page - 1) * rows);
cmsCommentExample.setLimit(rows);
cmsCommentExample.setOrderByClause("commentId desc");
List<CmsComment> tags = cmsCommentService.getMapper().selectByExample(cmsCommentExample);
// 分页对象
long total = cmsCommentService.getMapper().countByExample(cmsCommentExample);
Paginator paginator = new Paginator(total, page, rows, request);
model.addAttribute("tags", tags);
model.addAttribute("paginator", paginator);
return "/comment/list";
}
/**
* 新增get
* @return
*/
@RequestMapping(value = "/add", method = RequestMethod.GET)
public String add() {
return "/comment/add";
}
/**
* 新增post
* @param cmsComment
* @param binding
* @return
*/
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String add(@Valid CmsComment cmsComment, BindingResult binding) {
if (binding.hasErrors()) {
for (ObjectError error : binding.getAllErrors()) {
_log.error(error.getDefaultMessage());
}
return "/comment/add";
}
cmsComment.setCtime(System.currentTimeMillis());
cmsCommentService.getMapper().insertSelective(cmsComment);
_log.info("新增记录id为:{}", cmsComment.getArticleId());
return "redirect:/comment/list";
}
/**
* 删除
* @param id
* @return
*/
@RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
public String delete(@PathVariable("id") int id) {
cmsCommentService.getMapper().deleteByPrimaryKey(id);
return "redirect:/comment/list";
}
/**
* 修改get
* @param id
* @param model
* @return
*/
@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
public String update(@PathVariable("id") int id, Model model) {
model.addAttribute("comment", cmsCommentService.getMapper().selectByPrimaryKey(id));
return "/comment/update";
}
/**
* 修改post
* @param id
* @param cmsComment
* @param binding
* @param model
* @return
*/
@RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
public String update(@PathVariable("id") int id, @Valid CmsComment cmsComment, BindingResult binding, Model model) {
if (binding.hasErrors()) {
model.addAttribute("errors", binding.getAllErrors());
return "/comment/update/" + id;
}
cmsCommentService.getMapper().updateByPrimaryKeySelective(cmsComment);
return "redirect:/comment/list";
}
}
\ No newline at end of file
package com.zheng.cms.controller;
import com.zheng.cms.dao.model.CmsTag;
import com.zheng.cms.dao.model.CmsTagExample;
import com.zheng.cms.service.CmsTagService;
import com.zheng.common.util.Paginator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
/**
* 标签控制器
* Created by shuzheng on 2016/11/14.
*/
@Controller
@RequestMapping("/tag")
public class CmsTagController extends BaseController {
private final static Logger _log = LoggerFactory.getLogger(CmsTagController.class);
@Autowired
private CmsTagService cmsTagService;
/**
* 首页
* @return
*/
@RequestMapping("")
public String index() {
return "redirect:/tag/list";
}
/**
* 列表
* @param page
* @param rows
* @param request
* @return
*/
@RequestMapping("/list")
public String list(
@RequestParam(required = false, defaultValue = "1", value = "page") int page,
@RequestParam(required = false, defaultValue = "20", value = "rows") int rows,
HttpServletRequest request, Model model) {
// 数据列表
CmsTagExample cmsTagExample = new CmsTagExample();
cmsTagExample.setOffset((page - 1) * rows);
cmsTagExample.setLimit(rows);
cmsTagExample.setOrderByClause("tagId desc");
List<CmsTag> tags = cmsTagService.getMapper().selectByExample(cmsTagExample);
// 分页对象
long total = cmsTagService.getMapper().countByExample(cmsTagExample);
Paginator paginator = new Paginator(total, page, rows, request);
model.addAttribute("tags", tags);
model.addAttribute("paginator", paginator);
return "/tag/list";
}
/**
* 新增get
* @return
*/
@RequestMapping(value = "/add", method = RequestMethod.GET)
public String add() {
return "/tag/add";
}
/**
* 新增post
* @param cmsTag
* @param binding
* @return
*/
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String add(@Valid CmsTag cmsTag, BindingResult binding) {
if (binding.hasErrors()) {
for (ObjectError error : binding.getAllErrors()) {
_log.error(error.getDefaultMessage());
}
return "/tag/add";
}
cmsTag.setCtime(System.currentTimeMillis());
cmsTagService.getMapper().insertSelective(cmsTag);
_log.info("新增记录id为:{}", cmsTag.getTagId());
return "redirect:/tag/list";
}
/**
* 删除
* @param id
* @return
*/
@RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
public String delete(@PathVariable("id") int id) {
cmsTagService.getMapper().deleteByPrimaryKey(id);
return "redirect:/tag/list";
}
/**
* 修改get
* @param id
* @param model
* @return
*/
@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
public String update(@PathVariable("id") int id, Model model) {
model.addAttribute("tag", cmsTagService.getMapper().selectByPrimaryKey(id));
return "/tag/update";
}
/**
* 修改post
* @param id
* @param cmsTag
* @param binding
* @param model
* @return
*/
@RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
public String update(@PathVariable("id") int id, @Valid CmsTag cmsTag, BindingResult binding, Model model) {
if (binding.hasErrors()) {
model.addAttribute("errors", binding.getAllErrors());
return "/tag/update/" + id;
}
cmsTagService.getMapper().updateByPrimaryKeySelective(cmsTag);
return "redirect:/tag/list";
}
}
\ No newline at end of file
package com.zheng.common.util;
import javax.servlet.http.HttpServletRequest;
/**
* 分页实体类
* @author shuzheng
......@@ -17,6 +19,14 @@ public class Paginator {
private String url = ""; // 项目路径
private String query = ""; // 当前页所有参数
public Paginator(long total, int page, int rows, HttpServletRequest request) {
setTotal(total);
setPage(page);
setRows(rows);
setUrl(request.getRequestURI());
setQuery(request.getQueryString());
}
public long getTotal() {
return total;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册