分頁controller調用
liveDTO.setOffsetAndLimit(WebRequest.getOffset(), WebRequest.getLimit());
WebRequest
package cn.com.hwqh.util;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class WebRequest {
public static HttpServletRequest getHttpServletRequest(){
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
.getRequestAttributes())
.getRequest();
return request;
}
public static String getHeader(String key)
{
HttpServletRequest request = getHttpServletRequest();
return request.getHeader(key);
}
public static double getDouble(String key)
{
String value = getString(key);
return StringUtils.asDouble(value);
}
public static long getLong(String key)
{
String value = getString(key);
return StringUtils.asLong(value);
}
public static int getInt(String key)
{
String value = getString(key);
return StringUtils.asInt(value);
}
public static String getString(String key)
{
return getString(key, null);
}
public static boolean getBoolean(String key)
{
String value = getString(key);
return StringUtils.asBoolean(value, false);
}
public static List<String> getList(String key)
{
HttpServletRequest request = getHttpServletRequest();
String[] values = request.getParameterValues(key);
if (null == values || values.length == 0)return new ArrayList<>();
return Arrays.asList(values);
}
public static boolean getPurge()
{
String value = getString("purge");
return StringUtils.asBoolean(value, false);
}
public static int getLimit()
{
return 20;
}
public static int getOffset()
{
int value = getInt("pageNum");
if (value <= 0){
value = 1;
}
int offset = (value-1)*20;
return offset;
}
public static int getPageNo()
{
int value = getInt("pageNo");
if (value <= 0){
value = 1;
}
int offset = (value-1)*20;
return offset;
}
public static String getString(String key, String def)
{
HttpServletRequest request = getHttpServletRequest();
String value = request.getParameter(key);
if(StringUtils.isEmpty(value)) return def;
value = UrlUtils.decode(value);
value = value.trim();
return value;
}
public static JSONObject getJSON(String key)
{
HttpServletRequest request = getHttpServletRequest();
String value = request.getParameter(key);
return FastJsonHelper.toJSONObject(value);
}
/*
public static String replaceXSS(String value) {
if (value != null) {
try{
value = value.replace("+","%2B"); //'+' replace to '%2B'
value = URLDecoder.decode(value, "utf-8");
}catch(UnsupportedEncodingException e){
}catch(IllegalArgumentException e){
}
// Avoid null characters
value = value.replaceAll("\0", "");
// Avoid anything between script tags
Pattern scriptPattern = Pattern.compile("<script>(.*?)</script>", Pattern.CASE_INSENSITIVE);
value = scriptPattern.matcher(value).replaceAll("");
// Avoid anything in a src='...' type of expression
scriptPattern = Pattern.compile("src[\r\n]*=[\r\n]*\\\'(.*?)\\\'", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
value = scriptPattern.matcher(value).replaceAll("");
scriptPattern = Pattern.compile("src[\r\n]*=[\r\n]*\\\"(.*?)\\\"", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
value = scriptPattern.matcher(value).replaceAll("");
// Remove any lonesome </script> tag
scriptPattern = Pattern.compile("</script>", Pattern.CASE_INSENSITIVE);
value = scriptPattern.matcher(value).replaceAll("");
// Remove any lonesome <script ...> tag
scriptPattern = Pattern.compile("<script(.*?)>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
value = scriptPattern.matcher(value).replaceAll("");
// Avoid eval(...) expressions
scriptPattern = Pattern.compile("eval\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
value = scriptPattern.matcher(value).replaceAll("");
// Avoid expression(...) expressions
scriptPattern = Pattern.compile("expression\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
value = scriptPattern.matcher(value).replaceAll("");
// Avoid javascript:... expressions
scriptPattern = Pattern.compile("javascript:", Pattern.CASE_INSENSITIVE);
value = scriptPattern.matcher(value).replaceAll("");
// Avoid alert:... expressions
scriptPattern = Pattern.compile("alert", Pattern.CASE_INSENSITIVE);
value = scriptPattern.matcher(value).replaceAll("");
// Avoid οnlοad= expressions
scriptPattern = Pattern.compile("onload(.*?)=", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
value = scriptPattern.matcher(value).replaceAll("");
scriptPattern = Pattern.compile("vbscript[\r\n| | ]*:[\r\n| | ]*", Pattern.CASE_INSENSITIVE);
value = scriptPattern.matcher(value).replaceAll("");
}
return value;
} */
/**
* 過濾特殊字符
*/
public static String filter(String value) {
if (value == null) {
return null;
}
StringBuffer result = new StringBuffer(value.length());
for (int i=0; i<value.length(); ++i) {
switch (value.charAt(i)) {
case '<':
result.append("<");
break;
case '>':
result.append(">");
break;
case '"':
result.append("\"");
break;
case '\'':
result.append("'");
break;
case '%':
result.append("%");
break;
case ';':
result.append(";");
break;
case '(':
result.append("(");
break;
case ')':
result.append(")");
break;
case '&':
result.append("&");
break;
case '+':
result.append("+");
break;
default:
result.append(value.charAt(i));
break;
}
}
return result.toString();
}
public static String getRemoteIP() {
HttpServletRequest request = getHttpServletRequest();
String ip = request.getHeader("X-Real-IP");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
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.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
public static String getRemoteIP(HttpServletRequest request) {
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.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
public static void main(String args[]){
// String sql="1' or '1'='1";
// System.out.println("防SQL注入======> "+StringEscapeUtils.escapeSql(sql)); //防SQL注入
//
// System.out.println("轉義HTML,注意漢字======> "+StringEscapeUtils.escapeHtml("<font>chen磊 xing</font>")); //轉義HTML,注意漢字
// System.out.println("反轉義HTML======> "+StringEscapeUtils.unescapeHtml("<font>chen磊 xing</font>")); //反轉義HTML
//
// System.out.println("轉成Unicode編碼======> "+StringEscapeUtils.escapeJava("張三")); //轉義成Unicode編碼
// System.out.println("轉義XML======> "+StringEscapeUtils.escapeXml("<name>張三</name>")); //轉義xml
// System.out.println("反轉義XML======> "+StringEscapeUtils.unescapeXml("<name>張三</name>")); //轉義xml
//
String str = "什麼";
// int len = ;
System.out.println(UrlUtils.encode(str).indexOf("%"));
// if(value.indexOf("%"))
String sql="1' or '1'='1";
System.out.println("防SQL注入======> "+ StringEscapeUtils.escapeSql(sql)); //防SQL注入
}
}
Model
package cn.com.hwqh.common;
import cn.com.hwqh.util.FastJsonHelper;
import java.io.Serializable;
public class Model implements Serializable {
private static final long serialVersionUID = 1L;
@Override
public String toString() {
String rs;
try {
rs = FastJsonHelper.jsonEncode(this);
} catch (Exception e) {
rs = super.toString();
}
return rs;
}
private Integer offset;
private Integer limit;
public Integer getOffset() {
return offset;
}
public void setOffset(Integer offset) {
this.offset = offset;
}
public Integer getLimit() {
return limit;
}
public void setLimit(Integer limit) {
this.limit = limit;
}
public void setOffsetAndLimit(Integer offset,Integer limit){
this.offset = offset;
this.limit = limit;
}
}