📄 .#common.java.1.3
字号:
value = request.getParameter(param);
String mode = "";// 查询模式
if (param.startsWith("_")) {
continue;
}
if (value == null || value.equalsIgnoreCase("null")) {
continue;
}
if (value.trim().length() == 0) {
continue;
}
value = value.trim();
if (param.indexOf("$Q_") != -1) {
i++;
if (i != 1) {
buf.append(" and ");
}
// 字段名
buf.append(param.substring(param.indexOf("$Q_") + 3));
// 保存查询条件
qMap.put(param, value);
// 防止'注入
if (value.indexOf("'") != -1) {
value = value.replaceAll("'", "''");
}
String temp = param.substring(0, param.indexOf("$Q_"));
if (!COMMON.isEmpty(temp)) {
mode = temp;
}
// 查询值
// 是否是时间
boolean timeFlag = false;
if (value.indexOf("-") != -1) {
SimpleDateFormat dateFormat = new SimpleDateFormat(
DATA_FORMAT);
SimpleDateFormat timeFormat = new SimpleDateFormat(
TIME_FORMAT);
try {
dateFormat.parse(value);
timeFlag = true;
} catch (ParseException e) {
try {
timeFormat.parse(value);
timeFlag = true;
} catch (ParseException e1) {
timeFlag = false;
}
}
}
// 查询方式
if (isEmpty(mode)) {
// 默认查询模式
if (timeFlag) {
mode = "=";
} else {
mode = "like";
}
}
buf.append(" " + mode + " ");
if (!timeFlag) {
buf.append(" '");
}
if (mode.equals("like")) {
buf.append("%");
}
if (timeFlag) {
if (value.indexOf(":") == -1 && mode.indexOf("<") != -1) {
value += " 23:59:59";
}
if (DB_DIALECT.equals("ORACLE")) {
buf.append("To_date('" + value
+ "','yyyy-mm-dd hh24:mi:ss')");
} else {
buf.append("'" + value + "'");
}
} else {
buf.append(value);
}
if (mode.equals("like")) {
buf.append("%");
}
if (!timeFlag) {
buf.append("'");
}
}
}
request.setAttribute("qMap", qMap);
return buf.toString();
}
/**
* 从集合对象中获取特定值属性的链接字符串,以逗号分隔
*
* @param list
* 集合对象
* @param propName
* 元素属性名
* @param add
* 元素的附加值,如加上“'”,构成SQL的in条件
* @param expression
* 标准的ognl表达式
* @return
*/
public static String getPropValueFromList(Collection list, String propName,
String add, String expression) {
if (!COMMON.isEmpty(list)) {
StringBuffer temp = new StringBuffer();
for (Iterator iter = list.iterator(); iter.hasNext();) {
Object obj = iter.next();
try {
OgnlUtil parser = null;
if (expression != null) {
parser = new OgnlUtil(obj);
}
if (expression == null
|| (parser.getValue(expression))
.equals(Boolean.TRUE)) {
if (temp.length() != 0) {
temp.append(",");
}
if (!COMMON.isEmpty(add)) {
temp.append(add);
}
Object value = null;
if (obj instanceof Map) {
value = ((Map) obj).get(propName);
} else {
value = PropertyUtils.getProperty(obj, propName);
}
temp.append(value);
if (!COMMON.isEmpty(add)) {
temp.append(add);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return temp.toString();
} else {
return null;
}
}
/**
* @see #getPropValueFromList(Collection, String, String, String)
* @param list
* @param propName
* @param add
* @return
*/
public static String getPropValueFromList(Collection list, String propName,
String add) {
return getPropValueFromList(list, propName, add, null);
}
/**
* 从集合中获取满足特定表达式的对象
*
* @param list
* @param expression
* 标准的OGNL表达式 由于只返回第一个满足表达式的值,因此通常表达式匹配主键属性
* @return
*/
public static Object getObjectFromList(Collection list, String expression) {
if (COMMON.isEmpty(list)) {
return null;
}
for (Iterator iter = list.iterator(); iter.hasNext();) {
Object element = iter.next();
OgnlUtil parser = new OgnlUtil(element);
try {
if (parser.getValue(expression).equals(Boolean.TRUE)) {
return element;
}
} catch (SystemRuleException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 从集合中获取满足特定表达式的对象集合
*
* @param list
* @param expression
* 标准的OGNL表达式
* @return
*/
public static List getListFromList(Collection list, String expression) {
if (COMMON.isEmpty(list)) {
return null;
}
List list1 = new ArrayList();
for (Iterator iter = list.iterator(); iter.hasNext();) {
Object element = iter.next();
OgnlUtil parser = new OgnlUtil(element);
try {
if (parser.getValue(expression).equals(Boolean.TRUE)) {
list1.add(element);
}
} catch (SystemRuleException e) {
e.printStackTrace();
}
}
return list1;
}
/**
* @see #getTimeInterval(Timestamp, Timestamp, boolean)
* @param timestamp1
* @param timestamp2
* @return
*/
public static String getTimeInterval(Timestamp timestamp1,
Timestamp timestamp2) {
return getTimeInterval(timestamp1, timestamp2, false);
}
/**
* 取时间差
*
* @param timestamp1
* @param timestamp2
* @param showSecond
* 是否显示秒
* @return
*/
public static String getTimeInterval(Timestamp timestamp1,
Timestamp timestamp2, boolean showSecond) {
if (timestamp1 == null || timestamp2 == null)
return "";
StringBuffer buf = new StringBuffer();
long time1 = timestamp1.getTime();
long time2 = timestamp2.getTime();
long time = Math.abs(time1 - time2);
// day
long day = time / (24 * 60 * 60 * 1000L);
// hour
time = time - day * 24 * 60 * 60 * 1000L;
long hour = time / (60 * 60 * 1000L);
// minute
time = time - hour * 60 * 60 * 1000L;
long minute = time / (60 * 1000L);
// second
time = time - minute * 60 * 1000L;
long second = time / 1000L;
if (day != 0) {
buf.append(day + "天");
}
if (hour != 0) {
buf.append(hour + "时");
}
if (minute != 0) {
buf.append(minute + "分");
}
if (showSecond || buf.length() == 0) {
if (second != 0) {
buf.append(second + "秒");
}
}
return buf.toString();
}
/**
* 转换字符串至HTML格式
*
* @param src
* @return
*/
public static String toHtmlStr(String src) {
if (src == null || src.length() == 0)
return "";
int i;
StringBuffer aimStrBuf = new StringBuffer();
StringBuffer resStrBuf = new StringBuffer(src);
int strLength = resStrBuf.length();
char tmpChar = 0;
for (i = 0; i < strLength; i++) {
tmpChar = resStrBuf.charAt(i);
if (tmpChar == '<') {
aimStrBuf.append(new char[] { '&', 'l', 't', ';' });
} else if (tmpChar == '<') {
aimStrBuf.append(new char[] { '&', 'g', 't', ';' });
} else if (tmpChar == '\n') {
aimStrBuf.append(new char[] { '<', 'b', 'r', '>' });
} else if (tmpChar == '\r') {
} else if (tmpChar == '\t') {
aimStrBuf.append(new char[] { '&', 'n', 'b', 's', 'p', ';',
'&', 'n', 'b', 's', 'p', ';', '&', 'n', 'b', 's', 'p',
';', '&', 'n', 'b', 's', 'p', ';' });
} else if (tmpChar == ' ') {
aimStrBuf.append(new char[] { '&', 'n', 'b', 's', 'p', ';' });
} else if (tmpChar == '"') {
aimStrBuf.append(new char[] { '\\', '“' });
} else if (tmpChar == '\'') {
aimStrBuf.append(new char[] { '\\', '\'' });
} else if (tmpChar == '\\') {
aimStrBuf.append(new char[] { '\\', '\\' });
} else {
aimStrBuf.append(tmpChar);
}
}
return aimStrBuf.toString();
}
/*method 将字符串类型的日期转换为一个timestamp(时间戳记java.sql.Timestamp)
*@param dateString 需要转换为timestamp的字符串
*@return dataTime timestamp
*/
public static java.sql.Timestamp string2Time(String dateString)
throws java.text.ParseException {
DateFormat dateFormat;
dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss.SSS", Locale.ENGLISH);
dateFormat.setLenient(false);
java.util.Date timeDate = dateFormat.parse(dateString);//util类型
java.sql.Timestamp dateTime = new java.sql.Timestamp(timeDate.getTime());//Timestamp类型,timeDate.getTime()返回一个long型
return dateTime;
}
/**
* 转换日期为Timestamp
* */
public static java.sql.Timestamp toTimestamp() {
return new java.sql.Timestamp(new java.util.Date().getTime());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -