FreeMarker中文乱码
程序员文章站
2022-03-18 20:43:00
...
共有三处地方需要注意:
FreeMarkerConfigurer
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/views"/>
<property name="freemarkerSettings">
<props>
<!-- 设置页面中文乱码问题 -->
<prop key="default_encoding">UTF-8</prop><!--模板的编码格式 -->
<prop key="locale">UTF-8</prop><!-- 本地化设置 -->
<prop key="url_escaping_charset">UTF-8</prop>
<!-- 其他配置 -->
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="time_format">HH:mm:ss</prop>
<prop key="number_format">0.####</prop>
<prop key="boolean_format">true,false</prop>
<prop key="whitespace_stripping">true</prop>
<prop key="tag_syntax">auto_detect</prop>
</props>
</property>
<property name="templateLoaderPath">
<value>WEB-INF/FTL/</value>
</property>
</bean>
FreeMarkerViewResolver
<bean id="freemarkerResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<!-- 设置页面中文乱码问题 -->
<property name="contentType" value="text/html;charset=UTF-8"/>
<!-- 其他配置 -->
<property name="cache" value="true" />
<property name="prefix" value="" />
<property name="suffix" value=".ftl" />
<property name="requestContextAttribute" value="request" />
<property name="exposeSpringMacroHelpers" value="true" />
<property name="exposeRequestAttributes" value="true" />
<property name="exposeSessionAttributes" value="true" />
</bean>
ftl文件编码
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
hello你好${name}
</body>
</html>
注意这一句话:<meta http-equiv="Content-Type" content="text/html; charset=utf-8">