This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
k18-ntcs-web-argus-service/src/main/java/com/nis/web/controller/BaseController.java
2018-08-07 11:54:36 +08:00

172 lines
4.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.nis.web.controller;
import java.beans.PropertyEditorSupport;
import java.io.IOException;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.nis.util.DateUtils;
import com.nis.util.JsonMapper;
import com.nis.web.service.ArchiveServcie;
import com.nis.web.service.AreaService;
import com.nis.web.service.DictService;
import com.nis.web.service.LogService;
import com.nis.web.service.MenuService;
import com.nis.web.service.OfficeService;
import com.nis.web.service.RoleService;
import com.nis.web.service.SwaggerLogService;
import com.nis.web.service.SystemService;
import com.nis.web.service.UserService;
public class BaseController {
@Autowired
protected UserService userService;
@Autowired
protected SystemService systemService;
@Autowired
protected OfficeService officeService;
@Autowired
protected AreaService areaService;
@Autowired
protected RoleService roleService;
@Autowired
protected MenuService menuService;
@Autowired
protected LogService logService;
@Autowired
protected SwaggerLogService swaggerLogService;
@Autowired
protected ArchiveServcie archiveServcie;
@Autowired
protected DictService dictService;
protected final Logger logger = Logger.getLogger(this.getClass());
/**
* 管理基础路径
*/
@Value("${adminPath}")
protected String adminPath;
/**
* 前端基础路径
*/
@Value("${frontPath}")
protected String frontPath;
/**
* 前端URL后缀
*/
@Value("${urlSuffix}")
protected String urlSuffix;
/**
* 客户端返回JSON字符串
* @param response
* @param object
* @return
*/
protected String renderString(HttpServletResponse response, Object object) {
return renderString(response, JsonMapper.toJsonString(object), "application/json");
}
/**
* 客户端返回字符串
* @param response
* @param string
* @return
*/
protected String renderString(HttpServletResponse response, String string, String type) {
try {
response.reset();
response.setContentType(type);
response.setCharacterEncoding("utf-8");
response.getWriter().print(string);
return null;
} catch (IOException e) {
return null;
}
}
/**
* 添加Model消息
* @param
*/
protected void addMessage(Model model, String... messages) {
StringBuilder sb = new StringBuilder();
for (String message : messages){
sb.append(message).append(messages.length>1?"<br/>":"");
}
model.addAttribute("message", sb.toString());
}
/**
* 添加Flash消息
* @param
*/
protected void addMessage(RedirectAttributes redirectAttributes, String... messages) {
StringBuilder sb = new StringBuilder();
for (String message : messages){
sb.append(message).append(messages.length>1?"<br/>":"");
}
redirectAttributes.addFlashAttribute("message", sb.toString());
}
/**
* 初始化数据绑定
* 1. 将所有传递进来的String进行HTML编码防止XSS攻击
* 2. 将字段中Date类型转换为String类型
*/
@InitBinder
protected void initBinder(WebDataBinder binder) {
// String类型转换将所有传递进来的String进行HTML编码防止XSS攻击
binder.registerCustomEditor(String.class, new PropertyEditorSupport() {
@Override
public void setAsText(String text) {
setValue(text == null ? null : StringEscapeUtils.escapeHtml4(text.trim()));
}
@Override
public String getAsText() {
Object value = getValue();
return value != null ? value.toString() : "";
}
});
// Date 类型转换
binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
@Override
public void setAsText(String text) {
setValue(DateUtils.parseDate(text));
}
});
}
}