简体   繁体   English

Java UTF-8特殊字符转换为JavaScript

[英]Java UTF-8 special characters to JavaScript

I have a Java Web Application(Webwork) and I have some .jsps which show some data that can be special (UTF-8) characters. 我有一个Java Web应用程序(Webwork),并且有一些.jsps文件,它们显示一些可以是特殊(UTF-8)字符的数据。 Ex:Jérôme Serrano 例如:JérômeSerrano

The .jsps include the correct UTF-8 headers: .jsps包含正确的UTF-8标头:

  • in my jsp I include a decorator.jsp which has <%@ page pageEncoding="UTF-8" contentType="text/html;charset=utf-8" %> 在我的jsp中,我包含了一个decorator.jsp ,它具有<%@ page pageEncoding="UTF-8" contentType="text/html;charset=utf-8" %>
  • decorator.jsp in turn includes a meta.jsp which looks like: decorator.jsp依次包含一个meta.jsp ,如下所示:
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />

In .jsp I read my data like: 在.jsp中,我读取数据如下:

<ww:property value="%{content.author}" />

and it is correctly displayed: 并正确显示: 在此输入图像描述

I try to pass the string to JS, so that after the page is loaded I do something else(like call another service when the user clicks a button) 我尝试将字符串传递给JS,以便在页面加载后执行其他操作(例如,当用户单击按钮时调用另一个服务)

<script type="text/javascript" charset="utf-8">
var contentData = {};
contentData.author = "<ww:property value="%{content.author}" />";
alert(contentData.author);
</script>

BUT in JS the characters are not displayed correctly! 但是在JS中字符无法正确显示! 在此输入图像描述

So why when the .jsp is loaded the characters are rendered correctly, but once we pass them into JS they are broken? 那么,为什么在加载.jsp时可以正确呈现字符,但是一旦将它们传递给JS,它们就会坏了?

An external JavaScript with an UTF-8 character set: 具有UTF-8字符集的外部JavaScript:

 <script src="myscripts.js" charset="UTF-8"></script> 
or internal :D ! 或内部:D!

 <script charset="UTF-8"></script> 

Try to save your files using the option "Save with encoding" on your code editor. 尝试使用代码编辑器上的“使用编码保存”选项来保存文件。 I know that Sublime Text has this option (File -> Save with encoding). 我知道Sublime Text具有此选项(文件->使用编码保存)。

You can try different encodings. 您可以尝试不同的编码。 UTF-8 or ISO-8859-1. UTF-8或ISO-8859-1。

The idea is, you have to save the file using the same encoding which the headers (meta) is. 这个想法是,您必须使用与标头(元)相同的编码来保存文件。

After debugging and struggling with this whole night, here is my solution: - instead of writing values from Java directly into JavaScript variables like: 经过一整夜的调试和努力之后,这是我的解决方案:-而不是将Java中的值直接写入JavaScript变量中,例如:

<script charset="UTF-8">
var myName = "<ww:property value="name" />";
alert(">>" + myName);
</script>

in which case char encoding is not working properly. 在这种情况下,char编码无法正常工作。

I write Java values into hidden input . 我将Java值写入隐藏的输入中 This also has the benefits of automatically escaping chars like: '(apostrophe), " (quote) , etc... 这也具有自动转义字符的好处,例如:'(撇号),“(引号)等...

<input id="name_in" type="hidden" value="<ww:property value="name" />">
<script>
    var elem = $('#name_in').val();
    alert(">>"+elem);
</script>

In this case encoding works perfectly! 在这种情况下,编码效果很好! Also there is no need to JS escape chars like: ' and " 另外,也不需要JS转义字符,例如“和”

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

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