📄 pageutil.java
字号:
package com.sxit.wap.common;
import java.sql.*;
import java.util.*;
import com.sxit.wap.exception.*;
public class PageUtil {
//public static int INIT_PAGE_ROW = 10;
public static int getWidth(String monitorType) {
if ("14".equals(monitorType)) {
return 780;
} else if ("15".equals(monitorType)) {
return 1004;
} else if ("17".equals(monitorType)) {
return 1280;
} else {
return 780;
}
}
public static int getHeight(String monitorType, boolean bannerColsed) {
int bannerHeight = 0;
if (bannerColsed) bannerHeight = 81;
if ("14".equals(monitorType)) {
return 270 + bannerHeight;
} else if ("15".equals(monitorType)) {
return 425 + bannerHeight;
} else if ("17".equals(monitorType)) {
return 425 + bannerHeight;
} else {
return 270 + bannerHeight;
}
}
/************************解析页面之间传递的参数的函数************************/
public static String parseString(String value, String errorMessage) throws AppException {
if (value == null) value = "";
return encode(value);
}
public static String parseString(String value) throws Exception {
return parseString(value, "");
}
public static int parseInt(String value, String errorMessage) throws AppException {
int result = 0;
try {
result = Integer.parseInt(value);
return result;
}catch(Exception e) {
System.out.println("Integer.parseInt(" + value + ")" + " :\n" + e);
throw new AppException(errorMessage);
}
}
public static int parseInt(String value) throws Exception {
return Integer.parseInt(value);
}
public static short parseShort(String value, String errorMessage) throws AppException {
short result = 0;
try {
result = Short.parseShort(value);
return result;
}catch(Exception e) {
System.out.println("Short.parseShort(" + value + ")" + " :\n" + e);
throw new AppException(errorMessage);
}
}
public static short parseShort(String value) throws Exception {
return Short.parseShort(value);
}
public static float parseFloat(String value, String errorMessage) throws AppException {
float result = 0;
try {
result = Float.parseFloat(value);
return result;
}catch(Exception e) {
System.out.println("Float.parseFloat(" + value + ")" + " :\n" + e);
throw new AppException(errorMessage);
}
}
public static float parseFloat(String value) throws Exception {
return Float.parseFloat(value);
}
public static long parseLong(String value, String errorMessage) throws AppException {
long result = 0;
try {
result = Long.parseLong(value);
return result;
}catch(Exception e) {
System.out.println("Long.parseLong(" + value + ")" + " :\n" + e);
throw new AppException(errorMessage);
}
}
public static long parseLong(String value) throws Exception {
return Long.parseLong(value);
}
public static Timestamp parseTimestamp(String value, String errorMessage) throws AppException {
if (StringUtil.isEmpty(value)) {
if (StringUtil.isNotEmpty(errorMessage)) {
throw new AppException(errorMessage);
} else {
throw new AppException("parseTimestamp error");
}
}
StringTokenizer st = new StringTokenizer(value, "-/");
String year = "", month = "", day = "";
int yyyy = 0, mm = 0, dd = 0;
if ( st.hasMoreTokens() ) {
year = st.nextToken();
}
if (!(year.length() == 4 || year.length() == 2)) throw new AppException(errorMessage);
try {yyyy=Integer.parseInt(year);}catch(Exception e){throw new AppException(errorMessage);}
if ( st.hasMoreTokens() ) {
month = st.nextToken();
}
if (!(month.length() == 2 || month.length() == 1)) throw new AppException(errorMessage);
try {mm=Integer.parseInt(month);}catch(Exception e){throw new AppException(errorMessage);}
if ( st.hasMoreTokens() ) {
day = st.nextToken();
}
if (!(day.length() == 2 || day.length() == 1)) throw new AppException(errorMessage);
try {dd=Integer.parseInt(day);}catch(Exception e){throw new AppException(errorMessage);}
return DateUtil.getTimestamp(yyyy, mm, dd);
}
public static Timestamp parseTimestamp(String value) throws AppException {
return parseTimestamp(value, "");
}
public static String[] parseStringArray(String[] value, String errorMessage) throws AppException {
if (value == null) return new String[0];
String result[] = new String[value.length];
try {
for (int i = 0; i < value.length; i++) {
result[i] = (value[i] == null ? "" : encode(value[i]));
}
return result;
}catch(Exception e) {
throw new AppException(errorMessage);
}
}
public static String[] parseStringArray(String[] value) throws Exception {
String result[] = new String[value.length];
for (int i = 0; i < value.length; i++) {
result[i] = (value[i] == null ? "" : encode(value[i]));
}
return result;
}
public static int[] parseIntArray(String[] value, String errorMessage) throws AppException {
if (value == null) return new int[0];
int result[] = new int[value.length];
try {
for (int i = 0; i < value.length; i++) {
result[i] = Integer.parseInt(value[i]);
}
return result;
}catch(Exception e) {
System.out.println("Integer.parseInt(" + value + ")" + " :\n" + e);
throw new AppException(errorMessage);
}
}
public static long[] parseLongArray(String[] value, String errorMessage) throws AppException {
if (value == null) return new long[0];
long result[] = new long[value.length];
try {
for (int i = 0; i < value.length; i++) {
result[i] = Long.parseLong(value[i]);
}
return result;
}catch(Exception e) {
System.out.println("Long.parseLong(" + value + ")" + " :\n" + e);
throw new AppException(errorMessage);
}
}
public static int[] parseIntArray(String[] value) throws Exception {
int result[] = new int[value.length];
for (int i = 0; i < value.length; i++) {
result[i] = Integer.parseInt(value[i]);
}
return result;
}
public static int parseIntOfSubString(String value, int pos, String errorMessage) throws AppException {
StringTokenizer st = new StringTokenizer(value, ";");
int i = 0;
while (st.hasMoreTokens()) {
i++;st.nextToken();
}
String[] arr = new String[i];
i = 0;
st = new StringTokenizer(value, ";");
while (st.hasMoreTokens()) {
arr[i++] = st.nextToken();
}
if (pos < arr.length) {
return parseInt(arr[pos], errorMessage);
} else {
throw new AppException(errorMessage);
}
}
public static int parseIntOfSubString(String value, int pos) throws AppException {
return parseIntOfSubString(value, pos, "");
}
public static int[] parseIntArrayOfSubString(String value[], int pos, String errorMessage) throws AppException {
if (value == null) return new int[0];
int[] returnValue = new int[value.length];
for (int i=0; i<value.length; i++) {
returnValue[i] = parseIntOfSubString(value[i], pos, errorMessage);
}
return returnValue;
}
public static int[] parseIntArrayOfSubString(String value[], int pos) throws AppException {
return parseIntArrayOfSubString(value, pos, "");
}
public static String parseStringField(String value, String cFieldName, boolean enableNull, int length) throws AppException {
if (StringUtil.isEmpty(value)) {
if (enableNull) return "";
else throw new AppException(cFieldName + "不能为空");
}
String t = parseString(value, cFieldName + "错误");
//if (value.getBytes().length == 0 && !enableNull) throw new AppException(cFieldName + "不能为空");
if (value.getBytes().length > length) throw new AppException(cFieldName + "长度不能超过" + length + "位");
return t;
}
public static String parseStringField(String value, String cFieldName, boolean enableNull) throws AppException {
if (StringUtil.isEmpty(value)) {
if (enableNull) return "";
else throw new AppException(cFieldName + "不能为空");
}
String t = parseString(value, cFieldName + "错误");
//if (value.getBytes().length == 0 && !enableNull) throw new AppException(cFieldName + "不能为空");
return t;
}
public static int parseIntField(String value, String cFieldName, boolean enableNull, int low, int high) throws AppException {
if (StringUtil.isEmpty(value)) {
if (enableNull) return 0;
else throw new AppException(cFieldName + "不能为空");
}
int t = parseInt(value, cFieldName + "只能为大于" + low + "小于" + high + "的整数");
if (t == 0 && !enableNull) throw new AppException(cFieldName + "不能为空或0");
if (t < low || t > high) throw new AppException(cFieldName + "只能为大于" + low + "小于" + high + "的整数");
return t;
}
public static int parseIntField(String value, String cFieldName, boolean enableNull) throws AppException {
if (StringUtil.isEmpty(value)) {
if (enableNull) return 0;
else throw new AppException(cFieldName + "不能为空");
}
int t = parseInt(value, cFieldName + "只能为大于0小于65535的整数");
if (t == 0 && !enableNull) throw new AppException(cFieldName + "不能为空或0");
return t;
}
public static long parseLongField(String value, String cFieldName, boolean enableNull, long low, long high) throws AppException {
if (StringUtil.isEmpty(value)) {
if (enableNull) return 0L;
else throw new AppException(cFieldName + "不能为空");
}
long t = parseLong(value, cFieldName + "只能为大于" + low + "小于" + high + "的整数");
if (t == 0 && !enableNull) throw new AppException(cFieldName + "不能为空或0");
if (t < low || t > high) throw new AppException(cFieldName + "只能为大于" + low + "小于" + high + "的整数");
return t;
}
public static long parseLongField(String value, String cFieldName, boolean enableNull) throws AppException {
if (StringUtil.isEmpty(value)) {
if (enableNull) return 0L;
else throw new AppException(cFieldName + "不能为空");
}
long t = parseLong(value, cFieldName + "只能为大于0小于4294967295的整数");
if (t == 0 && !enableNull) throw new AppException(cFieldName + "不能为空或0");
return t;
}
public static float parseFloatField(String value, String cFieldName, boolean enableNull, float low, float high) throws AppException {
if (StringUtil.isEmpty(value)) {
if (enableNull) return 0F;
else throw new AppException(cFieldName + "不能为空");
}
float t = parseFloat(value, cFieldName + "只能为大于" + low + "小于" + high + "的数据");
if (t == 0 && !enableNull) throw new AppException(cFieldName + "不能为空或0");
if (t < low || t > high) throw new AppException(cFieldName + "只能为大于" + low + "小于" + high + "的数据");
return t;
}
public static float parseFloatField(String value, String cFieldName, boolean enableNull) throws AppException {
if (StringUtil.isEmpty(value)) {
if (enableNull) return 0F;
else throw new AppException(cFieldName + "不能为空");
}
float t = parseFloat(value, cFieldName + "数值输入错误");
if (t == 0 && !enableNull) throw new AppException(cFieldName + "不能为空或0");
return t;
}
public static Timestamp parseDateField(String value, String cFieldName, boolean enableNull) throws AppException {
if (StringUtil.isEmpty(value)) {
if (enableNull) return null;
else throw new AppException(cFieldName + "不能为空");
}
Timestamp t = parseTimestamp(value, cFieldName + "输入错误");
return t;
}
/************************分页函数************************/
public static int getPageRow(String pageRow, int initPageRow) {
int value = initPageRow;//INIT_PAGE_ROW;
try {
value = Integer.parseInt(pageRow);
}catch(Exception e){}
if (value == 0) value = initPageRow;//INIT_PAGE_ROW;
return value;
}
public static int getPageCount(int rowCount, int pageRow) {
if (rowCount == 0) return 1;
//if (pageRow == 0) pageRow = INIT_PAGE_ROW;
int pageCount = rowCount / pageRow;
if (rowCount % pageRow != 0) pageCount++;
return pageCount;
}
public static int getPageNum(String pageNum, int pageCount) {
int value = 1;
try{
value = Integer.parseInt(pageNum);
}catch(Exception e){}
if (value < 1) value = 1;
else if (value > pageCount) value=pageCount;
return value;
}
public static String encode(String value) {return value;
/*try {
return new String(value.getBytes("gb2312"));//iso-8859-1
} catch (Exception e) {
return "";
}*/
}
public static String outHtml(int rowCount, int pageRow, int pageCount, int pageNum) {
String sql = "<tr height=\"22\"> " +
"<td align=\"center\" nowrap colspan=\"100\" bgcolor=\"#FFFFFF\"> " +
"共" + rowCount + "行,分" + pageCount + "页,现在是第" + pageNum + "页 " +
((pageNum != 1)?"<a href=\"javascript:goToPage(1,'" + pageRow + "')\">":"") + "[首页]" + ((pageNum != 1)?"</a>":"") +
((pageNum != 1)?"<a href=\"javascript:goToPage('" + (pageNum-1) + "','" + pageRow + "')\">":"") + "[上页]" + ((pageNum != 1)?"</a>":"") +
((pageNum != pageCount)?"<a href=\"javascript:goToPage('" + (pageNum+1) + "','" + pageRow + "')\">":"") + "[下页]" + ((pageNum != pageCount)?"</a>":"") +
((pageNum != pageCount)?"<a href=\"javascript:goToPage('" + pageCount + "','" + pageRow + "')\">":"") + "[末页]" + ((pageNum != pageCount)?"</a>":"") +
" 每页显示行数: " +
"<input type=\"text\" name=\"rows\" value=\"" + pageRow + "\" size=\"3\"> " +
"<a href=\"javascript:rowFlush();\">[刷新]</a> </td> " +
"</tr> ";
return sql;
}
public static void main(String[] args) throws Exception {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -