import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
public class BaseController {
protected Logger logger = LoggerFactory.getLogger(this.getClass());
private Integer defaultPage = 1;
private Integer defaultPageSize = 15;
public Map<String, Object> getParams() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.getRequest();
Map<String, Object> initParams = ParamsUtil.handleServletParameter(request);
int currentPage = defaultPage;
if (!isNullOrEmpty(initParams.get("page"))) {
try {
currentPage = Integer.parseInt(String.valueOf(initParams.get("page")));
} catch (NumberFormatException e) {
currentPage = defaultPage;
}
}
initParams.put("currentPage", currentPage);
int pageSize = defaultPageSize;
if (!isNullOrEmpty(initParams.get("rows"))) {
try {
pageSize = Integer.parseInt(String.valueOf(initParams.get("rows")));
} catch (NumberFormatException e) {
pageSize = defaultPageSize;
}
}
initParams.put("pageSize", pageSize);
initParams.put("IP", NetWorkUtil.getRemortIP(request));
logger.debug("input params is {}",new Object[]{initParams});
return initParams;
}
public static boolean isNullOrEmpty(Object o) {
return o == null || String.valueOf(o).trim().length() == 0;
}
public String getRequestContent(HttpServletRequest request) {
StringBuilder buffer = new StringBuilder();
BufferedReader reader = null;
try {
request.setCharacterEncoding("UTF-8");
reader = new BufferedReader(new InputStreamReader(
request.getInputStream(), "UTF-8"));
String line = null;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
} catch (Exception e) {
logger.error("接收request请求处理异常:",e);
logger.info("接收request请求报文:" + buffer.toString());
} finally {
if (null != reader) {
try {
reader.close();
} catch (IOException e) {
logger.error("接收request请求信息时失败:", e);
}
}
}
return buffer.toString();
}
}
JAVA工具类(6)- Controller继承类获取请求参数
最新推荐文章于 2022-05-19 20:47:50 发布