简体   繁体   English

SpringMVC:如何在响应头中为html / jsp设置默认字符集

[英]SpringMVC: How to set the default charset for html/jsp in the response header

When springmvc is handling static html files, its returned header is: 当springmvc处理静态html文件时,其返回的标头为:

Content-Type: text/html

but what I want is: 但我想要的是:

Content-Type: text/html; charset=utf-8

When springmvc is handling jsp files, its returned header is: 当springmvc处理jsp文件时,其返回的标头为:

Content-Type: text/html; charset=ISO-8859-1

but what I want is: 但我想要的是:

Content-Type: text/html; charset=utf-8

I hate adding code: 我讨厌添加代码:

<%@ page pageEncoding="utf-8"%>

in every jsp files, I just want springmvc to assume my all jsp files are utf-8 encoded. 在每个jsp文件中,我只希望springmvc假定我所有的jsp文件都是utf-8编码的。

I have already added 我已经加了

<!-- encoding filter -->
<filter>
    <filter-name>Filter_CharacterEncoding</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>
</filter>
<filter-mapping>
    <filter-name>Filter_CharacterEncoding</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

in my web.xml, but it has no use 在我的web.xml中,但没有用

Blockquote I hate adding code: 我讨厌添加代码的Blockquote:

<%@ page pageEncoding="utf-8"% <%@ page pageEncoding =“ utf-8”%

Blockquote 大段引用

In that case you have to specify mime types in your server settings. 在这种情况下,您必须在服务器设置中指定mime类型。

If you are using tomcat use this (it is mentioned for static files but method is the same) 如果您使用的是tomcat,请使用 (静态文件中提到了它,但方法相同)

if you are using other server try searching out "How to set Mime types server name" 如果您使用其他服务器,请尝试搜索“如何设置Mime类型服务器名称”

While you specify mime type for the file add the character encoding as well with ";" 在为文件指定mime类型时,请同时添加字符编码和“;”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM