您好,登錄后才能下訂單哦!
SpringMVC中出現中文亂碼如何解決?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
<%@ 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>
業務控制類
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"; } }
結果:出現亂碼
自定義一個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中配置這個過濾器
<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>
直接在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>
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"); // 轉型為與協議相關對象 HttpServletRequest httpServletRequest = (HttpServletRequest) request; // 對request包裝增強 HttpServletRequest myrequest = new MyRequest(httpServletRequest); chain.doFilter(myrequest, response); } public void init(FilterConfig filterConfig) throws ServletException { } } //自定義request對象,HttpServletRequest的包裝類 class MyRequest extends HttpServletRequestWrapper { private HttpServletRequest request; //是否編碼的標記 private boolean hasEncode; //定義一個可以傳入HttpServletRequest對象的構造函數,以便對其進行裝飾 public MyRequest(HttpServletRequest request) { super(request);// super必須寫 this.request = request; } // 對需要增強方法 進行覆蓋 public Map getParameterMap() { // 先獲得請求方式 String method = request.getMethod(); if (method.equalsIgnoreCase("post")) { // post請求 try { // 處理post亂碼 request.setCharacterEncoding("utf-8"); return request.getParameterMap(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } else if (method.equalsIgnoreCase("get")) { // get請求 Map<String, String[]> parameterMap = request.getParameterMap(); if (!hasEncode) { // 確保get手動編碼邏輯只運行一次 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(); } //取一個值 public String getParameter(String name) { Map<String, String[]> parameterMap = getParameterMap(); String[] values = parameterMap.get(name); if (values == null) { return null; } return values[0]; // 取回參數的第一個值 } //取所有值 public String[] getParameterValues(String name) { Map<String, String[]> parameterMap = getParameterMap(); String[] values = parameterMap.get(name); return values; } }
關于SpringMVC中出現中文亂碼如何解決問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。