简体   繁体   English

如何在jsp中同时使用引号和反斜杠?

[英]How can I escape both quotation mark and backslash in jsp?

I need to escape " and \\ in jsp, but not necessarily in the same time. 我需要在jsp中转义“和\\,但不必同时进行。

With <c:out var="${value}" /> I can escape " character. 使用<c:out var="${value}" />可以转义"字符。

With ${fn:replace(value,'\\\\','&#92;')} I can escape the \\ character, and is working fine. 使用${fn:replace(value,'\\\\','&#92;')}我可以转义\\字符,并且工作正常。

I tried to handle both cases as follows. 我尝试按以下方式处理这两种情况。

I tried to use 我尝试使用

<c:out var="${fn:replace(value,'\\','&#92;')}"/>

but is not working, is not accepted, seems to be an error. 但不起作用,不被接受,似乎是一个错误。

I also tried to put in a variable the string after replace, and after that using it in c:out , but was the same thing. 我还尝试在替换后将字符串放入variable ,然后在c:out中使用它,但这是同一回事。

If anyone has an idea of a way to handle both cases, please let me know. 如果有人想办法处理这两种情况,请告诉我。

Note: the input comes from Java , that's why I used \\\\ , and is sent forward as JSON . 注意:输入来自Java ,这就是我使用\\\\的原因,并且将其作为JSON发送出去。 Thanks 谢谢

You could call fn:replace() twice, chaining the calls, to replace both characters. 您可以调用fn:replace()两次,将其链接在一起,以替换两个字符。

${fn:replace(fn:replace(value,'\\','&#92;'),'\"','&#34;')}

It's not very pretty, though. 不过不是很漂亮。

I found the best solution to be StringEscapeUtils from Apache , and I had to create a taglib as @Tap said, and added jar file commons-lang to project . 我找到了最好的解决方案是Apache StringEscapeUtils ,我不得不按照@Tap的说明创建一个taglib,并将jar文件commons-lang添加到project。 This has a lot of functions for working with strings. 这有很多处理字符串的功能。

More details can be found here -> 更多细节可以在这里找到->

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM