首页>代码>ssm个人博客,包括登录注册,前端后台功能全面完整>/lingnanblog-/src/main/java/neusoft/controller/admin/BlogAdminController.java
package neusoft.controller.admin;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import neusoft.pojo.Blog;
import neusoft.pojo.PageBean;
import neusoft.service.BlogService;
import neusoft.util.BlogIndex;
import neusoft.util.ResponseUtil;
import neusoft.util.StringUtil;
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.RequestParam;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
/**
* 管理员博客Controller层
* @author Administrator
*
*/
@Controller
@RequestMapping("/admin/blog")
public class BlogAdminController {
@Autowired
private BlogService blogService;
// 博客索引
private BlogIndex blogIndex=new BlogIndex();
/**
* 添加或者修改博客信息
* @param blog
* @param response
* @return
* @throws Exception
*/
@RequestMapping("/save")
public String save(Blog blog, HttpServletResponse response ,HttpSession session )throws Exception{
int resultTotal=0; // 操作的记录条数
if(blog.getId()==null){
resultTotal=blogService.add(blog);
blogIndex.addIndex(blog); // 添加博客索引
}else{
resultTotal=blogService.update(blog);
blogIndex.updateIndex(blog); // 更新博客索引
System.out.println(blog);
}
JSONObject result=new JSONObject();
if(resultTotal>0){
result.put("success", true);
// session.setAttribute("blogMsg",blog);
}else{
result.put("success", false);
}
ResponseUtil.write(response, result);
return null;
}
/**
* 分页查询博客信息
* @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,Blog s_blog,HttpServletResponse response)throws Exception{
System.out.println("jhdish");
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);
System.out.println("eeeeeeeeeeeee");
return null;
}
/**
* 删除博客信息
* @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++){
blogService.delete(Integer.parseInt(idsStr[i]));
blogIndex.deleteIndex(idsStr[i]); // 删除对应博客的索引
}
JSONObject result=new JSONObject();
result.put("success", true);
ResponseUtil.write(response, result);
return null;
}
/**
* 通过ID查找实体
* @param id
* @param response
* @return
* @throws Exception
*/
@RequestMapping("/getById")
public String getById(@RequestParam(value="id")String id,HttpServletResponse response)throws Exception{
Blog blog=blogService.getById(Integer.parseInt(id));
JSONObject jsonObject=JSONObject.fromObject(blog);
ResponseUtil.write(response, jsonObject);
return null;
}
}

最近下载
最近浏览