BizController.java
9.4 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package com.zteits.oa.report.web;
import com.alibaba.fastjson.JSONObject;
import com.zteits.oa.api.base.bean.BizResult;
import com.zteits.oa.api.base.bean.PageBean;
import com.zteits.oa.api.base.constants.ErrorType;
import com.zteits.oa.api.base.exception.BizException;
import com.zteits.oa.report.web.vo.EasyUIDataGridVO;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @描述:
* Copyright: Copyright (c) 2017 Alibb
*
* @ClassName: BizController.java
* @Description: 所有基于Spring MVC的Web控制器类(Action)的统一父类,提供一些便利的请求处理方法,如返回Json、文本数据等
* @version: v1.0.0
* @author: wangbiao
* @date: 2017年2月23日 下午1:26:50
*
* Modification History:
* Date Author Version Description
*---------------------------------------------------------*
* 2017年2月23日 wangbiao v1.0.0 创建
*/
public class BizController {
private static final String MIME_JSON = "application/json;charset=UTF-8";
/**
* 返回jqgrid格式数据
* @param bizResult
* @param voClass
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* 2017年5月12日 zhaowg
*/
protected <DTO,VO> BizResult<EasyUIDataGridVO<VO>> returnJqGridData(BizResult<PageBean<DTO>> bizResult, Class<VO> voClass) throws InstantiationException, IllegalAccessException {
PageBean<DTO> pageBean = getBizResultData(bizResult);
//将DTO转换为VO
List<VO> vos = new ArrayList<>();
if(!CollectionUtils.isEmpty(pageBean.getDataList())){
for (DTO dto : pageBean.getDataList()) {
VO vo = voClass.newInstance();
BeanUtils.copyProperties(dto, vo);
vos.add(vo);
}
}
//返回jqGrid数据
EasyUIDataGridVO<VO> jqGridDatas = new EasyUIDataGridVO<>();
jqGridDatas.setTotal(pageBean.getPageTotals()==null?0:pageBean.getPageTotals());
jqGridDatas.setRows(vos);
BizResult<EasyUIDataGridVO<VO>> bizResultVO = new BizResult<>();
bizResultVO.setCode(bizResult.getCode());
bizResultVO.setErrMsg(bizResult.getErrMsg());
bizResultVO.setData(jqGridDatas);
return bizResultVO;
}
/**
* 返回jqgrid格式数据
* @param bizResult
* @return
* 2017年5月12日 zhaowg
*/
protected <DTO> BizResult<EasyUIDataGridVO<DTO>> returnJqGridData(BizResult<PageBean<DTO>> bizResult) {
PageBean<DTO> pageBean = getBizResultData(bizResult);
//返回jqGrid数据
EasyUIDataGridVO<DTO> jqGridDatas = new EasyUIDataGridVO<>();
jqGridDatas.setTotal(pageBean.getPageTotals()==null?0:pageBean.getPageTotals());
jqGridDatas.setRows(pageBean.getDataList());
BizResult<EasyUIDataGridVO<DTO>> bizResultVO = new BizResult<>();
bizResultVO.setCode(bizResult.getCode());
bizResultVO.setErrMsg(bizResult.getErrMsg());
bizResultVO.setData(jqGridDatas);
return bizResultVO;
}
/**
* 分页结果封装后返回标准格式JSON串.<br/>
* @param response
* @param result
*/
@SuppressWarnings("unused")
public void returnJsonDataGrid(HttpServletResponse response,BizResult<?> result){
Map<String,Object> resultMap = new HashMap<String,Object>();
if(result == null){
throw new NullPointerException("result 为空,不能进行封装!");
}else if(result.getData() == null){
throw new NullPointerException("pageBean 为空,不能进行封装!");
}else{
if(result.getData() instanceof PageBean<?>) {
PageBean<?> pageBean = (PageBean<?>)result.getData();
Map<String,Object> map = new HashMap<String,Object>();
map.put("total", (null ==pageBean.getPageTotals()? 0 :pageBean.getPageTotals()));
map.put("rows", pageBean.getDataList() != null ? pageBean.getDataList() :new ArrayList<Object>() );
resultMap.put("code", result.getCode());
resultMap.put("msg", result.getErrMsg());
resultMap.put("data", map);
}else{
throw new ClassCastException("result.getData() 类型转换异常");
}
}
this.returnJson(response, resultMap);
}
/**
*
* @param data
* @param totalCount
* @return
* 2017年5月12日 zhaowg
*/
public <VO> BizResult<EasyUIDataGridVO<VO>> returnGridData4JqGrid(List<VO> data,Integer totalCount) {
//返回jqGrid数据
EasyUIDataGridVO<VO> jqGridDatas = new EasyUIDataGridVO<>();
jqGridDatas.setTotal(totalCount==null?0:totalCount);
jqGridDatas.setRows(data);
BizResult<EasyUIDataGridVO<VO>> bizResultVO = new BizResult<>();
bizResultVO.setCode(ErrorType.BIZ_SUCCESS.getCode());
bizResultVO.setErrMsg(ErrorType.BIZ_SUCCESS.getMsg());
bizResultVO.setData(jqGridDatas);
return bizResultVO;
}
/**
* 获取返回的数据
* @param bizResult
* @return
* 2017年5月16日 zhaowg
*/
public <T> T getBizResultData(BizResult<T> bizResult){
//有异常,抛出
if(!bizResult.isSuccess() || !bizResult.getCode().equals(ErrorType.BIZ_SUCCESS.getCode())){
throw new BizException(ErrorType.BIZ_ERROR,bizResult.getErrMsg());
}
return bizResult.getData();
}
/**
* 返回JSon格式的数据
*
* @param response
* @param data
* @throws Exception
*/
public String returnJson(HttpServletResponse response, Object data) {
return returnText(response, JSONObject.toJSONString(data), MIME_JSON);
}
/**
* 返回xml格式的数据
*
* @param response
* @param text
* @throws Exception
*/
public String returnXml(HttpServletResponse response, CharSequence text) {
return returnText(response, text, "text/xml;charset=UTF-8");
}
/**
* 返回文本数据
* @param response
* @param text
* @param contenttype 内容类型,如:text/plain、text/xml、application/json、text/json、text/javascript、application/javascript(不支持旧浏览器)
* @param encoding 字符集编码,如:GB18030、UTF-8,不建议使用GB2312和GBK
* @throws Exception
*/
public String returnText(HttpServletResponse response, CharSequence text,final String contenttype,final String encoding) {
return returnText(response, text, contenttype+";charset="+encoding);
}
/**
* 返回文本数据
*
* @param response
* @param text
* @param contenttype
* @throws IOException
* @author lihl2 2011-3-25
*/
public String returnText(HttpServletResponse response, CharSequence text,final String contenttype) {
response.setContentType(contenttype);
if (text != null) {
try {
response.getWriter().write(text.toString());
} catch (IOException e) {
throw new BizException(ErrorType.BIZ_ERROR,e);
}
}
return null;
}
/**
* 设置文件头格式
* @param response
* @param mimeType
* @param fileName
* @param size
*/
public void setFileHeader(HttpServletResponse response, CharSequence mimeType,
final CharSequence fileName, int size) {
response.reset();
// 设置response的Header
if(mimeType != null){
response.setContentType(mimeType.toString()) ;
}
try {
response.addHeader("Content-Disposition","attachment;filename=" + new String(fileName.toString().getBytes("GB18030"),"ISO-8859-1")) ;
} catch (UnsupportedEncodingException e) {
throw new BizException(ErrorType.BIZ_ERROR,"文件名编码转换失败") ;
}
if(size > 0){
response.addIntHeader("Content-Length", size) ;
}
}
/**
* 设置浏览器返回类型为Excel文件
* @param response
* @param mimeType
* @param fileName
* @param size
*/
public void setExcelFileHeader(HttpServletResponse response, CharSequence mimeType,
final CharSequence fileName, int size) {
setFileHeader(response, "application/vnd.ms-excel", fileName, size);
}
/**
* 文件下载
* @param response
* @param mimeType
* @param fileName
* @param file
* @param size
*/
public void downloadFile(HttpServletResponse response, CharSequence mimeType,
final CharSequence fileName, InputStream file, int size) {
setFileHeader(response, mimeType, fileName, size) ;
try {
OutputStream out = response.getOutputStream() ;
int l ;
while((l=file.read()) >= 0){
out.write(l) ;
}
out.flush() ;
} catch (IOException e) {
throw new BizException(ErrorType.BIZ_ERROR,"下载文件失败") ;
}finally {
if (null != file) {
try {
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 返回jqgrid格式数据
* @param bizResult
* @param voClass
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* 2017年5月12日 zhaowg
*/
protected <DTO,VO> BizResult<EasyUIDataGridVO<VO>> returnJqGridDataByList(BizResult<List<DTO>> bizResult,int total,Class<VO> voClass) throws InstantiationException, IllegalAccessException {
//List<DTO> pageBean = getBizResultData(bizResult);
//将DTO转换为VO
List<VO> vos = new ArrayList<>();
if(!CollectionUtils.isEmpty(bizResult.getData())){
for (DTO dto : bizResult.getData()) {
VO vo = voClass.newInstance();
BeanUtils.copyProperties(dto, vo);
vos.add(vo);
}
}
//返回jqGrid数据
EasyUIDataGridVO<VO> jqGridDatas = new EasyUIDataGridVO<>();
jqGridDatas.setTotal(total);
jqGridDatas.setRows(vos);
BizResult<EasyUIDataGridVO<VO>> bizResultVO = new BizResult<>();
bizResultVO.setCode(bizResult.getCode());
bizResultVO.setErrMsg(bizResult.getErrMsg());
bizResultVO.setData(jqGridDatas);
return bizResultVO;
}
}