1. 程式人生 > >jstl fn:replace替換換行符

jstl fn:replace替換換行符

轉自:https://blog.csdn.net/goddessming/article/details/51678928

近日在使用textarea時,輸入了回車,為了將textarea的內容作為一行顯示,,需要採用fn:replace函式進行處理

但是測試很多次都不可以,

1. 使用 ${fn: replace(source,'\n','')}

     這樣會提示錯誤,如果換成\\n不會進行替換,

2. 使用${fn:replace(source,'\\\\n','')}

一樣無效

3. 經過谷歌發現了一個方法:

<%
    request.setAttribute("vEnter", "\n");   
    //奇怪的是這一行我用 <c:set var="vEnter" value="\n" scope="request"/>
    //都沒用,估計是set標籤裡對value值處理了一下,沒jstl的原始碼,不清楚JSTL是怎麼處理的. 
%> 
      <td nowrap="false">${fn:replace(oneResult.answer,vEnter,"<br>")}</td>

轉自:http://blog.163.com/chenjie_8392/blog/static/439339842010513128139/

個人在jsp中的使用:

<%
    request.setAttribute("vEnter", "\n");   
%>

var researchResults =  "${fn:replace(obj.textAttribute,vEnter,"")}"; //詳細描述

jsp中如果不將換行符替換為空格,程式碼中會出現換行,導致javascript語法錯誤。

 

備註:

函式描述

  • fn:contains(string, substring) 如果引數string中包含引數substring,返回true
  • fn:containsIgnoreCase(string, substring) 如果引數string中包含引數substring(忽略大小寫),返回true
  • fn:endsWith(string, suffix) 如果引數 string 以引數suffix結尾,返回true
  • fn:escapeXml(string) 將有特殊意義的XML (和HTML)轉換為對應的XML character entity code,並返回
  • fn:indexOf(string, substring) 返回引數substring在引數string中第一次出現的位置
  • fn:join(array, separator) 將一個給定的陣列array用給定的間隔符separator串在一起,組成一個新的字串並返回。
  • fn:length(item) 返回引數item中包含元素的數量。引數Item型別是陣列、collection或者String。如果是String型別,返回值是String中的 字元數。
  • fn:replace(string, before, after) 返回一個String物件。用引數after字串替換引數string中所有出現引數before字串的地方,並返回替換後的結果
  • fn:split(string, separator) 返回一個數組,以引數separator 為分割符分割引數string,分割後的每一部分就是陣列的一個元素
  • fn:startsWith(string, prefix) 如果引數string以引數prefix開頭,返回true
  • fn:substring(string, begin, end) 返回引數string部分字串, 從引數begin開始到引數end位置,包括end位置的字元
  • fn:substringAfter(string, substring) 返回引數substring在引數string中後面的那一部分字串
  • fn:substringBefore(string, substring) 返回引數substring在引數string中前面的那一部分字串
  • fn:toLowerCase(string) 將引數string所有的字元變為小寫,並將其返回
  • fn:toUpperCase(string) 將引數string所有的字元變為大寫,並將其返回
  • fn:trim(string) 去除引數string 首尾的空格 ,並將其返回

   https://blog.csdn.net/szwangdf/article/details/27087215

   注:js引用java變數時要注意特殊字元的處理

  https://blog.csdn.net/oycn0755/article/details/2047207

以下是個人判斷欄位非空的程式碼,直接用長度會省去處理變數內容中特殊字元的麻煩

var cityName =  "${fn:length(roadFeedback.cityName)}";//城市資訊
if(cityName == "0"){
	alert("城市資訊不能為空!");
	return false;
}