使用 Spring MVC 时如何在 Freemarker 视图中设置内容类型?
我正在使用带有 freemarker 视图的 Sping MVC。我设置了一个 FreeMarkerViewResolver 来解析视图,到目前为止它可以工作,但现在我遇到了编码问题。我所有的视图都是 UTF-8 编码的 HTML 5 页面,我还在 HTML 页面中添加了 但字符仍然以错误的编码打印。我用curl检查了HTTP标头,发现了:
k@jules:~$ curl -I http://localhost:8080/testweb/test.view
HTTP/1.1 200 OK
Content-Type: */*;charset=ISO-8859-1
但是当我请求一些不存在的资源(这会生成Tomcat错误)时,我得到了:
k@jules:~$ curl -I http://localhost:8080/testweb/nothere.html
HTTP/1.1 200 OK
Content-Type: text/html;charset=utf-8
所以Tomcat本身返回了正确的内容类型,但是Spring MVC Freemarker 视图没有。
对于 JSP,我可以在 JSP 标头中设置 Content-Type,但是在哪里可以为 freemarker 模板设置它?我想我必须在 Spring bean 配置中的某个地方执行此操作,但我找不到正确的位置。
I'm using Sping MVC with freemarker views. I set up a FreeMarkerViewResolver to resolve the views and it works so far but now I have encoding problems. All my views are HTML 5 pages in UTF-8 encoding and I also added a <meta charset="UTF-8" />
to the HTML page but characters are still printed in the wrong encoding. I checked the HTTP headers with curl and found this:
k@jules:~$ curl -I http://localhost:8080/testweb/test.view
HTTP/1.1 200 OK
Content-Type: */*;charset=ISO-8859-1
But when I request some non-existing resource (Which generates a Tomcat error) then I get this:
k@jules:~$ curl -I http://localhost:8080/testweb/nothere.html
HTTP/1.1 200 OK
Content-Type: text/html;charset=utf-8
So Tomcat itself returns the correct content-type but a Spring MVC Freemarker views don't.
For a JSP I can set the Content-Type in the JSP header but where can I set it for a freemarker template? I guess I have to do this somewhere in the Spring bean configuration but I can't find the right place.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
视图解析器(应该位于您的
dispatcher-servlet.xml
中)具有一个contentType
属性:The view resolver (should be in your
dispatcher-servlet.xml
) has acontentType
property for that:在使用 spring 框架和 freemarker 模板时,我还遇到了显示 UTF-8 字符(特殊字符,如 æ. ø 和 å 等)的问题。
我所做的是。
1.确保您的 .ftl 页面使用 utf-8 编码
确保这一点很重要,即使您设置了所有其他要求,未使用 UTF-8 字符集编码的页面也可能显示错误的数字。检查您的 IDE 设置,找出它为您的文件设置的默认编码。然而,我认为今天 Eclipse 和 NetBeans 将所有文件都设置为标准 UTF-8 编码。您必须确保它采用无 BOM 的 UTF-8 编码。
2.在模板文件中包含元标记以设置字符集
在包含
标记的模板 (.ftl) 文件中,设置
,其属性为
charset="UTF- 8”
。这是如果您使用 HTML 5。如果您使用 xhtml 或 HTML 4,您的元标记需要如下所示3.确保在部署描述符文件中设置字符编码过滤器
您必须通过字符编码过滤器过滤所有传入/传出请求。此过滤器在您的部署描述符(web.xml / 或 java 等效的 WebApplicationInitializer)中设置。
WebApplicationInitializer(Java 文件)
web.xml
4.在配置器和视图解析器中设置 FreeMarker 字符编码
您还需要使所有 FreeMarker 文件都使用 UTF-8 标准编码,这是通过在 FreeMarkerConfigurer 和 FreeMarkerViewResolver 中将其属性设置为 UTF-8 来完成的。这是在 Spring 应用程序上下文文件中设置的(我将只显示 Java 等效项,因为它在 XML 文件中是相同的)。
希望这对您有帮助:)
I have also experienced a problem with showing UTF-8 characters (special characters like æ. ø and å etc.), when using spring framework and freemarker template.
What i did was.
1. Ensure that your .ftl page is encoded with utf-8
This is an important thing to ensure, that a page not encoded with UTF-8 charset, could show the wrong numbers even though you have all the other requirements set. Check your IDE settings, to find out which default encoding it sets your files to. I think however today that both Eclipse and NetBeans set all files with UTF-8 encoding as standard. You must ensure that it is encoding UTF-8 with no BOM.
2. Include the Meta tag in your template file to set the charset
In your template (.ftl) file, which holds your
<head>
tag, set a<meta>
, with the attributecharset="UTF-8"
. This is if you use HTML 5. If you use xhtml or HTML 4, your meta tag needs to look like this<meta charset="UTF-8" />
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
3. Make sure you set a Character Encoding Filter in your Deployment Descriptor File
You have to filter all incoming/outgoing requests through a character encoding filter. This filter is set in your deployment descriptor (web.xml / or the java equivalent WebApplicationInitializer).
WebApplicationInitializer (Java File)
web.xml
4. Set the FreeMarker Character Encoding in configurer and view resolver
You also need to make all your FreeMarker files be standard encoded with UTF-8, this is done by setting their properties to UTF-8 in the FreeMarkerConfigurer and the FreeMarkerViewResolver. This is set in your spring application context file (I will only show the Java equivalent as it is the same in the XML file).
Hope this helps you out :)