code_junkie

How can I replace newline characters using JSP and JSTL?

Nov 10th, 2011
1,293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
  2. <% pageContext.setAttribute("newLineChar", "n"); %>
  3.  
  4. ${fn:replace(item.comments, newLineChar, "; ")}
  5.  
  6. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  7. <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
  8.  
  9. <c:set var="newline" value="n"/>
  10. ${fn:replace(data, newLine, "; ")}
  11.  
  12. <?xml version="1.0" encoding="UTF-8"?>
  13. <taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee"
  14. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  15. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd">
  16. <tlib-version>1.0</tlib-version>
  17. <short-name>sf</short-name>
  18. <uri>http://www.stackoverflow.com</uri>
  19. <function>
  20. <name>clean</name>
  21. <function-class>com.stackoverflow.web.tag.function.TagUtils</function-class>
  22. <function-signature>
  23. java.lang.String clean(java.lang.String)
  24. </function-signature>
  25. </function>
  26. </taglib>
  27.  
  28. package com.stackoverflow.web.tag.function;
  29.  
  30. import javax.servlet.jsp.tagext.TagSupport;
  31.  
  32. public class TagUtils extends TagSupport {
  33. public static String clean(String comment) {
  34. return comment.replaceAll("n", "; ");
  35. }
  36. }
  37.  
  38. <%@ taglib prefix="sf" uri="http://www.stackoverflow.com"%>
  39. ${sf:clean(item.comments)}
  40.  
  41. ${fn:replace(data, '\n', ';')}
  42.  
  43. ${fn:replace(data, '\n', ';')}
  44.  
  45. ${fn:replace(data, '\n', ';')}
  46.  
  47. <%@ taglib prefix="str" uri="http://jakarta.apache.org/taglibs/string-1.1" %>
  48. ...
  49. <str:replace var="result" replace="~n" with=";" newlineToken="~n">
  50. Text containing newlines
  51. </str:replace>
  52. ...
  53.  
  54. <%@ taglib prefix="ns" uri="..." %>
  55. ...
  56. ${ns:replace(data)}
  57.  
  58. <c:set var="newline" value="n"/>
  59. ${fn:replace(data, newLine, "; ")}
  60.  
  61. <% pageContext.setAttribute("newLineChar", "n"); %>
  62. ${fn:replace(item.comments, newLineChar, "; ")}
  63.  
  64. <% pageContext.setAttribute("newLineChar", "r"); %>
  65. <c:set var="textAreaDefault" value="${fn:replace(textAreaDefault, newLineChar, '')}" />
  66.  
  67. <str:replace var="your_Var_replaced" replace="n" with="Your ney caracter" newlineToken="n">${your_Var_to_replaced}</str:replace>
  68.  
  69. ${fn:replace(text, "
  70. ", "<br/>")}
  71.  
  72. <c:set var="nl" value="
  73. " /><%-- this is a new line --%>
  74.  
  75. <c:set var="newline" value="<%= "n" %>" />
  76. ${fn:replace(myAddress, newline, "<br />")}
  77.  
  78. <c:set var="newline" value="
  79. " /><!--this line can't be indented -->
  80. ${fn:replace(myAddress, newline, "<br />")}
Add Comment
Please, Sign In to add comment