package com.open1111.controller.admin;
import java.io.File;
import java.util.Date;
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 org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.multipart.MultipartFile;
import com.open1111.entity.Article;
import com.open1111.entity.PageBean;
import com.open1111.service.ArticleService;
import com.open1111.service.impl.InitComponent;
import com.open1111.util.DateUtil;
import com.open1111.util.ResponseUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
/**
* 帖子后台管理Controller层
* @author user
*
*/
@Controller
@RequestMapping("/admin/article")
public class ArticleAdminController {
@Resource
private ArticleService articleService;
@Resource
private InitComponent initComponent;
/**
* 添加或者修改帖子信息
* @param article
* @param response
* @return
* @throws Exception
*/
@RequestMapping("/save")
public String save(@RequestParam("slideImageFile") MultipartFile slideImageFile, Article article,HttpServletResponse response,HttpServletRequest request)throws Exception{
if(!slideImageFile.isEmpty()){
String filePath=request.getServletContext().getRealPath("/");
String imageName=DateUtil.getCurrentDateStr()+"."+slideImageFile.getOriginalFilename().split("\\.")[1];
slideImageFile.transferTo(new File(filePath+"static/userImages/"+imageName));
article.setSlideImage(imageName);
}
int resultTotal=0; // 操作的记录条数
article.setPublishDate(new Date());
if(article.getId()==null){ // 添加
resultTotal=articleService.add(article);
}else{ // 修改
}
StringBuffer result=new StringBuffer();
if(resultTotal>0){
initComponent.refreshSystem(ContextLoader.getCurrentWebApplicationContext().getServletContext());
result.append("<script language=‘javascript‘>alert(‘提交成功‘);</script>");
}else{
result.append("<script language=‘javascript‘>alert(‘提交失败,请联系管理员‘);</script>");
}
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,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<Article> articleList=articleService.list(map);
Long total=articleService.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(articleList, jsonConfig);
result.put("rows", jsonArray);
result.put("total", total);
ResponseUtil.write(response, result);
return null;
}
}
2.日期操作工具
package com.open1111.util;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 日期工具类
* @author Administrator
*
*/
public class DateUtil {
/**
* 日期对象转字符串
* @param date
* @param format
* @return
*/
public static String formatDate(Date date,String format){
String result="";
SimpleDateFormat sdf=new SimpleDateFormat(format);
if(date!=null){
result=sdf.format(date);
}
return result;
}
/**
* 字符串转日期对象
* @param str
* @param format
* @return
* @throws Exception
*/
public static Date formatString(String str,String format) throws Exception{
if(StringUtil.isEmpty(str)){
return null;
}
SimpleDateFormat sdf=new SimpleDateFormat(format);
return sdf.parse(str);
}
public static String getCurrentDateStr()throws Exception{
Date date=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddhhmmss");
return sdf.format(date);
}
}