I'm using form:hidden tag from spring forms to store some values in json

<form:hidden path="myJson"/>

This JSON values have this nature:

[{"html":"<a href=\"index.html\">text has &#39;single quotes&#39; or even \"double quotes\"</a>"}]

So the JSON has double quotes that are escaped with \" in case they are inside a string, and may have single quotes that are escaped as its HTML entity

&#39;

So I've been using an ordinary input type="hidden" like this one:

<input type="hidden" name="myJson" value='[{"html":"<a href=\"index.html\">text has &#39;single quotes&#39; or even \"double quotes\"</a>"}]'>

Notice value attribute uses single quotes. Now, using spring forms the output uses double quotes:

<input type="hidden" name="myJson" value="[{"html":"<a href=\"index.html\">text has &#39;single quotes&#39; or even \"double quotes\"</a>"}]">

So when I'm getting the value I only get "[{" and the rest is written in the body of the page.

Also, if I cannot use htmlEscape="false" like this:

<input:hidden path="myJson" htmlEscape="false"/>

Because I get all the value full of htmlEntities:

[{&quot;html;&quot...

Then the library that uses this JSON values reads them literally and instead of writting the link it writes the full HTML in the body.

Is there some way to change the output of input:hidden so it displays single quotes instead of double quotes?

Alternatively, is there a better way to achieve this? The requirement being that this JSON can hold HTML and single and double quotes.

Thanks!

有帮助吗?

解决方案

you can try to use javascript as follow:

html

<input type="hidden" id="example" value="">

js

var json = JSON.stringify([{"product_id":123,"name":"stack"}]);
document.getElementById('example').setAttribute('value', json.replace(/"/g, "&quot;"));
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top