1
hao
2025-05-20 8e24c6fea30d9b179375ee2893710cdec2443b13
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package com.app.base.control;
 
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.ApplicationContext;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
import com.system.log.service.SysLogService;
 
public abstract class BaseController {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
 
    @Autowired
    private ApplicationContext applicationContext;
    /*@Autowired
    private ResourceBundleMessageSource messageSource;*/
 
    public ApplicationContext getApplicationContext() {
        return this.applicationContext;
    }
    
    @Autowired
    private SysLogService sysLogService;
    public SysLogService getSysLogService(){
        return this.sysLogService;
    }
 
    /**
     * 获取servlet属性
     * @return
     */
    protected final ServletRequestAttributes getServletRequestAttributes() {
        return (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    }
 
    /**
     * 获取request
     * @return
     */
    protected HttpServletRequest getRequest() {
        return getServletRequestAttributes().getRequest();
    }
 
    /**
     * 获取response
     * @return
     */
    protected HttpServletResponse getResponse() {
        return getServletRequestAttributes().getResponse();
    }
 
    protected String getHeader(String key) {
        return getRequest().getHeader(key);
    }
 
    protected PageRequest getPageRequest() {
        return this.getPageRequest(null);
    }
 
    protected PageRequest getPageRequest(Sort sort) {
        HttpServletRequest request = getRequest();
        int page = 1;
        int rows = 10;
        if (null != request.getParameter("page")) {
            page = Integer.parseInt(request.getParameter("page"));
        }
        if(null!=request.getParameter("rows")) {
            rows = Integer.parseInt(request.getParameter("rows"));
        }
        //Paginator paginator = new Paginator();
        PageRequest pageRequest = new PageRequest(page-1, rows, sort);
        
        return pageRequest;
    }
    
    protected PageRequest getPageRequestK3(Sort sort) {
        HttpServletRequest request = getRequest();
        int page = 1;
        int rows = 10;
        if (null != request.getParameter("pageK3")) {
            page = Integer.parseInt(request.getParameter("pageK3"));
        }
        if(null!=request.getParameter("rowsK3")) {
            rows = Integer.parseInt(request.getParameter("rowsK3"));
        }
        //Paginator paginator = new Paginator();
        PageRequest pageRequest = new PageRequest(page-1, rows, sort);
        
        return pageRequest;
    }
 
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        registerCustomEidtorsForWebDataBinder(binder);
    }
 
    /**
     * 日期类型转换
     * 
     */
    protected void registerCustomEidtorsForWebDataBinder(WebDataBinder binder) {
        // 日期格式化
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
 
    /**
     * 国际化
     * @param code    编码
     * @return
     */
    protected String getText(String code) throws Exception {
        return getText(code, new Object[]{}, "UnKnown", Locale.CHINA);
    }
 
    /**
     * 国际化
     * @param code    编码
     * @param args    占位符
     * @return
     */
    protected String getText(String code, Object[] args) {
        return getText(code, args, code, Locale.CHINA);
    }
 
    /**
     * 国际化
     * @param code    编码
     * @param args    占位符
     * @param defaultMessage    默认值
     * @return
     */
    protected String getText(String code, Object[] args, String defaultMessage) {
        return getText(code, args, defaultMessage, Locale.CHINA);
    }
 
    /**
     * 国际化
     * @param code    编码
     * @param args    占位符
     * @param defaultMessage    默认值
     * @param locale    语言区域
     * @return
     */
    protected String getText(String code, Object[] args, String defaultMessage, Locale locale) {
        return "";//messageSource.getMessage(code, args, defaultMessage, locale);
    }
}