ParseUtil
ParseUtil类主要实现将字符串(数字)转化为数值,这个在读取配置文件或数据转化过程中有很大的作用。源程序如下:
/** *@Description: 转换类 */ package com.lulei.util; public class ParseUtil { /** * @param str * @param defaultDouble * @return double * @Author: lulei * @Description: 将字符串转化为double */ public static double parseStringToDouble(String str, double defaultDouble){ double re = defaultDouble; try{ re = Double.parseDouble(str); } catch (Exception e){ } return re; } /** * @param str * @param defaultInt * @return int * @Author: lulei * @Description: 将字符串转化为int */ public static int parseStringToInt(String str, int defaultInt){ int re = defaultInt; try{ re = Integer.parseInt(str); } catch (Exception e){ } return re; } /** * @param str * @param defaultLong * @return * @Author:lulei * @Description:将字符串转化为long */ public static long parseStringToLong(String str, long defaultLong) { long re = defaultLong; try{ re = Long.parseLong(str); } catch (Exception e){ } return re; } }
ParseRequest
ParseRequest类主要获取request参数值,并进行简单的数据转化。源程序如下:
/** *@Description: 获取request参数值 */ package com.lulei.util; import javax.servlet.http.HttpServletRequest; public class ParseRequest { /** * @param request * @param paramName * @param defaultStr * @return String * @Author: lulei * @Description: 获取参数值,返回字符串,去除前后多余的空格 */ public static String getString(HttpServletRequest request, String paramName, String defaultStr){ String param = request.getParameter(paramName); if (param == null){ return defaultStr; } try { return new String(param.getBytes("iso-8859-1"), "utf-8").trim(); } catch (Exception e) { e.printStackTrace(); return defaultStr; } } /** * @param request * @param paramName * @param defaultStr * @return * @Author: lulei * @Description: 获取参数值,返回字符串,参数采用gbk编码,去除前后多余的空格 */ public static String getStringGbk(HttpServletRequest request, String paramName, String defaultStr){ String param = request.getParameter(paramName); if (param == null){ return defaultStr; } try { return new String(param.getBytes("iso-8859-1"), "gbk").trim(); } catch (Exception e) { e.printStackTrace(); return defaultStr; } } /** * @param request * @param paramName * @param defaultInt * @return int * @Author: lulei * @Description: 获取参数值,返回int形整数 */ public static int getInt(HttpServletRequest request, String paramName, int defaultInt){ String param = request.getParameter(paramName); if (param == null){ return defaultInt; } try { int re = Integer.parseInt(param); return re; } catch (Exception e) { return defaultInt; } } /** * @param request * @param paramName * @param defaultlong * @return long * @Author: lulei * @Description: 获取参数值,返回long形数字 */ public static long getLong(HttpServletRequest request, String paramName, long defaultlong){ String param = request.getParameter(paramName); if (param == null){ return defaultlong; } try { long re = Long.parseLong(param); return re; } catch (Exception e) { return defaultlong; } } }
这里需要说明以下,之前介绍的一些工具类还有以后将会介绍的工具类,里面的一些方法在案例中并不一定会用到,还有一些方法体十分简单,这样做的目的也是为了使项目代码更加简单,便于维护。