ssh開發常用工具類
阿新 • • 發佈:2019-02-11
常用的泛型工具類:
import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; /** * 泛型工具類 * */ public class GenericsUtils { /** * 通過反射,獲得指定類的父類的泛型引數的實際型別. 如BuyerServiceBean extends DaoSupport<Buyer> * * @param clazz clazz 需要反射的類,該類必須繼承範型父類 * @param index 泛型引數所在索引,從0開始. * @return 範型引數的實際型別, 如果沒有實現ParameterizedType介面,即不支援泛型,所以直接返回<code>Object.class</code> */ public static Class getClassGenricType(Class clazz, int index) { Type genType = clazz.getGenericInterfaces()[0];//得到自己 //如果沒有實現ParameterizedType介面,即不支援泛型,直接返回Object.class if (!(genType instanceof ParameterizedType)) { return Object.class; } //返回表示此型別實際型別引數的Type物件的陣列,數組裡放的都是對應型別的Class, 如BuyerServiceBean extends DaoSupport<Buyer,Contact>就返回Buyer和Contact型別 Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); if (index >= params.length || index < 0) { throw new RuntimeException("你輸入的索引"+ (index<0 ? "不能小於0" : "超出了引數的總數")); } if (!(params[index] instanceof Class)) { return Object.class; } return (Class) params[index]; } /** * 通過反射,獲得指定類的父類的第一個泛型引數的實際型別. 如BuyerServiceBean extends DaoSupport<Buyer> * * @param clazz clazz 需要反射的類,該類必須繼承泛型父類 * @return 泛型引數的實際型別, 如果沒有實現ParameterizedType介面,即不支援泛型,所以直接返回<code>Object.class</code> */ public static Class getClassGenricType(Class clazz) { return getClassGenricType(clazz,0); } /** * 通過反射,獲得指定類的父類的泛型引數的實際型別. 如BuyerServiceBean extends DaoSupport<Buyer> * * @param clazz clazz 需要反射的類,該類必須繼承範型父類 * @param index 泛型引數所在索引,從0開始. * @return 範型引數的實際型別, 如果沒有實現ParameterizedType介面,即不支援泛型,所以直接返回<code>Object.class</code> */ public static Class getSuperClassGenricType(Class clazz, int index) { Type genType = clazz.getGenericSuperclass();//得到泛型父類 //如果沒有實現ParameterizedType介面,即不支援泛型,直接返回Object.class if (!(genType instanceof ParameterizedType)) { return Object.class; } //返回表示此型別實際型別引數的Type物件的陣列,數組裡放的都是對應型別的Class, 如BuyerServiceBean extends DaoSupport<Buyer,Contact>就返回Buyer和Contact型別 Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); if (index >= params.length || index < 0) { throw new RuntimeException("你輸入的索引"+ (index<0 ? "不能小於0" : "超出了引數的總數")); } if (!(params[index] instanceof Class)) { return Object.class; } return (Class) params[index]; } /** * 通過反射,獲得指定類的父類的第一個泛型引數的實際型別. 如BuyerServiceBean extends DaoSupport<Buyer> * * @param clazz clazz 需要反射的類,該類必須繼承泛型父類 * @return 泛型引數的實際型別, 如果沒有實現ParameterizedType介面,即不支援泛型,所以直接返回<code>Object.class</code> */ @SuppressWarnings("unchecked") public static Class getSuperClassGenricType(Class clazz) { return getSuperClassGenricType(clazz,0); } /** * 通過反射,獲得方法返回值泛型引數的實際型別. 如: public Map<String, Buyer> getNames(){} * * @param Method method 方法 * @param int index 泛型引數所在索引,從0開始. * @return 泛型引數的實際型別, 如果沒有實現ParameterizedType介面,即不支援泛型,所以直接返回<code>Object.class</code> */ public static Class getMethodGenericReturnType(Method method, int index) { Type returnType = method.getGenericReturnType(); if(returnType instanceof ParameterizedType){ ParameterizedType type = (ParameterizedType) returnType; Type[] typeArguments = type.getActualTypeArguments(); if (index >= typeArguments.length || index < 0) { throw new RuntimeException("你輸入的索引"+ (index<0 ? "不能小於0" : "超出了引數的總數")); } return (Class)typeArguments[index]; } return Object.class; } /** * 通過反射,獲得方法返回值第一個泛型引數的實際型別. 如: public Map<String, Buyer> getNames(){} * * @param Method method 方法 * @return 泛型引數的實際型別, 如果沒有實現ParameterizedType介面,即不支援泛型,所以直接返回<code>Object.class</code> */ @SuppressWarnings("unchecked") public static Class getMethodGenericReturnType(Method method) { return getMethodGenericReturnType(method, 0); } /** * 通過反射,獲得方法輸入引數第index個輸入引數的所有泛型引數的實際型別. 如: public void add(Map<String, Buyer> maps, List<String> names){} * * @param Method method 方法 * @param int index 第幾個輸入引數 * @return 輸入引數的泛型引數的實際型別集合, 如果沒有實現ParameterizedType介面,即不支援泛型,所以直接返回空集合 */ public static List<Class> getMethodGenericParameterTypes(Method method, int index) { List<Class> results = new ArrayList<Class>(); Type[] genericParameterTypes = method.getGenericParameterTypes(); if (index >= genericParameterTypes.length ||index < 0) { throw new RuntimeException("你輸入的索引"+ (index<0 ? "不能小於0" : "超出了引數的總數")); } Type genericParameterType = genericParameterTypes[index]; if(genericParameterType instanceof ParameterizedType){ ParameterizedType aType = (ParameterizedType) genericParameterType; Type[] parameterArgTypes = aType.getActualTypeArguments(); for(Type parameterArgType : parameterArgTypes){ Class parameterArgClass = (Class) parameterArgType; results.add(parameterArgClass); } return results; } return results; } /** * 通過反射,獲得方法輸入引數第一個輸入引數的所有泛型引數的實際型別. 如: public void add(Map<String, Buyer> maps, List<String> names){} * * @param Method method 方法 * @return 輸入引數的泛型引數的實際型別集合, 如果沒有實現ParameterizedType介面,即不支援泛型,所以直接返回空集合 */ @SuppressWarnings("unchecked") public static List<Class> getMethodGenericParameterTypes(Method method) { return getMethodGenericParameterTypes(method, 0); } /** * 通過反射,獲得Field泛型引數的實際型別. 如: public Map<String, Buyer> names; * * @param Field field 欄位 * @param int index 泛型引數所在索引,從0開始. * @return 泛型引數的實際型別, 如果沒有實現ParameterizedType介面,即不支援泛型,所以直接返回<code>Object.class</code> */ public static Class getFieldGenericType(Field field, int index) { Type genericFieldType = field.getGenericType(); if(genericFieldType instanceof ParameterizedType){ ParameterizedType aType = (ParameterizedType) genericFieldType; Type[] fieldArgTypes = aType.getActualTypeArguments(); if (index >= fieldArgTypes.length || index < 0) { throw new RuntimeException("你輸入的索引"+ (index<0 ? "不能小於0" : "超出了引數的總數")); } return (Class)fieldArgTypes[index]; } return Object.class; } /** * 通過反射,獲得Field泛型引數的實際型別. 如: public Map<String, Buyer> names; * * @param Field field 欄位 * @param int index 泛型引數所在索引,從0開始. * @return 泛型引數的實際型別, 如果沒有實現ParameterizedType介面,即不支援泛型,所以直接返回<code>Object.class</code> */ public static Class getFieldGenericType(Field field) { return getFieldGenericType(field, 0); } }
struts2工具類:
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; public class Struts2Utils { public static HttpSession getSession() { return ServletActionContext.getRequest().getSession(); } public static HttpSession getSession(Boolean b) { return ServletActionContext.getRequest().getSession(b.booleanValue()); } public static HttpServletRequest getRequest() { return ServletActionContext.getRequest(); } public static HttpServletResponse getResponse() { return ServletActionContext.getResponse(); } public static String getParameter(String name) { return getRequest().getParameter(name); } public static String[] getParameterValues(String name){ return getRequest().getParameterValues(name); } public static void setAttribute(String name,Object o) { ServletActionContext.getRequest().setAttribute(name, o); } public static Object getAttribute(String name) { return ServletActionContext.getRequest().getAttribute(name); } public static Object getContextAttribute(String name) { return ServletActionContext.getServletContext().getAttribute(name); } public static void setContextAttribute(String name,Object object) { ServletActionContext.getServletContext().setAttribute(name,object); } }
Web工具類:
package org.j2cms.utils; import java.util.HashMap; import java.util.Map; import java.util.regex.Pattern; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.j2cms.model.user.User; /** * * @author Administrator * */ public class WebUtil { /** * 獲取登入使用者 */ public static User getUser(HttpServletRequest request){ return (User) request.getSession().getAttribute("user"); } /*** * 獲取URI的路徑,如路徑為http://www.j2cms.com/action/post.htm?method=add, 得到的值為"/action/post.htm" * @param request * @return */ public static String getRequestURI(HttpServletRequest request){ return request.getRequestURI(); } /** * 獲取完整請求路徑(含內容路徑及請求引數) * @param request * @return */ public static String getRequestURIWithParam(HttpServletRequest request){ return getRequestURI(request) + (request.getQueryString() == null ? "" : "?"+ request.getQueryString()); } /** * 新增cookie * @param response * @param name cookie的名稱 * @param value cookie的值 * @param maxAge cookie存放的時間(以秒為單位,假如存放三天,即3*24*60*60; 如果值為0,cookie將隨瀏覽器關閉而清除) */ public static void addCookie(HttpServletResponse response, String name, String value, int maxAge) { Cookie cookie = new Cookie(name, value); cookie.setPath("/"); if (maxAge>0) cookie.setMaxAge(maxAge); response.addCookie(cookie); } /** * 獲取cookie的值 * @param request * @param name cookie的名稱 * @return */ public static String getCookieByName(HttpServletRequest request, String name) { Map<String, Cookie> cookieMap = WebUtil.readCookieMap(request); if(cookieMap.containsKey(name)){ Cookie cookie = (Cookie)cookieMap.get(name); return cookie.getValue(); }else{ return null; } } protected static Map<String, Cookie> readCookieMap(HttpServletRequest request) { Map<String, Cookie> cookieMap = new HashMap<String, Cookie>(); Cookie[] cookies = request.getCookies(); if (null != cookies) { for (int i = 0; i < cookies.length; i++) { cookieMap.put(cookies[i].getName(), cookies[i]); } } return cookieMap; } /** * 去除html程式碼 * @param inputString * @return */ public static String HtmltoText(String inputString) { String htmlStr = inputString; //含html標籤的字串 String textStr =""; java.util.regex.Pattern p_script; java.util.regex.Matcher m_script; java.util.regex.Pattern p_style; java.util.regex.Matcher m_style; java.util.regex.Pattern p_html; java.util.regex.Matcher m_html; java.util.regex.Pattern p_ba; java.util.regex.Matcher m_ba; try { String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; //定義script的正則表示式{或<script[^>]*?>[\\s\\S]*?<\\/script> } String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; //定義style的正則表示式{或<style[^>]*?>[\\s\\S]*?<\\/style> } String regEx_html = "<[^>]+>"; //定義HTML標籤的正則表示式 String patternStr = "\\s+"; p_script = Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE); m_script = p_script.matcher(htmlStr); htmlStr = m_script.replaceAll(""); //過濾script標籤 p_style = Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE); m_style = p_style.matcher(htmlStr); htmlStr = m_style.replaceAll(""); //過濾style標籤 p_html = Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE); m_html = p_html.matcher(htmlStr); htmlStr = m_html.replaceAll(""); //過濾html標籤 p_ba = Pattern.compile(patternStr,Pattern.CASE_INSENSITIVE); m_ba = p_ba.matcher(htmlStr); htmlStr = m_ba.replaceAll(""); //過濾空格 textStr = htmlStr; }catch(Exception e) { System.err.println("Html2Text: " + e.getMessage()); } return textStr;//返回文字字串 } /*********************************************************** * 函式名:strToInt 作 用:把字串轉為整型 參 數:s: 字串型 返回值:整型 ***********************************************************/ public static int StrToInt(String s) { try { int i = Integer.parseInt(checkReplace(s)); return i; // 返回轉化以後的字串 } catch (Exception e) { return 0; } } /********************************************************* * 函式名:checkReplace 作 用:轉化SQL特殊字串 參 數:s: 字串型,待轉化的字元 返回值:轉化以後的字串 * 調 用:String s2 = checkReplace(s1); ***********************************************************/ public static String checkReplace(String s) { try { if (s == null || s.equals("")) return ""; else { StringBuffer stringbuffer = new StringBuffer(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); switch (c) { case 34: // '"' stringbuffer.append("""); break; case 39: // '\'' stringbuffer.append("'"); break; case 124: // '|' stringbuffer.append(""); break; case '&': stringbuffer.append("&"); break; case '<': stringbuffer.append("<"); break; case '>': stringbuffer.append(">"); break; default: stringbuffer.append(c); break; } } return stringbuffer.toString().trim(); // 返回轉化以後的字串 } } catch (Exception e) { return ""; } } }
struts2 與fckeditor衝突
1,自定義filter
public class MyStrutsFilterDispatcher extends StrutsPrepareAndExecuteFilter { /** *struts2的攔截器與fck上傳檔案衝突 *重寫struts2的攔截器,增加了判斷,如果是fck的編輯器上傳檔案,則不進行攔截。 * 判斷路徑是否有fckeditor,實現在fck下的操作不進行攔截 */ @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { String url = ((HttpServletRequest)req).getRequestURI(); if (url.indexOf("fckeditor") < 0) { super.doFilter(req, res, chain); } else { chain.doFilter(req, res); } } }2. web.xml中這麼配置:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.j2cms.web.filter.MyStrutsFilterDispatcher</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>**.**.**.action</param-value>
</init-param>
</filter>