Spring MVC控制器

  

@Controller@RequestMapping ("/articlecontent”)
br/> @RequestMapping (“/articlecontent”)
  

 <代码> @ autowired
  私人TestService TestService;
  
  @ autowired
  私人ArticleContentService ArticleContentService;
  
  私人日志记录器=LoggerFactory.getLogger (ArticleContentController.class);
  
  @RequestMapping (value=" https://www.yisu.com/articlecontentadd ",方法=RequestMethod.GET)
  公共字符串articleContentAdd(模型模型){
  模型。addAttribute (“appExtStr testService.getAppExtStr ());
  返回“articlecontent/articlecontentadd”;
  }
  
  @RequestMapping (value=" https://www.yisu.com/articlecontentdetail ",方法=RequestMethod.POST)
  公共空间articleContentDetail(模型模型,@RequestBody <字符串、对象>地图,地图HttpServletRequest请求,HttpServletResponse响应){
  Map =新HashMap <字符串、对象> ();
  Map  articleMap=articleContentService.getArticleContent(地图). get (0);
  result.putAll (articleMap);
  MvcHelper。WriteResponseInfo(响应,因此,request.getCharacterEncoding ());
  }
  
  @RequestMapping (value=" https://www.yisu.com/articlecontentadds ",方法=RequestMethod.POST)
  公共空间articleContentAdds(模型模型,@RequestBody <字符串、对象>地图,地图HttpServletRequest请求,HttpServletResponse响应){
  Map =新HashMap <字符串、对象> ();
  地图。把(“造物主”,“zhoujun”);
  int数=articleContentService.insertArticleContent(地图);
  如果数量> 0){
  结果。把(“rtnCnt”,“1”);
  其他}{
  结果。把(“rtnCnt”、“0”);
  }
  MvcHelper。WriteResponseInfo(响应,因此,request.getCharacterEncoding ());
  }
  
  @RequestMapping (value=" https://www.yisu.com/articlecontentlist ",方法=RequestMethod.POST)
  公共空间articleContentList(模型模型,@RequestBody <字符串、对象>地图,地图HttpServletRequest请求,HttpServletResponse响应){
  Map =新HashMap <字符串、对象> ();
  Map  pMap=MvcHelper.getPagerParams(地图);
  int totalCnt=articleContentService.getArticleContentListCnt (pMap);
  int totalPage=MvcHelper。getTotalPage (pMap totalCnt);
  结果。put (" listMap ", articleContentService.getArticleContentList (pMap));
  结果。把(“totalCnt”, totalCnt);
  结果。把(“totalPage”, totalPage);
  MvcHelper。WriteResponseInfo(响应,因此,request.getCharacterEncoding ());
  } 
  

}
公共类MvcHelper {

  
 <代码>公共静态孔隙WriteResponseInfo (HttpServletResponse响应、对象dataObj字符串characterEncoding) {
  objectmap jacksonObjectMapper=new objectmap ();
  字符串jsonFormatMapResultString=零;
  尝试{
  jsonFormatMapResultString=jacksonObjectMapper.writeValueAsString (dataObj);
  }捕捉(JsonProcessingException e) {//TODO自动生成的catch块
  e.printStackTrace ();
  }//response.setContentType (request.getContentType ());
  response.setCharacterEncoding (characterEncoding);
  尝试{
  response.getWriter () .print (jsonFormatMapResultString);
  }捕捉(IOException e) {//TODO自动生成的catch块
  e.printStackTrace ();
  }
  }
  
  公共静态Map  getPagerParams (Map 地图){
  int pageIndex=Integer.valueOf (String.valueOf(地图。getOrDefault (“PageIndex”,“1”)));
  int页大?Integer.valueOf (String.valueOf(地图。getOrDefault(“页大小”,“10”)));
  int firstRowIndex=(pageIndex - 1) *页大小+ 1;
  int lastRowIndex=pageIndex *页大小;
  地图。把(“firstRowIndex”, firstRowIndex);
  地图。把(“lastRowIndex”, lastRowIndex);
  返回地图;
  }
  
  公共静态int getTotalPage (Map 地图,int totalCnt) {
  int totalPage=0;
  int页大?Integer.valueOf (String.valueOf(地图。getOrDefault(“页大小”,“10”)));//这个。TotalCnt %。页大?=0 ?这一点。TotalCnt/R炒笮?Math.ceil(这一点。TotalCnt/this.PageSize);
  totalPage=totalCnt %页大?=0 ?totalCnt/页大小:(int) Math.ceil((双)totalCnt/页大小);
  返回totalPage;
  } 
  

}

Spring MVC控制器