日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

springmvc 全局编码_SpringMVC-设置UTF-8编码

發(fā)布時(shí)間:2024/9/15 javascript 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springmvc 全局编码_SpringMVC-设置UTF-8编码 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

SpringMVC - 設(shè)置UTF-8編碼

參考

全局修改輸出為UTF-8編碼

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

annotation方法

由方法1, 我們可以看到一個(gè)起關(guān)鍵作用的類org.springframework.http.converter.StringHttpMessageConverter. 打開源碼可以發(fā)現(xiàn):

public class StringHttpMessageConverter extends AbstractHttpMessageConverter {

// 省略 ....

public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");

private final List availableCharsets;

private boolean writeAcceptCharset = true;

public StringHttpMessageConverter(Charset defaultCharset) {

super(defaultCharset, MediaType.TEXT_PLAIN, MediaType.ALL);

this.availableCharsets = new ArrayList(Charset.availableCharsets().values());

}

// 省略 ....

}

可以看到, 默認(rèn)編碼已經(jīng)被設(shè)定為了Charset.forName("ISO-8859-1"). 所以我們需要想辦法替換掉這個(gè)默認(rèn)編碼. 方法1是通過xml加載了參數(shù)為UTF-8的StringHttpMessageConverter.

回到使用注解配置的Java代碼中:

因?yàn)榕渲肧pringMVC的類, 需要繼承于WebMvcConfigurerAdapter, 我們可以看看里面有沒有相關(guān)方法可用.

public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer{...}

// 所以繼續(xù)看WebMvcConfigurer

public interface WebMvcConfigurer {

/**

* Configure the {@link HttpMessageConverter}s to use for reading or writing

* to the body of the request or response. If no converters are added, a

* default list of converters is registered.

*

Note that adding converters to the list, turns off

* default converter registration. To simply add a converter without impacting

* default registration, consider using the method

* {@link #extendMessageConverters(java.util.List)} instead.

* @param converters initially an empty list of converters

*/

void configureMessageConverters(List> converters);

/**

* A hook for extending or modifying the list of converters after it has been

* configured. This may be useful for example to allow default converters to

* be registered and then insert a custom converter through this method.

* @param converters the list of configured converters to extend.

* @since 4.1.3

*/

void extendMessageConverters(List> converters);

}

可以看到configureMessageConverters和extendMessageConverters兩個(gè)方法. 區(qū)別是前者會(huì)覆蓋掉默認(rèn)的Converter, 而后者是擴(kuò)展. 所以我們只需要在我們繼承于WebMvcConfigurerAdapter這個(gè)類的實(shí)現(xiàn)中覆蓋掉configureMessageConverters方法即可.

實(shí)現(xiàn)如下:

@Configuration

@EnableWebMvc

@ComponentScan("me.xiaofud.spring101.spittr.web")

public class WebConfig extends WebMvcConfigurerAdapter {

@Override

public void configureMessageConverters(List> converters) {

StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(Charset.forName("UTF-8"));

stringHttpMessageConverter.setWriteAcceptCharset(false);

converters.add(stringHttpMessageConverter);

}

測試:

curl -I -X POST "http://localhost:8080/app/post/add"

HTTP/1.1 200 OK

Server: Apache-Coyote/1.1

Content-Type: text/plain;charset=UTF-8

Content-Length: 1338

Date: Sun, 12 Nov 2017 14:17:49 GMT

修改讀取參數(shù)時(shí)候的編碼

在web.xml中:

添加一個(gè)filter, 注冊org.springframework.web.filter.CharacterEncodingFilter.

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

forceEncoding

true

encodingFilter

/*

總結(jié)

以上是生活随笔為你收集整理的springmvc 全局编码_SpringMVC-设置UTF-8编码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。