亂碼產生的原因
前後端編碼不一致就會導致亂碼
問題詳情
點擊提交,服務器解析出亂碼
User [username=??????é??????¤§???, password=, height=, address=Address [province=, city=]]
解決問題後
User [username=南京郵電大學, password=, height=, address=Address [province=, city=]]
前端頁面的JSP模板設置
Winodws-Preferences-Web-JSP Files-Editor-Templates
JSP with html 5 markup
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${cursor}
</body>
</html>
在Web.xml配製Filter
一定要爲第一個filter-mapping
<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>