使用 Spring MVC 时如何在 Freemarker 视图中设置内容类型?

发布于 2024-12-03 04:40:58 字数 720 浏览 2 评论 0原文

我正在使用带有 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 技术交流群。

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

发布评论

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

评论(2

情独悲 2024-12-10 04:40:58

视图解析器(应该位于您的 dispatcher-servlet.xml 中)具有一个 contentType 属性:

<bean id="viewResolver"
   class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    <property name="prefix" value=""/>
    <property name="suffix" value=".ftl"/>
    <property name="contentType" value="text/html;charset=UTF-8"/>
</bean>

The view resolver (should be in your dispatcher-servlet.xml) has a contentType property for that:

<bean id="viewResolver"
   class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    <property name="prefix" value=""/>
    <property name="suffix" value=".ftl"/>
    <property name="contentType" value="text/html;charset=UTF-8"/>
</bean>
雨的味道风的声音 2024-12-10 04:40:58

在使用 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,您的元标记需要如下所示

  • HTML 5
  • HTML 4/XHTML

3.确保在部署描述符文件中设置字符编码过滤器
您必须通过字符编码过滤器过滤所有传入/传出请求。此过滤器在您的部署描述符(web.xml / 或 java 等效的 WebApplicationInitializer)中设置。

WebApplicationInitializer(Java 文件)

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    registerCharacterEncodingFilter(servletContext);
}

/**
 * Filter all incoming requests with character encoding UTF-8
 * @param servletContext 
 */
private void registerCharacterEncodingFilter(ServletContext servletContext) {
    CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
    encodingFilter.setEncoding("UTF-8");
    encodingFilter.setForceEncoding(true);
    FilterRegistration.Dynamic characterEncodingFilter = servletContext.addFilter("characterEncodingFilter", encodingFilter);
    characterEncodingFilter.addMappingForUrlPatterns(null, false, "/*");
}

web.xml

<filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

4.在配置器和视图解析器中设置 FreeMarker 字符编码
您还需要使所有 FreeMarker 文件都使用 UTF-8 标准编码,这是通过在 FreeMarkerConfigurer 和 FreeMarkerViewResolver 中将其属性设置为 UTF-8 来完成的。这是在 Spring 应用程序上下文文件中设置的(我将只显示 Java 等效项,因为它在 XML 文件中是相同的)。

/**
 * FreeMarker Configurer will help configure different settings of
 * the FreeMarker template engine.
 * 
 * @return an object of the FreeMarkerConfigurer class.
 */
@Bean
public FreeMarkerConfigurer freemarkerConfig() {
    FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
    freeMarkerConfigurer.setTemplateLoaderPath("/templates/");
    freeMarkerConfigurer.setDefaultEncoding("UTF-8");
    return freeMarkerConfigurer;
}

/**
 * The View resolver to use when resolving FreeMarker views.
 * 
 * @return the View Resolver Object used to resolve FreeMarker views.
 */
@Bean
public FreeMarkerViewResolver viewResolver() {
    FreeMarkerViewResolver viewResolver = new FreeMarkerViewResolver();
    viewResolver.setPrefix("");
    viewResolver.setSuffix(".ftl");
    viewResolver.setCache(false);   //Set to true during production
    viewResolver.setContentType("text/html;charset=UTF-8");
    return viewResolver;
}

希望这对您有帮助:)

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 attribute charset="UTF-8". This is if you use HTML 5. If you use xhtml or HTML 4, your meta tag needs to look like this

  • HTML 5 <meta charset="UTF-8" />
  • HTML 4/XHTML <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)

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    registerCharacterEncodingFilter(servletContext);
}

/**
 * Filter all incoming requests with character encoding UTF-8
 * @param servletContext 
 */
private void registerCharacterEncodingFilter(ServletContext servletContext) {
    CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
    encodingFilter.setEncoding("UTF-8");
    encodingFilter.setForceEncoding(true);
    FilterRegistration.Dynamic characterEncodingFilter = servletContext.addFilter("characterEncodingFilter", encodingFilter);
    characterEncodingFilter.addMappingForUrlPatterns(null, false, "/*");
}

web.xml

<filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

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

/**
 * FreeMarker Configurer will help configure different settings of
 * the FreeMarker template engine.
 * 
 * @return an object of the FreeMarkerConfigurer class.
 */
@Bean
public FreeMarkerConfigurer freemarkerConfig() {
    FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
    freeMarkerConfigurer.setTemplateLoaderPath("/templates/");
    freeMarkerConfigurer.setDefaultEncoding("UTF-8");
    return freeMarkerConfigurer;
}

/**
 * The View resolver to use when resolving FreeMarker views.
 * 
 * @return the View Resolver Object used to resolve FreeMarker views.
 */
@Bean
public FreeMarkerViewResolver viewResolver() {
    FreeMarkerViewResolver viewResolver = new FreeMarkerViewResolver();
    viewResolver.setPrefix("");
    viewResolver.setSuffix(".ftl");
    viewResolver.setCache(false);   //Set to true during production
    viewResolver.setContentType("text/html;charset=UTF-8");
    return viewResolver;
}

Hope this helps you out :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文