BaseController.java
4.7 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
package com.sinocontact.app.controller;
import com.alibaba.fastjson.JSON;
import com.sinocontact.app.common.CommonConstant;
import com.sinocontact.app.dao.ranking.RankingMapper;
import com.sinocontact.app.entity.User;
import com.sinocontact.app.service.ranking.RankingService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* 所有controller的基类
* @author todd
* @since 2019/1/22
*/
@Controller
public class BaseController {
private static final Logger logger = Logger.getLogger(BaseController.class);
//公用
private HttpServletRequest request ;
private HttpServletResponse response;
private Model model;
private HttpSession session;
public HttpServletRequest getRequest() {
return request;
}
public HttpServletResponse getResponse() {
return response;
}
public Model getModel() {
return model;
}
public HttpSession getSession() {
return session;
}
@ModelAttribute
public void setModel(HttpServletRequest request,HttpServletResponse response,Model model,HttpSession session){
this.model = model;
this.request = request;
this.response = response;
this.session = session;
}
/**
* 得到request中参数
* @author todd
* @since 2019/1/22
*/
public String getParameter(String name){
return this.getRequest().getParameter(name);
}
/**
* 添加页面变量属性
* @author todd
* @since 2019/1/22
*/
public void putObject(String name, Object obj){
model.addAttribute(name, obj);
}
/**
* 向cookie中写入一条记录
* @author todd
* @since 2019/1/22
*/
public void setCookieValue(String name, String value){
try{
Cookie cookie = new Cookie(name,value);
cookie.setPath("/");
response.addCookie(cookie);
}catch(Exception e){
logger.error("向cookie中写入name="+name+",value="+value+"出现异常:",e);
}
}
/**
* 设置cookie失效时间
* @param expiry 失效时间(秒)
* @author todd
* @since 2019/1/22
*/
public void setCookieValue(String name, String value, int expiry){
try{
Cookie cookie = new Cookie(name,value);
cookie.setMaxAge(expiry);
cookie.setPath("/");
response.addCookie(cookie);
}catch(Exception e){
logger.error("向cookie中写入name="+name+",value="+value+"出现异常:",e);
}
}
/**
* 删除指定的cookie
* @author todd
* @since 2019/1/22
*/
public void deleteCookie(String name){
try{
Cookie cookie = new Cookie(name,"");
cookie.setMaxAge(0);
cookie.setPath("/");
response.addCookie(cookie);
}catch(Exception e){
logger.error("删除cookie"+name+"出现异常:",e);
}
}
/**
* 从cookie中获取key为name的值
* @author todd
* @since 2019/1/22
*/
public String getCookieValue(String name){
String value = null;
try{
Cookie[] cookies = request.getCookies();
if(cookies == null)
return value;
//遍历方式查找Cookies中是否存在 name
for(Cookie c : cookies){
if(c.getName().equals(name) == true){//在cookies中找到name的cookie
value = c.getValue();
break;
}
}
}catch(Exception e){
value = null;
logger.error("获取cookie中name="+name+"的数据时出现异常:",e);
}
return value;
}
/**
* 把javabean转换为json字符串
* @author todd
* @since 2019/1/22
*/
public String renderJSON(Object javaObject){
return JSON.toJSON(javaObject).toString();
}
/**
* 获取用户信息
* @return User
* @author caoMingYang
* @since 2019/4/12 15:12
*/
public User getUserInfo(){
return (User) getSession().getAttribute(CommonConstant.SESSION_KEY);
}
/**
* 得到ip地址
* @author todd
* @since 2019/1/22
*/
public String getIp() {
String ip = getRequest().getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = getRequest().getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = getRequest().getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = getRequest().getRemoteAddr();
}
return ip;
}
}