java t.page 上的 Freemarker utf-8 编码问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14938644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:56:33  来源:igfitidea点击:

Freemarker utf-8 encoding problems on t.page

javahtmlspringutf-8freemarker

提问by claudioivp

I'm having problems with inside pages. It simply are recognizing pages as iso, but I want utf-8, I'm declaring it as default charset. I tried some modifications on freemarker configuration, but they are not having effect.

我的内页有问题。它只是将页面识别为iso,但我想要utf-8,我将其声明为默认字符集。我尝试了对 freemarker 配置的一些修改,但它们没有效果。

spring-servlet.xml

spring-servlet.xml

<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <property name="templateLoaderPath" value="/WEB-INF/pages/"/>
</bean>

template.html

模板.html

<#macro page>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Cemitério - Prefeitura Municipal de Maringá</title>
</head>

<body>
Usuários
<#nested/>
</body>
</html>
</#macro>

login.html

登录.html

<#import "templates/template.html" as t/>

<@t.page>

<#if erroLogin??>
    ${erroLogin}
</#if>
<form action="entrar" method="post">
    <div>
        <label>Usuário:</label>
        <input type="text" name="usuario" />
        <br />
        <label>Senha:</label>
        <input type="text" name="senha" />
        <br />
        <input type="submit" name="submit" />
    </div>
</form>

</@t.page>

output

输出

enter image description here

在此处输入图片说明

回答by ddekany

Since the accents were all right in the inserted variables, yet the accents entered directly into the templates weren't, and the browser seems to know that the page uses UTF-8 (that you can check in the page information dialog of the browser), either:

由于插入变量中的重音是正确的,而直接输入模板的重音却没有,浏览器似乎知道该页面使用UTF-8(您可以在浏览器的页面信息对话框中查看) , 任何一个:

  • The template file was saved with the wrong encoding. In Eclipse, you should go to Window -> Preferences -> Workspace, and set text file encoding to UTF-8. This is a global setting, but by default Eclipse uses the platform default, which doesn't make sense in 99% of the projects. You can also set this on project level under Project -> Properties -> Resource.

  • FreeMarker has used wrong charset to decode the template files, as it also uses the platform default by default. So you should set the default_encodingsetting to UTF-8. You can also force the encoding in the template with <#ftl encoding='UTF-8'>.

  • 模板文件以错误的编码保存。在 Eclipse 中,您应该转到 Window -> Preferences -> Workspace,并将文本文件编码设置为 UTF-8。这是一个全局设置,但默认情况下 Eclipse 使用平台默认值,这在 99% 的项目中没有意义。您还可以在项目 -> 属性 -> 资源下在项目级别设置此项。

  • FreeMarker 使用了错误的字符集来解码模板文件,因为它默认也使用平台默认值。所以你应该将default_encoding设置设置为UTF-8. 您还可以使用<#ftl encoding='UTF-8'>.

回答by grepit

how about if you add this charset="UTF-8"

如果你添加这个 charset="UTF-8" 怎么样

<label charset="UTF-8" >Usuário:</label>

in HTML 5 you would add:

在 HTML 5 中,您将添加:

<meta charset="UTF-8">

in previous HTML (notice you have lower case in your code..maybe that might be contributing to it)

在以前的 HTML 中(注意你的代码中有小写......也许这可能是造成它的原因)

<meta http-equiv="Content-type" content="text/html;charset=UTF-8">

回答by claudioivp

I found the solution. I need to create the login.html file again, using dreamweaver, then save as html and paste the file on the eclipse project.

我找到了解决方案。我需要使用 Dreamweaver 再次创建 login.html 文件,然后另存为 html 并将该文件粘贴到 eclipse 项目中。

回答by Jasper de Vries

In some cases you might want to encode entities. If you are working with a Java object as the data (context) for your template you could add a method there to encode entities:

在某些情况下,您可能希望对实体进行编码。如果您使用 Java 对象作为模板的数据(上下文),您可以在那里添加一个方法来编码实体:

public String htmlEntities(String input)
{
    StringBuilder sb = new StringBuilder(input.length());
    for (char c : input.toCharArray()) {
        if (c == '"') {
            sb.append("&quot;");
        }
        else if (c == '<') {
            sb.append("&lt;");
        }
        else if (c == '>') {
            sb.append("&gt;");
        }
        else if (c < 128) {
            sb.append(c);
        }
        else {
            sb.append(String.format("&#x%04x;", (int) c));
        }
    }
    return sb.toString();
}

This can be used in your template like:

这可以在您的模板中使用,例如:

${htmlEntities('Usuário')}

The result will be:

结果将是:

Usu&#x00e1;rio