ssm(springmvc4+spring4+mybatis3)整合實戰-個人博客系統-後端功能開發

ssm(springmvc4+spring4+mybatis3)整合實戰-個人博客系統-後端功能開發

    下面就介紹介紹個人博客系統後端業務功能的管理。主要包括:發表博客,管理評論,管理博客,友情鏈接,博客類型管理等等。具體內容過多,我就不多廢話,直接上代碼。其中的代碼我已經做了註釋,直接閱讀即可。如果有相關問題,可以加後面的羣討論。

    首先是後端首頁的展示:

    下面是博客管理controller--BlogAdminController.java,其中功能包括髮表博客,列表搜索,獲取博客詳情,刪除博客,修改博客等等:

 

package com.steadyjack.controller.admin;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.steadyjack.entity.Blog;
import com.steadyjack.entity.PageBean;
import com.steadyjack.lucene.BlogIndex;
import com.steadyjack.service.BlogService;
import com.steadyjack.util.ResponseUtil;
import com.steadyjack.util.StringUtil;
import com.steadyjack.util.WebFileOperationUtil;

/**
 * title:BlogAdminController.java
 * description:管理員博客Controller層
 * time:2017年1月23日 下午10:15:18
 * author:debug-steadyjack
 */
@Controller
@RequestMapping("/admin/blog")
public class BlogAdminController {

	@Resource
	private BlogService blogService;
	
	// 博客索引
	private BlogIndex blogIndex=new BlogIndex();
	
	/**
	 * title:BlogAdminController.java
	 * description:添加或者修改博客信息
	 * time:2017年1月23日 下午10:15:36
	 * author:debug-steadyjack
	 * @param blog
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	public String save(Blog blog,HttpServletResponse response,HttpServletRequest request)throws Exception{
		int resultTotal=0;
		
		String newContent=WebFileOperationUtil.copyImageInUeditor(request, blog.getContent());
		blog.setContent(newContent);
		
		blogIndex.setRequest(request);
		if(blog.getId()==null){
			//添加博客索引
			resultTotal=blogService.add(blog);
			blogIndex.addIndex(blog); 
		}else{
			//更新博客索引
			resultTotal=blogService.update(blog);
			blogIndex.updateIndex(blog); 
		}
		
		JSONObject result=new JSONObject();
		if(resultTotal>0){
			result.put("success", true);
		}else{
			result.put("success", false);
		}
		ResponseUtil.write(response, result);
		
		return null;
	}
	
	/**
	 * title:BlogAdminController.java
	 * description:分頁查詢博客信息
	 * time:2017年1月23日 下午10:17:41
	 * author:debug-steadyjack
	 * @param page
	 * @param rows
	 * @param s_blog
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public String list(@RequestParam(value="page",required=false)String page,
			@RequestParam(value="rows",required=false)String rows,Blog s_blog,
			HttpServletResponse response)throws Exception{
		PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("title", StringUtil.formatLike(s_blog.getTitle()));
		map.put("start", pageBean.getStart());
		map.put("size", pageBean.getPageSize());
		
		List<Blog> blogList=blogService.list(map);
		Long total=blogService.getTotal(map);
		
		JSONObject result=new JSONObject();
		
		//處理專門的日期字段值
		JsonConfig jsonConfig=new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray=JSONArray.fromObject(blogList,jsonConfig);
		
		result.put("rows", jsonArray);
		result.put("total", total);
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 * title:BlogAdminController.java
	 * description:刪除博客信息
	 * time:2017年1月23日 下午10:20:11
	 * author:debug-steadyjack
	 * @param ids
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	public String delete(@RequestParam(value="ids")String ids,HttpServletResponse response,
			HttpServletRequest request)throws Exception{
		String[] idsStr=ids.split(",");
		
		blogIndex.setRequest(request);
		for(int i=0;i<idsStr.length;i++){
			Integer currId=Integer.parseInt(idsStr[i]);
			Blog currBlog=blogService.findById(currId);
			
			//刪除博客內容裏面的圖片
			WebFileOperationUtil.deleteImagesInUeditor(request, currBlog.getContent());
			
			blogService.delete(currId);
			//刪除對應博客的索引
			blogIndex.deleteIndex(idsStr[i]); 
			
		}
		JSONObject result=new JSONObject();
		result.put("success", true);
		ResponseUtil.write(response, result);
		
		return null;
	}
	
	/**
	 * title:BlogAdminController.java
	 * description:通過ID查找實體
	 * time:2017年1月23日 下午10:20:45
	 * author:debug-steadyjack
	 * @param id
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findById")
	public String findById(@RequestParam(value="id")String id,HttpServletResponse response)throws Exception{
		Blog blog=blogService.findById(Integer.parseInt(id));
		JSONObject jsonObject=JSONObject.fromObject(blog);
		ResponseUtil.write(response, jsonObject);
		
		return null;
	}
	
}


    其中,值得說明的是,因爲發表博客涉及到“百度編輯器ueditor”的操作,比如上傳的圖片,需要在保存博客時對圖片進行處理,比如移動copy到指定的文件目錄(因爲一般而言ueditor配置初始文件上傳目錄是一個臨時目錄,不建議 永久保存在哪裏)。對於這個操作,我將在下一篇博客重點講述。

 

    下面是博客類型Controller--BlogTypeAdminController.java:

 

package com.steadyjack.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.steadyjack.entity.BlogType;
import com.steadyjack.entity.PageBean;
import com.steadyjack.service.BlogService;
import com.steadyjack.service.BlogTypeService;
import com.steadyjack.util.ResponseUtil;

/**
 * title:BlogTypeAdminController.java
 * description:管理員博客類別Controller層
 * time:2017年1月23日 下午10:22:24
 * author:debug-steadyjack
 */
@Controller
@RequestMapping("/admin/blogType")
public class BlogTypeAdminController {

