背景
舉個(gè)例子,出現(xiàn)中文亂碼的例子:提交表單的時(shí)候。
表單
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/c02/t1" method="post">
<input type="text" name="name">
<input type="submit">
</form>
</body>
</html>
業(yè)務(wù)控制類
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class controllerDemo02 {
@RequestMapping("/c02/t1")
public String test1(String name, Model model){
System.out.println(name);
model.addAttribute("message",name);
return "test";
}
}
結(jié)果:出現(xiàn)亂碼
解決方案
使用自己寫的過濾器
自定義一個(gè)Filter過濾器,去過濾亂碼
package Filter;
import javax.servlet.*;
import java.io.IOException;
public class EncodingFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletRequest.setCharacterEncoding("utf-8");
servletResponse.setCharacterEncoding("utf-8");
filterChain.doFilter(servletRequest,servletResponse);
}
public void destroy() {
}
}
在web.xml中配置這個(gè)過濾器
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>Filter.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
使用SpringMVC提供的過濾器
直接在web.xml中配置(固定的)
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
如果上面兩種方法還實(shí)現(xiàn)不了,在試試以下這個(gè)方法,大神編寫的
package Filter;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;
public class GenericEncodingFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//處理response的字符編碼
HttpServletResponse myResponse=(HttpServletResponse) response;
myResponse.setContentType("text/html;charset=UTF-8");
// 轉(zhuǎn)型為與協(xié)議相關(guān)對(duì)象
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
// 對(duì)request包裝增強(qiáng)
HttpServletRequest myrequest = new MyRequest(httpServletRequest);
chain.doFilter(myrequest, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
}
}
//自定義request對(duì)象,HttpServletRequest的包裝類
class MyRequest extends HttpServletRequestWrapper {
private HttpServletRequest request;
//是否編碼的標(biāo)記
private boolean hasEncode;
//定義一個(gè)可以傳入HttpServletRequest對(duì)象的構(gòu)造函數(shù),以便對(duì)其進(jìn)行裝飾
public MyRequest(HttpServletRequest request) {
super(request);// super必須寫
this.request = request;
}
// 對(duì)需要增強(qiáng)方法 進(jìn)行覆蓋
public Map getParameterMap() {
// 先獲得請(qǐng)求方式
String method = request.getMethod();
if (method.equalsIgnoreCase("post")) {
// post請(qǐng)求
try {
// 處理post亂碼
request.setCharacterEncoding("utf-8");
return request.getParameterMap();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} else if (method.equalsIgnoreCase("get")) {
// get請(qǐng)求
Map<String, String[]> parameterMap = request.getParameterMap();
if (!hasEncode) { // 確保get手動(dòng)編碼邏輯只運(yùn)行一次
for (String parameterName : parameterMap.keySet()) {
String[] values = parameterMap.get(parameterName);
if (values != null) {
for (int i = 0; i < values.length; i++) {
try {
// 處理get亂碼
values[i] = new String(values[i]
.getBytes("ISO-8859-1"), "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
}
hasEncode = true;
}
return parameterMap;
}
return super.getParameterMap();
}
//取一個(gè)值
public String getParameter(String name) {
Map<String, String[]> parameterMap = getParameterMap();
String[] values = parameterMap.get(name);
if (values == null) {
return null;
}
return values[0]; // 取回參數(shù)的第一個(gè)值
}
//取所有值
public String[] getParameterValues(String name) {
Map<String, String[]> parameterMap = getParameterMap();
String[] values = parameterMap.get(name);
return values;
}
}
以上就是關(guān)于當(dāng)使用 SpringMVC 的時(shí)候出現(xiàn)中文亂碼的三種解決方法的詳細(xì)內(nèi)容,想要了解更多關(guān)于 SpringMVC 其他方面的資料,可以搜索W3Cschool其它相關(guān)技術(shù)文章!也希望大家能夠多多關(guān)注和支持我們!