天天看點

java html 轉義工具類_分享springframework的實作JavaScriptUtils工具類後端JAVA代碼和前端HTML反斜杠特殊字元轉換處理工具類...

一、前言

關于spring-web(4.1.4)的org.springframework.web.util.JavaScriptUtils工具類對于轉義特殊字元顯示到前端javascript代碼,需要進行反斜杠\等處理,工具類具體代碼如下所示

二、源碼說明package org.springframework.web.util;@[email protected]@[email protected] class [email protected]@{@[email protected]  public static String javaScriptEscape(String input)@[email protected]  {@[email protected]    if (input == null) {@[email protected]      return input;@[email protected]    }@[email protected]@[email protected]    StringBuilder filtered = new StringBuilder(input.length());@[email protected]    char prevChar = ';@[email protected]@[email protected]    for (int i = 0; i ') {@[email protected]        filtered.append("\\u003E");@[email protected]      }@[email protected]      else if (c == 8232) {@[email protected]        filtered.append("\\u2028");@[email protected]      }@[email protected]      else if (c == 8233) {@[email protected]        filtered.append("\\u2029");@[email protected]      }@[email protected]      [email protected]@        filtered.append(c);@[email protected]@[email protected]      prevChar = c;@[email protected]    }@[email protected]@[email protected]    return filtered.toString();@[email protected]  }@[email protected]}