	@Resource
	private BlogTypeService blogTypeService;
	
	@Resource
	private BlogService blogService;
	
	/**
	 * title:BlogTypeAdminController.java
	 * description:分頁查詢博客類別信息
	 * time:2017年1月23日 下午10:22:43
	 * author:debug-steadyjack
	 * @param page
	 * @param rows
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public String list(@RequestParam(value="page",required=false)String page,
			@RequestParam(value="rows",required=false)String rows,HttpServletResponse response)throws Exception{
		PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("start", pageBean.getStart());
		map.put("size", pageBean.getPageSize());
		List<BlogType> blogTypeList=blogTypeService.list(map);
		Long total=blogTypeService.getTotal(map);
		
		JSONObject result=new JSONObject();
		JSONArray jsonArray=JSONArray.fromObject(blogTypeList);
		result.put("rows", jsonArray);
		result.put("total", total);
		ResponseUtil.write(response, result);
		
		return null;
	}
	
	/**
	 * title:BlogTypeAdminController.java
	 * description:添加或者修改博客類別信息
	 * time:2017年1月23日 下午10:23:38
	 * author:debug-steadyjack
	 * @param blogType
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	public String save(BlogType blogType,HttpServletResponse response)throws Exception{
		//操作的記錄條數
		int resultTotal=0;
		
		if(blogType.getId()==null){
			resultTotal=blogTypeService.add(blogType);
		}else{
			resultTotal=blogTypeService.update(blogType);
		}
		
		JSONObject result=new JSONObject();
		
		if(resultTotal>0){
			result.put("success", true);
		}else{
			result.put("success", false);
		}
		ResponseUtil.write(response, result);
		
		return null;
	}
	
	/**
	 * title:BlogTypeAdminController.java
	 * description:刪除博客類別信息
	 * time:2017年1月23日 下午10:24:08
	 * author:debug-steadyjack
	 * @param ids
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	public String delete(@RequestParam(value="ids")String ids,HttpServletResponse response)throws Exception{
		String[] idsStr=ids.split(",");
		JSONObject result=new JSONObject();
		
		for(int i=0;i<idsStr.length;i++){
			if(blogService.getBlogByTypeId(Integer.parseInt(idsStr[i]))>0){
				result.put("exist", "博客類別下有博客,不能刪除!");
			}else{
				blogTypeService.delete(Integer.parseInt(idsStr[i]));				
			}
		}
		result.put("success", true);
		ResponseUtil.write(response, result);
		
		return null;
	}
}


    然後是評論管理Controller--CommentAdminController.java:

 

 

package com.steadyjack.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.steadyjack.entity.Comment;
import com.steadyjack.entity.PageBean;
import com.steadyjack.service.CommentService;
import com.steadyjack.util.ResponseUtil;

/**
 * title:CommentAdminController.java
 * description:管理員評論Controller層
 * time:2017年1月23日 下午10:24:52
 * author:debug-steadyjack
 */
@Controller
@RequestMapping("/admin/comment")
public class CommentAdminController {

	@Resource
	private CommentService commentService;
	
