spring mvc 表單提交 亂碼

不知為何就叫呵呵發表於2017-03-28

1。在web.xml新增過濾器:

<filter>
        <filter-name>SpringCharacterEncoding</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>SpringCharacterEncoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

2.spring-mvc 配置檔案,配置自動掃描時:

    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                        <value>text/json;charset=UTF-8</value>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

 

3.post提交就沒問題,而get可能還需要在方法上@RequestMapping註解新增produces引數

@RequestMapping(value="/selectPlaceListMap",produces = "text/html;charset=UTF-8")

 4.以上不行,就修改eclipse自身配置的servers底下的server.xml

檔案

修改:

<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

沒有則新增,實際執行環境更改tomcat的server.xml即可。

相關文章