首页 > 技术文章 > spring mvc 中文参数乱码

gmq-sh 2016-07-07 11:06 原文

最近做项目,springmvc的url中文参数乱码:

 

请求url:

http://localhost:8080/supply/supply_list.htm?productName=测试&isHomePage=

在后端:

@RequestMapping(value = SupplyURL.SUPPLY_LIST, method = RequestMethod.GET)
    public String toSupplyList (Model model, String productName) {

        PageBean<Supply> pagination = supplyFacade.querySupplyByPage(searchSupplyVO);
        
        model.addAttribute("searchSupplyVO", searchSupplyVO);return "/supply/supply_list";
    }

这里productName的值就变成了乱码,

检查我的web的web.xml中已经增加了过滤中文编码的:

 

<!-- Character Encoding filter -->
    <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>

 

但是还是乱码。

 

仔细检查了,还是中间容器tomcat导致的。修改tomcat的conf/server.xml中的

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

加上useBodyEncodingForURI="true"  URIEncoding="UTF-8" 。即可。

比如我要下载

http://localhost:8080/template/档案模板.xls

会转义成如下的,此时如果加上 URIEncoding="UTF-8" ,则可以在webapp/template/目录下找到文件。不会再报404了。

http://localhost:8080/template/%E5%AD%A6%E7%94%9F%E6%A1%A3%E6%A1%88%E5%AF%BC%E5%85%A5%E6%A8%A1%E6%9D%BF.xls

详细参考本人另一篇博文:

http://www.cnblogs.com/gmq-sh/p/7492126.html

 

推荐阅读