Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The situation of Chinese garbled code in SpringMVC and how to solve it

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "Chinese garbled code in SpringMVC and how to solve it". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope this article "Chinese garbled code in SpringMVC and how to solve it" can help you solve the problem.

Background

For example, there is an example of garbled code in Chinese: when submitting a form.

Form

Title

Service control class

Package controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class controllerDemo02 {@ RequestMapping ("/ c02/t1") public String test1 (String name, Model model) {System.out.println (name); model.addAttribute ("message", name); return "test";}} the solution uses its own filter

Customize a Filter filter to filter garbled codes

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 () {}}

Configure this filter in web.xml

EncodingFilter Filter.EncodingFilter EncodingFilter / * use the filter provided by SpringMVC

Configure directly in web.xml (fixed)

Encoding org.springframework.web.filter.CharacterEncodingFilter encoding utf-8 encoding / * if the above two methods can not be implemented, try the following method, 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 written by God Public class GenericEncodingFilter implements Filter {public void destroy () {} public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {/ / process response character encoding HttpServletResponse myResponse= (HttpServletResponse) response; myResponse.setContentType ("text/html;charset=UTF-8"); / / transform to protocol-related object HttpServletRequest httpServletRequest = (HttpServletRequest) request / / A pair of request wrapper enhancements HttpServletRequest myrequest = new MyRequest (httpServletRequest); chain.doFilter (myrequest, response);} public void init (FilterConfig filterConfig) throws ServletException {}} / / Custom request object, HttpServletRequest wrapper class class MyRequest extends HttpServletRequestWrapper {private HttpServletRequest request; / / tag private boolean hasEncode whether encoded or not / / define a constructor that can be passed in a HttpServletRequest object to decorate it with public MyRequest (HttpServletRequest request) {super (request); / / super must write this.request = request;} / / A pair of enhanced methods to override public Map getParameterMap () {/ / get the request method String method = request.getMethod () first If (method.equalsIgnoreCase ("post")) {/ / post request try {/ / handle post garbled request.setCharacterEncoding ("utf-8"); return request.getParameterMap ();} catch (UnsupportedEncodingException e) {e.printStackTrace () }} else if (method.equalsIgnoreCase ("get")) {/ / get request Map parameterMap = request.getParameterMap (); if (! hasEncode) {/ / ensure that the get manual coding logic runs for (String parameterName: parameterMap.keySet ()) {String [] values = parameterMap.get (parameterName) only once If (values! = null) {for (int I = 0; I < values.length) Try {/ / handle get garbled values [I] = new String (values [I] .getBytes ("ISO-8859-1"), "utf-8") } catch (UnsupportedEncodingException e) {e.printStackTrace ();}} hasEncode = true;} return parameterMap } return super.getParameterMap ();} / / take a value public String getParameter (String name) {Map parameterMap = getParameterMap (); String [] values = parameterMap.get (name); if (values = = null) {return null;} return values [0] / / retrieve the first value of the parameter} / / take all values public String [] getParameterValues (String name) {Map parameterMap = getParameterMap (); String [] values = parameterMap.get (name); return values;}} this is the end of the content on "Chinese garbled in SpringMVC and how to solve it". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report