	/**
	 * title:CommentAdminController.java
	 * description:分頁查詢評論信息
	 * time:2017年1月23日 下午10:25:04
	 * author:debug-steadyjack
	 * @param page
	 * @param rows
	 * @param state
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public String list(@RequestParam(value="page",required=false)String page,
			@RequestParam(value="rows",required=false)String rows,
			@RequestParam(value="state",required=false)String state,HttpServletResponse response)throws Exception{
		PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("start", pageBean.getStart());
		map.put("size", pageBean.getPageSize());
		//評論狀態
		map.put("state", state); 
		
		List<Comment> commentList=commentService.list(map);
		Long total=commentService.getTotal(map);
		
		JSONObject result=new JSONObject();
		JsonConfig jsonConfig=new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray=JSONArray.fromObject(commentList,jsonConfig);
		result.put("rows", jsonArray);
		result.put("total", total);
		ResponseUtil.write(response, result);
		
		return null;
	}
	
	/**
	 * title:CommentAdminController.java
	 * description:刪除評論信息
	 * time:2017年1月23日 下午10:31:32
	 * author:debug-steadyjack
	 * @param ids
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	public String delete(@RequestParam(value="ids")String ids,HttpServletResponse response)throws Exception{
		String[] idsStr=ids.split(",");
		for(int i=0;i<idsStr.length;i++){
			commentService.delete(Integer.parseInt(idsStr[i]));
		}
		JSONObject result=new JSONObject();
		result.put("success", true);
		ResponseUtil.write(response, result);
		
		return null;
	}
	
	/**
	 * title:CommentAdminController.java
	 * description:評論審覈
	 * time:2017年1月23日 下午10:31:58
	 * author:debug-steadyjack
	 * @param ids
	 * @param state
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/review")
	public String review(@RequestParam(value="ids")String ids,@RequestParam(value="state")Integer state,
			HttpServletResponse response)throws Exception{
		String[] idsStr=ids.split(",");
		for(int i=0;i<idsStr.length;i++){
			Comment comment=new Comment();
			comment.setState(state);
			comment.setId(Integer.parseInt(idsStr[i]));
			commentService.update(comment);
		}
		JSONObject result=new JSONObject();
		result.put("success", true);
		ResponseUtil.write(response, result);
		
		return null;
	}
}


    修改個人信息控制器Controller-BloggerAdminController.java:

 

 

package com.steadyjack.controller.admin;

import java.io.File;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.steadyjack.entity.Blogger;
import com.steadyjack.service.BloggerService;
import com.steadyjack.util.CryptographyUtil;
import com.steadyjack.util.DateUtil;
import com.steadyjack.util.ResponseUtil;
import com.steadyjack.util.WebFileUtil;

/**
 * title:BloggerAdminController.java
 * description:管理員博主Controller層
 * time:2017年1月22日 下午10:27:50
 * author:debug-steadyjack
 */
@Controller
@RequestMapping("/admin/blogger")
public class BloggerAdminController {

	@Resource
	private BloggerService bloggerService;
	
	/**
	 * title:BloggerAdminController.java
	 * description:修改博主信息 - 異步
	 * time:2017年1月22日 下午10:28:11
	 * author:debug-steadyjack
	 * @param imageFile
	 * @param blogger
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	public String save(@RequestParam("imageFile") MultipartFile imageFile,Blogger blogger,HttpServletRequest request,HttpServletResponse response)throws Exception{
		if(!imageFile.isEmpty()){
			//對上傳的文件(個人頭像)進行處理:文件命名,轉爲File
			String filePath=WebFileUtil.getSystemRootPath(request);
			String imageName=DateUtil.getCurrentDateStr()+"."+imageFile.getOriginalFilename().split("\\.")[1];
			imageFile.transferTo(new File(filePath+"static/userImages/"+imageName));
			blogger.setImageName(imageName);
		}
		int resultTotal=bloggerService.update(blogger);
		StringBuffer result=new StringBuffer();
		if(resultTotal>0){
			result.append("<script language='javascript'>alert('修改成功!');</script>");
		}else{
			result.append("<script language='javascript'>alert('修改失敗!');</script>");
		}
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 * title:BloggerAdminController.java
	 * description:查詢博主信息 - 異步
	 * time:2017年1月22日 下午10:41:45
	 * author:debug-steadyjack
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/find")
	public String find(HttpServletResponse response)throws Exception{
		Blogger blogger=bloggerService.find();
		JSONObject jsonObject=JSONObject.fromObject(blogger);
		ResponseUtil.write(response, jsonObject);
		
		return null;
	}
	
	/**
	 * title:BloggerAdminController.java
	 * description:修改博主密碼
	 * time:2017年1月22日 下午11:18:42
	 * author:debug-steadyjack
	 * @param newPassword
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/modifyPassword")
	public String modifyPassword(String newPassword,HttpServletResponse response)throws Exception{
		Blogger blogger=new Blogger();
		blogger.setPassword(CryptographyUtil.md5(newPassword, "debug"));
		int resultTotal=bloggerService.update(blogger);
		JSONObject result=new JSONObject();
		if(resultTotal>0){
			result.put("success", true);
		}else{
			result.put("success", false);
		}
		ResponseUtil.write(response, result);
		
		return null;
	}
	
	/**
	 * title:BloggerAdminController.java
	 * description:註銷-退出登錄
	 * time:2017年1月22日 下午11:18:17
	 * author:debug-steadyjack
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/logout")
	public String  logout()throws Exception{
		SecurityUtils.getSubject().logout();
		return "redirect:/login.jsp";
	}
}


    友情鏈接管理Controller--LinkAdminController.java

 

 

package com.steadyjack.controller.admin;


import java.util.HashMap;
import java.util.List;
import java.util.Map;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


import com.steadyjack.entity.Link;
import com.steadyjack.entity.PageBean;
import com.steadyjack.service.LinkService;
import com.steadyjack.util.ResponseUtil;


/**
 * title:LinkAdminController.java
 * description:友情鏈接Controller層
 * time:2017年1月23日 下午10:32:33
 * author:debug-steadyjack
 */
@Controller
@RequestMapping("/admin/link")
public class LinkAdminController {
	
