如何在 jsp 中传递一个大字符串变量而不进行任何更改?

How to pass a large string variable in jsp without any alteration?

我需要从后台获取一个数据,在前台通过JSP赋值给局部变量。来自后端的实际字符串数据如下:

"{\"deltas\":[{\"source\":{\"position\":63,\"lines\":[]},\"target\":{\"position\":65,\"lines\":[\"return {\"]},\"type\":\"INSERT\"},{\"source\":{\"position\":57,\"lines\":[\"//comment one\",\"//comment two\",\"//comment three\"]},\"target\":{\"position\":62,\"lines\":[]},\"type\":\"DELETE\"},{\"source\":{\"position\":53,\"lines\":[\"\"]},\"target\":{\"position\":56,\"lines\":[\"version.open(options,endpoint, false);\",\"version.send(payload);\",\"result \= version.responseE4X;\"]},\"type\":\"CHANGE\"},{\"source\":{\"position\":44,\"lines\":[]},\"target\":{\"position\":44,\"lines\":[\"            \\"\<sch:status\>\\"+status+\\"\</sch:status\>\\"+\",\"            \\"\<sch:workflowExternalRef\>\\"+refId+\\"\</sch:workflowExternalRef\>\\"+\",\"            \\"\<sch:description\>\\"+description+\\"\</sch:description\>\\" +\"]},\"type\":\"INSERT\"},{\"source\":{\"position\":42,\"lines\":[\"} else if(taskType\=\=\\"registration\\"){\"]},\"target\":{\"position\":42,\"lines\":[\"} else if(taskType\=\=\\"appRegistration\\"){\"]},\"type\":\"CHANGE\"},{\"source\":{\"position\":35,\"lines\":[\"} else if(taskType\=\=\\"app\\"){\"]},\"target\":{\"position\":35,\"lines\":[\"} else if(taskType\=\=\\"application\\"){\"]},\"type\":\"CHANGE\"},{\"source\":{\"position\":28,\"lines\":[\"} else if(taskType\=\=\\"signup\\"){\"]},\"target\":{\"position\":28,\"lines\":[\"} else if(taskType\=\=\\"user-signup\\"){\"]},\"type\":\"CHANGE\"},{\"source\":{\"position\":14,\"lines\":[\"                              { name : \\"\\", value :cookie },\"]},\"target\":{\"position\":14,\"lines\":[\"                              { name : \\"Cookie\\", value :cookie },\"]},\"type\":\"CHANGE\"},{\"source\":{\"position\":11,\"lines\":[\"var cookie\=session.get(\\"\\");\"]},\"target\":{\"position\":11,\"lines\":[\"var cookie\=session.get(\\"workflowCookie\\");\"]},\"type\":\"CHANGE\"}]}"

我正在使用 JSP 将其分配给如下局部变量:

var diffOutputList = '<c:out value="${diffResponse.diffOutputList}"/>'

在此之后,当我在控制台中打印 diffOutputList 时,打印如下:

