Freemarker utf-8 encoding problems on t.page

荒凉一梦 提交于 2019-12-05 02:00:05

问题


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.

spring-servlet.xml

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

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

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


回答1:


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:

  • 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_encoding setting to UTF-8. You can also force the encoding in the template with <#ftl encoding='UTF-8'>.




回答2:


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

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

in HTML 5 you would add:

<meta charset="UTF-8">

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

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



回答3:


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.




回答4:


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:

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


来源:https://stackoverflow.com/questions/14938644/freemarker-utf-8-encoding-problems-on-t-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!