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; } } }
這裡需要說明以下,之前介紹的一些工具類還有以後將會介紹的工具類,裡面的一些方法在案例中並不一定會用到,還有一些方法體十分簡單,這樣做的目的也是為了使項目代碼更加簡單,便於維護。
ps:最近發現其他網站可能會對博客轉載,上面並沒有源鏈接,如想查看更多關於 基於lucene的案例開發 請點擊這裡。或訪問網址http://blog.csdn.net/xiaojimanman/article/category/2841877