本文共 791 字,大约阅读时间需要 2 分钟。
背景:
一个简单的Controller类,返回结果直接写死了,跟传的参数没关系
@Controller
@RequestMapping("/article")public classArticleController {
@AutowiredprivateArticleService articleService;
@RequestMapping("/add")
@ResponseBodypublicResult add(Article article) {try{
articleService.add(article);return new Result("添加成功!");
}catch(Exception e) {return new Result("500","添加失败"+e);
}
}
}
web.xml,该部分配置放在web.xml的最上面
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
characterEncodingFilter
/*
spring-mvc.xml
使用maven的tomcat7插件,启动项目。
浏览器中输入:http://localhost:8080/spring-web-mybatics/article/add?title=你好&classify=1&content=测试测试测试
返回结果的汉字:“添加成功!变为:”?????
解决方法:
把项目的编码改成UTF-8,重新编译、启动即可。
后来想截图,再把UTF-8改为GBK,问题居然没有复现。。。
原文:https://www.cnblogs.com/javasl/p/12766250.html