<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="./t7" method="post">
<input type="text" name="name">
<input type="submit">
</form>
</body>
</html>
package com.lei.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class EncodingController {
@PostMapping(value = "/t7")
public String test1(String name, Model model) {
System.out.println(name);
model.addAttribute("msg",name);
return "test1";
}
}



检查Tomcat配置文件参数


spring自带过滤器解决
<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>

Spring MVC 中的字符编码问题与解决
这篇博客探讨了在Spring MVC应用中遇到的字符编码问题,包括POST请求的数据编码不正确。作者展示了如何通过配置Tomcat和使用Spring的CharacterEncodingFilter来确保UTF-8编码。示例代码展示了一个简单的表单提交,并在Controller中接收和打印POST参数。
727

被折叠的 条评论
为什么被折叠?



