1.今天在做一个例子的时候,发现后台不能正确接收中文的url参数,试了各种解决办法都不可以。
以下是代码:
Controller:
package com.springapp.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/")
public class HelloController {
@RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Hello world IDEA!");
model.put("content","This is my first springmvc web");
return "index";
}
@RequestMapping(value = "/page/{name}/{age}",method = RequestMethod.GET)
public String getName(ModelMap modelMap, @PathVariable("name") String name, @PathVariable("age") int age) {
modelMap.addAttribute("name",name);
modelMap.addAttribute("age",age);
return "name";
}
}
name.jsp
<%@ page pageEncoding="UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<p>
名字:${name}<br/>
年龄:${age}<br/>
</p>
</body>
</html>
web.xml
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Encoding -->
<filter>
<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
天蓬老师2017-04-17 18:01:59
大部分解決方式樓上都已經講得比較清楚了。我把3種方式整理一下吧
Tomcat 可以直接設定URIEconding="UTF-8"
new String("中文".getBytes("ISO-8859-1"), "UTF-8");
將中文使用URLEncoder
編碼如:baidu.com/s?wd=%E4%BD%A0%E5%A5%BD%E4%B8%96%E7%95%8C
這3種方式都可以解決中文URL亂碼問題。
巴扎黑2017-04-17 18:01:59
有沒有debug下看後台接收到的資料是亂碼?還是到資料庫亂碼了?兩者是不同的情況,前者可以嘗試request.setCharacterEncoding
方法,并且用new String(para.getBytes(“ISO-8859-1”),“UTF-8”
對參數進行轉碼試試,如果看到後台的不是亂碼,到數據庫亂碼那就看數據庫鏈接上是不是忘了加enConding…
大家讲道理2017-04-17 18:01:59
封裝到一個物件裡,然後用@RequestBody 來註解這個參數,然後從對象裡get出來,但是有時候參數很少的時候,再去封裝一個對象,感覺多此一舉
ringa_lee2017-04-17 18:01:59
springmvc中文亂碼問題:http://luanxiyuan.iteye.com/blog/1849169
new String(接收的參數.getBytes("ISO-8859-1"), "UTF-8");
PHP中文网2017-04-17 18:01:59
如果是 tomcat 容器的話,請配置 URIEconding="UTF-8" useBodyEncodingForURI="true"
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000" URIEconding="UTF-8" useBodyEncodingForURI="true"
redirectPort="8443" />
迷茫2017-04-17 18:01:59
@RequestMapping(value = "/xxx", method = RequestMethod.GET, headers = {"content-type=application/json;charset=UTF-8"}, Produces = {"application/json;charset=UTF-8 “})