Velocity 模板似乎无法使用 UTF-8

发布于 2024-10-19 19:58:38 字数 598 浏览 1 评论 0原文

我一直在尝试使用包含以下内容的速度模板:

Sübjäct $item

除了两个 Unicode 字符的翻译之外,一切正常。命令行上打印的结果字符串如下所示:

Sübjäct foo

我在速度网站和网络上搜索了这个问题,并提出了不同的字体编码选项,并将其添加到我的代码中。但这些没有帮助。这是实际的代码:

velocity.setProperty("file.resource.loader.path", absPath);
velocity.setProperty("input.encoding", "UTF-8");
velocity.setProperty("output.encoding", "UTF-8");

Template t = velocity.getTemplate("subject.vm");
t.setEncoding("UTF-8");

StringWriter sw = new StringWriter();

t.merge(null, sw);       
System.out.println(sw.getBuffer());

我如何解决这个问题?

I have been trying to use a velocity Template with the following content:

Sübjäct $item

Everything works fine except the translation of the two Unicode characters. The result string printed on the command line looks like:

Sübjäct foo

I searched the velocity website and the web for this issue, and came up with different font encoding options, which I added to my code. But these don't help. This is the actual code:

velocity.setProperty("file.resource.loader.path", absPath);
velocity.setProperty("input.encoding", "UTF-8");
velocity.setProperty("output.encoding", "UTF-8");

Template t = velocity.getTemplate("subject.vm");
t.setEncoding("UTF-8");

StringWriter sw = new StringWriter();

t.merge(null, sw);       
System.out.println(sw.getBuffer());

How an I fix this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

疧_╮線 2024-10-26 19:58:38

您尝试过使用这种语法吗?

Template template = Velocity.getTemplate("subject.vm", "UTF-8");

看起来它应该做正确的事情。

Have you tried using this syntax?

Template template = Velocity.getTemplate("subject.vm", "UTF-8");

That looks like it should do the right thing.

南渊 2024-10-26 19:58:38

如果您将 VelocityEngine 与 JavaMailSenderImpl 类一起使用,请不要忘记设置 defaultEncoding 属性。另外,如上所述,尝试为VelocityEngine类配置input.encoding和output.encoding属性。我在下面留下一个例子。

配置文件

<bean id="smtpSession" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:jboss/example/jndiName"/>
    </bean>
<bean id="javaMailSenderImpl" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="session" ref="smtpSession"/>
        <property name="defaultEncoding" value="UTF-8"/>
</bean>

<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
        <property name="velocityProperties">
            <props>
                <prop key="input.encoding">UTF-8</prop>
                <prop key="output.encoding">UTF-8</prop>
                <prop key="response.encoding">UTF-8</prop>
                <prop key="resource.loader">file</prop>
                <prop key="file.resource.loader.class">org.apache.velocity.runtime.resource.loader.FileResourceLoader
                </prop>
                <prop key="file.resource.loader.path">${relative.path}/email-templates</prop>
                <prop key="file.resource.loader.cache">false</prop>                    
            </props>
        </property>
    </bean>

If you're using VelocityEngine along with JavaMailSenderImpl class, don't forget to set the defaultEncoding property. Also, as mentioned above, try configuring input.encoding and output.encoding properties for the VelocityEngine class. I leave an example below.

Configuration file

<bean id="smtpSession" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:jboss/example/jndiName"/>
    </bean>
<bean id="javaMailSenderImpl" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="session" ref="smtpSession"/>
        <property name="defaultEncoding" value="UTF-8"/>
</bean>

<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
        <property name="velocityProperties">
            <props>
                <prop key="input.encoding">UTF-8</prop>
                <prop key="output.encoding">UTF-8</prop>
                <prop key="response.encoding">UTF-8</prop>
                <prop key="resource.loader">file</prop>
                <prop key="file.resource.loader.class">org.apache.velocity.runtime.resource.loader.FileResourceLoader
                </prop>
                <prop key="file.resource.loader.path">${relative.path}/email-templates</prop>
                <prop key="file.resource.loader.cache">false</prop>                    
            </props>
        </property>
    </bean>
暮倦 2024-10-26 19:58:38

我的解决方案:将“-Dfile.encoding=UTF-8”添加到jvm选项(不包括引号)。

我尝试了上述可能的解决方案,但没有一个对我有用。

经过几天的扭曲搜索和探索,我猜我的问题发生在velocitie渲染html文件上,因为我发现一些错误显示的文本实际上是GB2312编码的,我意识到当页面打开时vm文件的编码不正确渲染(我猜)。

My solution: add "-Dfile.encoding=UTF-8" to jvm option(quotes not included).

I try the above possible solution, none of then works for me.

After days of twisted search and explore, I guess my problem happen on velocitie's rendering the html file, because I found some wrongly displayed text is actually in GB2312 encoding, I realised that the encoding of the vm file is not correct when the page is been rendering(I guess).

对你再特殊 2024-10-26 19:58:38

如果您手动创建 JavaMailSenderImpl 实例,则可以通过以下方式设置编码属性:

JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
Properties mailProperties = new Properties();
mailProperties.put("defaultEncoding", "UTF-8");
mailSender.setJavaMailProperties(mailProperties);

如果您使用 MimeMessageHelper,请按如下方式设置编码:

MimeMessageHelper helper = new MimeMessageHelper(mailSender.createMimeMessage(), true, "UTF-8");

最后,对于 Velocity引擎,设置编码时有两种方式获取/应用模板:

Template template = Velocity.getTemplate("template", "UTF-8");
// or
velocityEngine.mergeTemplate("template", "UTF-8", velocityContext, stringWriter);

If you're creating a JavaMailSenderImpl instance manually, you can set the encoding property this way:

JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
Properties mailProperties = new Properties();
mailProperties.put("defaultEncoding", "UTF-8");
mailSender.setJavaMailProperties(mailProperties);

If you're using MimeMessageHelper, set the encoding as follows:

MimeMessageHelper helper = new MimeMessageHelper(mailSender.createMimeMessage(), true, "UTF-8");

Finally, for Velocity Engine, there are two ways to get/apply the template while setting the encoding:

Template template = Velocity.getTemplate("template", "UTF-8");
// or
velocityEngine.mergeTemplate("template", "UTF-8", velocityContext, stringWriter);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文