woensdag 6 juni 2007

fn:replace carriage return with br

So I have a String that needs to be passed through as a javascript method argument. Problem is this String is user input and therefore can contain newlines so code can be scattered over multiple lines resulting in js syntax errors, example:


functionName("a correct string argument","this one is
scattered over multiple lines and therefore generates js syntax errors");


Easy, lets replace these newlines with the fn:replace tag... Didn't turn out to be so simple at all, "\\\r\\\n" is not accepted with jstl. Read the following threads for some suggestions:

http://forum.java.sun.com/thread.jspa?threadID=614154&messageID=3427211

http://mail-archives.apache.org/mod_mbox/jakarta-taglibs-user/200404.mbox/%3c20040413170630.M35388@pdragon.net%3e

Important! In case of just formatting (not syntax problem like me) you can always use <pre> + css

Eventually I just created an extra getter in my model returning the corrected string, if you don't know how to do this see:

http://www.rgagnon.com/javadetails/java-0454.html


When always running on the same system you can also use System.getProperty("line.separator") in your replace.


Geen opmerkingen:

Een reactie plaatsen