package framework.base; import java.io.File; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import framework.fileUpload.FileUploadHelper; import framework.fileUpload.FileUploadHelper.UploadFile; import framework.mybatis.pageAction.PageUtil; import framework.startup.FrameUtil; /** * 全部控制器的基类,提供控制器的工具 * * @author liuyajun, 8384503@qq.com * @date 2016年1月13日 * @time 上午8:32:35 */ public abstract class FrameBaseController { /** * 下载文件 * TODO: 需要保证能够使用中文URL * * @param f * @param name 另取名字 * @param delete */ public String downloadFile(File f, String name, boolean delete) { String furi = f.toURI().toString(); String rootUri = new File(FrameUtil.getWebRoot()).toURI().toString(); int pos = furi.indexOf(rootUri); if(pos==0 && !furi.contains("/WEB-INF/")){ furi = furi.substring(rootUri.length()); if(!furi.startsWith("/")){ furi="/"+furi; } this.getRequest().setAttribute("downloadFileURL", furi); } this.getRequest().setAttribute("downloadFile", f); this.getRequest().setAttribute("downloadFileName", name); this.getRequest().setAttribute("downloadFileDelete", Boolean.valueOf(delete)); return "forward:/frame-jsp/downloadFile.jsp"; } /** * 通过上传控件的名称过滤上传文件,<input type="file" name="inputName" ... * @param uploadList * @param inputName * @return */ public List getUploadFileByInputName( List uploadList, String inputName){ return FileUploadHelper.getUploadFileByInputName(uploadList, inputName); } /** * 得到全部的上传文件 * @return */ public List getAllUploadFile() { return FileUploadHelper.getAllUploadFile(this.getRequest()); } /** * 生成分页查询的参数,去调用正常的mybatis查询 * @param param 查询条件,可以为 null * @return */ public Map wrapPageSearchParam(Map param){ return PageUtil.wrapPageSearchParamMap(this.getRequest(), param); } /** * request.setAttribute(key, val); * * @param key * @param val */ public void setAttribute(String key, Object val){ this.getRequest().setAttribute(key, val); } public HttpServletRequest getRequest(){ HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); return req; } public RuntimeException exception(Throwable t){ return new RuntimeException(t); } public RuntimeException exception(String msg){ return new RuntimeException(msg); } public boolean isEmpty(String str){ return str==null || str.trim().length()==0; } /** * 设置ajax返回内容,并且提供返回页面
* 用在 controller 中 String 返回类型(非@ResonseBody)的方法中 * @param content * @return */ public String ajax(String content){ HttpServletRequest req = getRequest(); req.setAttribute("content", content); return "forward:/frame-jsp/ajax.jsp"; } }