	@Resource
	private LinkService linkService;
	
	/**
	 * title:LinkAdminController.java
	 * description:分頁查詢友情鏈接信息
	 * time:2017年1月23日 下午10:32:41
	 * author:debug-steadyjack
	 * @param page
	 * @param rows
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public String list(@RequestParam(value="page",required=false)String page,
			@RequestParam(value="rows",required=false)String rows,
			HttpServletResponse response)throws Exception{
		PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("start", pageBean.getStart());
		map.put("size", pageBean.getPageSize());
		
		List<Link> linkList=linkService.list(map);
		Long total=linkService.getTotal(map);
		JSONObject result=new JSONObject();
		JSONArray jsonArray=JSONArray.fromObject(linkList);
		result.put("rows", jsonArray);
		result.put("total", total);
		ResponseUtil.write(response, result);
		
		return null;
	}
	
	/**
	 * title:LinkAdminController.java
	 * description:添加或者修改友情鏈接信息
	 * time:2017年1月23日 下午10:33:17
	 * author:debug-steadyjack
	 * @param link
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	public String save(Link link,HttpServletResponse response)throws Exception{
		//操作的記錄條數
		int resultTotal=0;
		
		if(link.getId()==null){
			resultTotal=linkService.add(link);
		}else{
			resultTotal=linkService.update(link);
		}
		JSONObject result=new JSONObject();
		
		if(resultTotal>0){
			result.put("success", true);
		}else{
			result.put("success", false);
		}
		ResponseUtil.write(response, result);
		
		return null;
	}
	
	/**
	 * title:LinkAdminController.java
	 * description:刪除友情鏈接信息
	 * time:2017年1月23日 下午10:33:47
	 * author:debug-steadyjack
	 * @param ids
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	public String delete(@RequestParam(value="ids")String ids,HttpServletResponse response)throws Exception{
		String[] idsStr=ids.split(",");
		for(int i=0;i<idsStr.length;i++){
			linkService.delete(Integer.parseInt(idsStr[i]));
		}
		JSONObject result=new JSONObject();
		result.put("success", true);
		ResponseUtil.write(response, result);
		
		return null;
	}
}


     最後是介紹一個全局的關於日期處理的處理器DateJsonValueProcessor.java:

 

 

package com.steadyjack.controller.admin;

import java.text.SimpleDateFormat;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

/**
 * title:DateJsonValueProcessor.java
 * description:json-lib 日期處理器(處理含有日期字段的對象;處理含有日期字段的對象list)
 * time:2017年1月23日 下午10:29:09
 * author:debug-steadyjack
 */
public class DateJsonValueProcessor implements JsonValueProcessor{

	private String format;  
	
    public DateJsonValueProcessor(String format){  
        this.format = format;  
    }  
    
	public Object processArrayValue(Object value, JsonConfig jsonConfig) {
		return null;
	}

	public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
		if(value == null)  
        {  
            return "";  
        }  
        if(value instanceof java.sql.Timestamp)  
        {  
            String str = new SimpleDateFormat(format).format((java.sql.Timestamp)value);  
            return str;  
        }  
        if (value instanceof java.util.Date)  
        {  
            String str = new SimpleDateFormat(format).format((java.util.Date) value);  
            return str;  
        }  
          
        return value.toString(); 
	}

}


    相關的後端的頁面已經在前一篇博客上傳了,在本文的最後,將直接分享整個項目的源碼與數據庫。並也會分享到github,我會在第一條評論留言!如果下載不方便,可以加入羣討論:Java開源技術交流:583522159  我叫debug

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章