本文介绍了t.page上的Freemarker utf-8编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



spring-servlet.xml

 < bean id =freemarkerConfigclass =org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer> 
< property name =templateLoaderPathvalue =/ WEB-INF / pages //>
< / bean>

template.html

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

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

login.html

 < #importtemplates / template.htmlas t /> 

< @ t.page>

< #if erroLogin ??>
$ {erroLogin}
< /#if>
< form action =entrarmethod =post>
< div>
< label>用户名:< / label>
< input type =textname =usuario/>
< br />
< label> Senha:< / label>
< input type =textname =senha/>
< br />
< input type =submitname =submit/>
< / div>
< / form>

< / @ t.page>

输出

解决方案

由于插入变量中的口音是完全正确的,但直接输入到模板中的口音却没有,并且浏览器似乎知道该页面使用UTF-8(您可以在浏览器的页面信息对话框中进行检查) ,要么:


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


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

  • / ul>

    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

    解决方案

    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'>.

    这篇关于t.page上的Freemarker utf-8编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 18:14