{&#034;deltas&#034;:[{&#034;source&#034;:{&#034;position&#034;:63,&#034;lines&#034;:[]},&#034;target&#034;:{&#034;position&#034;:65,&#034;lines&#034;:[&#034;return {&#034;]},&#034;type&#034;:&#034;INSERT&#034;},{&#034;source&#034;:{&#034;position&#034;:57,&#034;lines&#034;:[&#034;//comment one&#034;,&#034;//comment two&#034;,&#034;//comment three&#034;]},&#034;target&#034;:{&#034;position&#034;:62,&#034;lines&#034;:[]},&#034;type&#034;:&#034;DELETE&#034;},{&#034;source&#034;:{&#034;position&#034;:53,&#034;lines&#034;:[&#034;&#034;]},&#034;target&#034;:{&#034;position&#034;:56,&#034;lines&#034;:[&#034;version.open(options,endpoint, false);&#034;,&#034;version.send(payload);&#034;,&#034;result = version.responseE4X;&#034;]},&#034;type&#034;:&#034;CHANGE&#034;},{&#034;source&#034;:{&#034;position&#034;:44,&#034;lines&#034;:[]},&#034;target&#034;:{&#034;position&#034;:44,&#034;lines&#034;:[&#034;            &#034;&lt;sch:status&gt;&#034;+status+&#034;&lt;/sch:status&gt;&#034;+&#034;,&#034;            &#034;&lt;sch:workflowExternalRef&gt;&#034;+refId+&#034;&lt;/sch:workflowExternalRef&gt;&#034;+&#034;,&#034;            &#034;&lt;sch:description&gt;&#034;+description+&#034;&lt;/sch:description&gt;&#034; +&#034;]},&#034;type&#034;:&#034;INSERT&#034;},{&#034;source&#034;:{&#034;position&#034;:42,&#034;lines&#034;:[&#034;} else if(taskType==&#034;registration&#034;){&#034;]},&#034;target&#034;:{&#034;position&#034;:42,&#034;lines&#034;:[&#034;} else if(taskType==&#034;appRegistration&#034;){&#034;]},&#034;type&#034;:&#034;CHANGE&#034;},{&#034;source&#034;:{&#034;position&#034;:35,&#034;lines&#034;:[&#034;} else if(taskType==&#034;app&#034;){&#034;]},&#034;target&#034;:{&#034;position&#034;:35,&#034;lines&#034;:[&#034;} else if(taskType==&#034;application&#034;){&#034;]},&#034;type&#034;:&#034;CHANGE&#034;},{&#034;source&#034;:{&#034;position&#034;:28,&#034;lines&#034;:[&#034;} else if(taskType==&#034;signup&#034;){&#034;]},&#034;target&#034;:{&#034;position&#034;:28,&#034;lines&#034;:[&#034;} else if(taskType==&#034;user-signup&#034;){&#034;]},&#034;type&#034;:&#034;CHANGE&#034;},{&#034;source&#034;:{&#034;position&#034;:14,&#034;lines&#034;:[&#034;                              { name : &#034;&#034;, value :cookie },&#034;]},&#034;target&#034;:{&#034;position&#034;:14,&#034;lines&#034;:[&#034;                              { name : &#034;Cookie&#034;, value :cookie },&#034;]},&#034;type&#034;:&#034;CHANGE&#034;},{&#034;source&#034;:{&#034;position&#034;:11,&#034;lines&#034;:[&#034;var cookie=session.get(&#034;&#034;);&#034;]},&#034;target&#034;:{&#034;position&#034;:11,&#034;lines&#034;:[&#034;var cookie=session.get(&#034;workflowCookie&#034;);&#034;]},&#034;type&#034;:&#034;CHANGE&#034;}]}

但我需要以下格式的字符串:

'{"deltas":[{"source":{"position":63,"lines":[]},"target":{"position":65,"lines":["return {"]},"type":"INSERT"},{"source":{"position":57,"lines":["//comment one","//comment two","//comment three"]},"target":{"position":62,"lines":[]},"type":"DELETE"},{"source":{"position":53,"lines":[""]},"target":{"position":56,"lines":["version.open(options,endpoint, false);","version.send(payload);","result = version.responseE4X;"]},"type":"CHANGE"},{"source":{"position":44,"lines":[]},"target":{"position":44,"lines":["            \"&lt;sch:status&gt;\"+status+\"&lt;/sch:status&gt;\"+","            \"&lt;sch:workflowExternalRef&gt;\"+refId+\"&lt;/sch:workflowExternalRef&gt;\"+","            \"&lt;sch:description&gt;\"+description+\"&lt;/sch:description&gt;\" +"]},"type":"INSERT"},{"source":{"position":42,"lines":["} else if(taskType==\"registration\"){"]},"target":{"position":42,"lines":["} else if(taskType==\"appRegistration\"){"]},"type":"CHANGE"},{"source":{"position":35,"lines":["} else if(taskType==\"app\"){"]},"target":{"position":35,"lines":["} else if(taskType==\"application\"){"]},"type":"CHANGE"},{"source":{"position":28,"lines":["} else if(taskType==\"signup\"){"]},"target":{"position":28,"lines":["} else if(taskType==\"user-signup\"){"]},"type":"CHANGE"},{"source":{"position":14,"lines":["                              { name : \"\", value :cookie },"]},"target":{"position":14,"lines":["                              { name : \"Cookie\", value :cookie },"]},"type":"CHANGE"},{"source":{"position":11,"lines":["var cookie=session.get(\"\");"]},"target":{"position":11,"lines":["var cookie=session.get(\"workflowCookie\");"]},"type":"CHANGE"}]}'

c:out 自动转义渲染中的 XML 标签。转义将 " 替换为 &#034;.

您可以使用 escapeXml 参数禁用转义:

var diffOutputList = '<c:out escapeXml="false" value="${diffResponse.diffOutputList}" />'

但在这种情况下,c:out 并不是必需的,您可以使用:

var diffOutputList = '${diffResponse.diffOutputList}'