-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseController.java
More file actions
128 lines (117 loc) · 3.46 KB
/
BaseController.java
File metadata and controls
128 lines (117 loc) · 3.46 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
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* @Project:IntelligentClass
* @PackageName:com.tiye.IntelligentClass.controller
* @Auther: 张颖
* @Date: 2018年05月15日 08:37
* @Description:
*/
public class BaseController {
//protected Logger log = Logger.getLogger(this.getClass());
/**
* springMVC 获取requset
* @return
*/
public HttpServletRequest getRequest() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
return request;
}
/**
* 获取response
*
* @return
*/
public HttpServletResponse getResponse() {
HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
return response;
}
/**
* 获取session
* @return
*/
public HttpSession getSession() {
HttpSession session = this.getRequest().getSession();
return session;
}
/**
* 获取ServletContext
* @return
*/
public ServletContext getServletContent() {
// ServletContext application= request.getServletContext();
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
ServletContext servletContext = webApplicationContext.getServletContext();
return servletContext;
}
/**
* 获取ModelAndView
* @return
*/
public ModelAndView getModelAndView() {
return new ModelAndView();
}
public ModelAndView get404ModelAndView() {
ModelAndView view = new ModelAndView();
view.setViewName("404");
return view;
}
/**
* 过滤参数
*
* @return
*/
public ParameterMap getParameterMap() {
ParameterMap pm = new ParameterMap(this.getRequest());
pm.put("rip", getRemortIP());
return pm;
}
/**
* 获取ip
*
* @return
*/
public String getRemortIP() {
HttpServletRequest request = this.getRequest();
String ip = "";
if (request.getHeader("x-forwarded-for") == null) {
ip = request.getRemoteAddr();
} else {
ip = request.getHeader("x-forwarded-for");
}
return ip;
}
/**
* 获取port
* @return
*/
public int getPort() {
return this.getRequest().getServerPort();
}
/**
* 获取ip
* @param
* @return
*/
public String getIpAddr() {
HttpServletRequest request = this.getRequest();
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
}