package com.togest.emis.modules.g6c.web;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.togest.emis.framework.orm.Page;
import com.togest.emis.framework.orm.PropertyFilter;
import com.togest.emis.framework.utils.ConvertUtils;
import com.togest.emis.framework.utils.JSONParserUtils;
import com.togest.emis.framework.utils.RequestParamUtils;
import com.togest.emis.framework.utils.StringHelper;
import com.togest.emis.modules.g6c.entity.Download;
import com.togest.emis.modules.g6c.service.Downloadservice;
import com.togest.emis.modules.system.cache.SystemCache;
import com.togest.emis.modules.system.config.SystemConstants;
/**
- @author mmk
-
*/
@Controller
@RequestMapping(value = "/6c/system/download")
public class DownloadController {
@Autowired
private Downloadservice service;
@RequestMapping(method = RequestMethod.GET)
public String list(Model model, HttpServletRequest request, String id) {
model.addAttribute("psn_id", id);
model.addAttribute(SystemConstants.NAV_PATH,
SystemCache.getPath(id, RequestParamUtils.getUserID(request)));
return "/6c/download";
}@RequestMapping(value = "getPage", method = RequestMethod.POST)
public void getPage(HttpServletRequest request,
HttpServletResponse response, String name, String address ,Model model) {
Map map = new HashMap();
try {
List filters = PropertyFilter
.buildFromHttpRequest(request);
Page page = RequestParamUtils
.getPage(request, new Page());
if (!page.isOrderBySetted()) {
page.setOrderBy(SystemConstants.OrderByField);
page.setOrder(Page.ASC);
page = service.findPage(page, filters);
}
map.put(SystemConstants.EASYTABLE_PAGE_TOTAL_INDEX,
page.getTotalCount());
map.put(SystemConstants.EASYTABLE_PAGE_RESULT_INDEX,
page.getResult());
} catch (Exception e) {
e.printStackTrace();
}
try {
JSONParserUtils.write(response, JSONObject.fromObject(map)
.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}@RequestMapping(value = "save", method = RequestMethod.POST)
public void save(@RequestParam(value = "file", required = false) MultipartFile file,
HttpServletRequest request, Model model,HttpServletResponse response, Download d,Map map) {
String path = request.getSession().getServletContext()
.getRealPath("download");
String fileName = request.getParameter("name");
String fileContent = file.getOriginalFilename();
String fileType = fileContent.substring(fileContent.lastIndexOf("."));
String fileAll = fileName+fileType;
String address = path+"\"+fileAll;
String file_path = address.substring(address.lastIndexOf("download"));
String fileUrl = request.getContextPath() + "/download/"
+ fileAll;
// //String fileName = new Date().getTime()+".jpg";
File targetFile = new File(path, fileAll);
if (!targetFile.exists()) {
targetFile.mkdirs();
}// 保存 try { file.transferTo(targetFile); } catch (Exception e) { e.printStackTrace(); } map.put("fileAll", fileAll); model.addAttribute("address", address); model.addAttribute("path", path); model.addAttribute("fileAll", fileAll); model.addAttribute("fileUrl",fileUrl); model.addAttribute("fileName", fileName); System.out.println("+++++++++++++++"+ fileAll); if (StringHelper.isBlank(d.getId())) { d.setId(null); } //d.setFile(fileAll); d.setAddress(file_path); service.save(d); map = new HashMap<String, Object>(); map.put("msg", "新增成功"); try { JSONParserUtils.write(response, JSONObject.fromObject(map) .toString()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
}
@RequestMapping(value = "edit", method = RequestMethod.GET)
public void edit(HttpServletRequest request, HttpServletResponse response) {
String id = RequestParamUtils.getStringParamter(request, "id");if (id != null) { Download d = service.get(id); try { Map<String, Object> map = ConvertUtils.objectToMap(d, new String[] { "id", "sort", "name", "address" }); JSONObject json = JSONObject.fromObject(map); JSONParserUtils.write(response, json.toString()); } catch (Exception e) { e.printStackTrace(); } }
}
@RequestMapping(value = "update", method = RequestMethod.POST)
public void update(HttpServletResponse response, Download d) {
service.save(d);
Map map = new HashMap();
map.put("msg", "修改成功");
try {
JSONParserUtils.write(response, JSONObject.fromObject(map)
.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}@RequestMapping(value = "delete", method = RequestMethod.POST)
public void delete(HttpServletResponse response, String id) {
Map map = new HashMap();
String[] ids = id.split(",");
service.delete(id);
map.put("msg", "删除成功");try { JSONParserUtils.write(response, JSONObject.fromObject(map) .toString()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
}
@RequestMapping(value = "find", method = RequestMethod.POST)
public String find(Model model, HttpServletRequest request) {
List dlist = service.findAll();
// for(Download d :dlist){
// String dname =d.getName();
// String daddress = d.getAddress();
// request.setAttribute("dname", dname);
// request.setAttribute("daddress", daddress);
// }
model.addAttribute("dlist", dlist);
// ActionContext.getContext().getSession().put("dlist", dlist);
// ActionContext.getContext().getValueStack().set("dlist", dlist);
// request.setAttribute("dlist", dlist);
return "/system/passport/login";
}
}
这是控制器的代码
package com.togest.emis.modules.g6c.service;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.togest.emis.framework.orm.Page;
import com.togest.emis.framework.orm.PropertyFilter;
import com.togest.emis.modules.g6c.dao.DownloadDao;
import com.togest.emis.modules.g6c.entity.Download;
import com.togest.emis.modules.system.entity.P_Dictionary;
@Service
public class Downloadservice {
@Autowired
private DownloadDao dao;
/**
* 根据主键ID删除实体
*
* @param id
*/
public void delete(String id) {
dao.delete(id);
}
/**
* 保存实体
*
* @param entity
*/
public void save(Download entity) {
dao.save(entity);
}
/**
* 根据主键ID获取实体
*
* @param id
* @return
*/
public Download get(String id) {
return dao.get(id);
}
/**
* 查询所有
*/
public List<Download> findAll(){
String hql ="FROM Download";
List<Download> list = dao.find(hql);
//for(Download d : list){
//System.out.println("+++++++++++++"+d.getName());
//}
return list;
}
public Page<Download> findPage(final Page<Download> page,
final List<PropertyFilter> filters) {
return dao.findPage(page, filters);
}
}
业务层代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>