liuxiaolong
2019-05-09 0d1d88cdb668e75ea8609417ac18ae19947e9525
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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<UploadFile> getUploadFileByInputName(
            List<UploadFile> uploadList, String inputName){
        return FileUploadHelper.getUploadFileByInputName(uploadList, inputName);
    }
    
    /**
     * 得到全部的上传文件
     * @return
     */
    public List<UploadFile> getAllUploadFile() {
        return FileUploadHelper.getAllUploadFile(this.getRequest());
    }
    /**
     * 生成分页查询的参数,去调用正常的mybatis查询
     * @param param 查询条件,可以为 null
     * @return 
     */
    public Map<String, Object> wrapPageSearchParam(Map<String,Object> 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返回内容,并且提供返回页面<br/>
     * 用在 controller 中 String 返回类型(非@ResonseBody)的方法中
     * @param content
     * @return
     */
    public String ajax(String content){
        HttpServletRequest req = getRequest();
        req.setAttribute("content", content);
        
        return "forward:/frame-jsp/ajax.jsp";
    }
 
}