将包含换行符的Java变量转换为javascript变量 [英] Convert Java variable containing newline to javascript variable

查看:51
本文介绍了将包含换行符的Java变量转换为javascript变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将发送到JSP的服务器端的Java变量(包含换行符和其他特殊字符)转换为javascript变量.

I want to convert a java variable(containing newline and other special characters) from server side sent to JSP to javascript variable.

在我的JSP中,

var test = '${educationDescription}';
alert(test);

从服务器端发送的教育值是

Value of educationDescription sent from server side is

    This is
test
containing newline
and other special chars like " & ; etc

但是我收到上述代码段的javascript错误.

But I am getting javascript error for the above code snippet.

推荐答案

您可以执行以下操作.使用两个斜杠("\\n")

You can do something like this. Use two slashes ("\\n")

<%
String s = "This is a \\n test";
request.setAttribute("s", s);
%>

var s="${s}";
alert(s);

这篇关于将包含换行符的Java变量转换为javascript变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