简体   繁体   English

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

[英]Freemarker utf-8 encoding problems on t.page

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. 它只是将页面识别为iso,但是我想要utf-8,我将其声明为默认字符集。 I tried some modifications on freemarker configuration, but they are not having effect. 我尝试对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 template.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 login.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 输出

在此处输入图片说明

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. 在Eclipse中,您应该转到Window-> Preferences-> Workspace,并将文本文件编码设置为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. 这是一个全局设置,但是默认情况下Eclipse使用平台默认值,这在99%的项目中没有意义。 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. FreeMarker使用错误的字符集来解码模板文件,因为默认情况下它也使用平台默认值。 So you should set the default_encoding setting to UTF-8 . 因此,您应该将default_encoding设置设置为UTF-8 You can also force the encoding in the template with <#ftl encoding='UTF-8'> . 您还可以使用<#ftl encoding='UTF-8'>强制在模板中进行<#ftl encoding='UTF-8'>

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">

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项目中。

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

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

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