瓷砖编码问题
我正在尝试对正在开发的 Spring 应用程序使用 UTF-8 编码,但在从图块插入属性时无法获得正确的编码。
我的 JSP 模板中有这个片段:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title><tiles:getAsString name="title" /></title>
</head>
<body>
<tiles:insertAttribute name="header" ignore="true" />
....
在我的tile XML 配置文件中,我有类似的内容:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
"http://tiles.apache.org/dtds/tiles-config_2_1.dtd">
<tiles-definitions>
<definition name="tiles:base" template="/WEB-INF/views/templates/main.jsp">
<put-attribute name="title" value="Título" />
...
我在 eclipse 中检查了该文件是否具有 UTF-8 编码。 尽管 JSP 的其余部分是正确的(例如插入标题中的 JSP 片段),但标题属性中传递的单词在页面中未正确显示(重音字符以错误的方式显示)。如果我将编码更改为 ISO-8859-1,标题就可以,但页面的其余部分是错误的。我似乎无法将图块文件中的编码更改为 UTF-8。我还在我创建的文件中查找了“ISO-8859-1”,但没有在任何文件中设置此配置。
谁能告诉我如何为图块设置正确的编码?
谢谢
I'm trying to use UTF-8 encoding for the Spring application I'm developing but I have problems in getting the correct encoding when inserting attributes from tiles.
I have this fragment in my JSP template:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title><tiles:getAsString name="title" /></title>
</head>
<body>
<tiles:insertAttribute name="header" ignore="true" />
....
and in my tiles XML config file I have something like:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
"http://tiles.apache.org/dtds/tiles-config_2_1.dtd">
<tiles-definitions>
<definition name="tiles:base" template="/WEB-INF/views/templates/main.jsp">
<put-attribute name="title" value="Título" />
...
I have check in eclipse that this files have UTF-8 encoding.
The word passed in title attribute is not shown correctly (the accented characters are shown in a wrong way) in the page though the rest of the JSP is correct (for instance the JSP fragment which is inserted in header). If I change the encoding to ISO-8859-1 the title is OK, but the rest odf the page is wrong. It seems I cannot get to change the encoding to UTF-8 in my tiles file. I have also looked for "ISO-8859-1" in the files I've created and I haven't set up this configuration in any file.
Can anyone tell me how can I set up the correct encoding for tiles?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将以下内容添加到
web.xml
中。这与在每个 JSP 文件中添加标头具有相同的效果。网络.xml:
Add the following to
web.xml
. This has the same effect as adding the header in each JSP file.web.xml:
这是字符集的问题,而不是编码的问题。我必须
在每个 JSP 中进行设置并且它有效。我不知道是否有更简单的方法在 Spring Web 应用程序的所有 JSP 中配置它。
It was a problem with the charset, not with the encoding. I had to set
in every JSP and it worked. I don't know if there is an easier way of configure this in all the JSPs of a Spring Web application.
另一种方法是使用 ReloadableResourceBundleMessageSource(带有属性 defaultEncoding="UTF-8")也用于从图块插入的内容。
我的意思是您可以从tiles传递一个关键字,并使用它从资源包中输出所需的内容,如下所示:
Another way could be a usage of ReloadableResourceBundleMessageSource (with property defaultEncoding="UTF-8") also for the content being inserted from tiles.
I meant you can pass a keyword from tiles, and use it to output needed content from resource bundle, like this:
在我的 Struts 2.3 到 2.5 迁移过程中,我遇到了类似的问题:
JSP 引用的所有 javascript .JS 文件的内容类型(在响应标头中)现在为“application/javascript;charset=ISO-8859-1”(struts 2.5)而不是 charset=UTF-8(在 struts 2.3 中)。
对于引用 js 文件的 JSP 和脚本标记,字符集属性设置为 utf-8。
我添加了 Leonel 的代码,它终于起作用了:
但编码现在是“text/html;charset=UTF-8”。所以我丢失了应用程序/javascript。它不能正常工作。
所以我尝试了其他方法:
https://www.baeldung.com/tomcat-utf-8
这样我就得到了正确的字符集和内容类型。
让我们定义一个名为 CharacterSetFilter 的类:
我们需要将过滤器添加到应用程序的 web.xml 中,以便将其应用于所有请求和响应:
During my Struts 2.3 to 2.5 migration i encountered a similar problem :
content-type (in response header) of all javascript .JS files referenced by JSP were now "application/javascript;charset=ISO-8859-1" (struts 2.5) instead of charset=UTF-8 (in struts 2.3).
Charset attribute was set to utf-8 for JSP and script markup referencing the js file.
I added the code from Leonel and it finally worked:
but encoding is now "text/html;charset=UTF-8". So i've lost the application/javascript. it did'nt work properly.
So i tried something else :
https://www.baeldung.com/tomcat-utf-8
With this i get the correct charset and content-type.
Let’s define a class named CharacterSetFilter:
We need to add the filter to our application’s web.xml so that it’s applied to all requests and responses: