请求或响应的中文乱码问题

  • tomcat9解决了get请求和响应的中文乱码问题,但是没有解决post请求或响应的中文乱码问题

  • tomcat10解决了get和post请求以及响应的中文乱码问题

  • 考虑到实际项目中服务器的更新速度(短时间不会都采用tomcat10)为了项目的通用性和可靠性,最好为请求和响应添加应对中文乱码的方案

  • 通过在web.xml中配置编码过滤器,对符合通配请求条件的请求可以进行自定义的编码设置

  • 编码过滤器一般在web.xml文件中进行其他配置之前配置,尽早解决可能出现的中文乱码问题

  • web.xml中新增对编码过滤器的配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"> <!-- 配置编码过滤器 -->
<filter>
<filter-name>encode</filter-name>
<!-- 底层使用的过滤器 -->
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!--
(ctrl + alt + 点击上述CharacterEncodingFilter,查看源代码,可以看到需要以下参数)
private String encoding;
private boolean forceRequestEncoding;
private boolean forceResponseEncoding
-->
<!-- 向底层过滤器的参数赋值,进行编码的自定义配置 -->
<init-param>
<param-name>encoding</param-name>
<!-- 建议大写,小写有时解析不出来-->
<param-value>UTF-8</param-value>
</init-param> <init-param>
<!-- 对请求的编码解析采用encoding变量中的编码方式-->
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param> <init-param>
<!-- 对响应的编码解析采用encoding变量中的编码方式-->
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter> <filter-mapping>
<filter-name>encode</filter-name>
<!-- 指定对哪些请求进行过滤并进行编码配置处理-->
<url-pattern>/*</url-pattern>
</filter-mapping> </web-app>

控制器中action方法的返回值

  • 控制器中的方法名称不一定含有action单词,但是一般习惯于把控制器中的方法称为action方法

  • 返回String:设置了视图解析器之后,可以自动拼接前缀和后缀,来作为客户端请求的资源地址,完成响应页面的跳转。还可以拼接字符串,返回指定的路径

  • 返回Object:使用jackson工具(要添加jackson的依赖)进行转换,自动将对象或者集合转为json格式的数据并返回

  • 返回void:无返回值,一般用于ajax请求

  • 返回基本数据类型:用于ajax请求

  • ModelAndView:返回数据和视图对象(现在用的较少)

响应ajax请求

  • 与前面SpringMVC博客集中的SpringMVC(指SpringMVC 02)项目相比,新增的配置如下
  • 在pom.xml中添加jackson依赖,为了可以利用SpringMVC自动返回json格式的数据
    <!-- 添加jackson的依赖-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
</dependency>
  • 在webapp/js目录下添加jQuery函数库,为了前端可以使用jQuery封装的ajax()方法,便捷的发送ajax请求
  • webapp/index.jsp如下,是发送ajax请求并回显后端返回数据的前端界面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>index.jsp</title>
<script src="js/jquery-3.6.1.js"></script>
</head>
<body> <a href="javascript:showUser()">发送ajax请求,获取用户信息列表</a><br> <div id="userDiv">div,用来显示服务器返回的数据</div> <script type="text/javascript">
function showUser() {
//使用jQuery封装的ajax()发送ajax请求
$.ajax({
url:"${pageContext.request.contextPath}/list.action",
type:"get",
dataType:"json",
success:function (userList){
var data = ""
$.each(userList, function (i, user){
data += user.name + " ---- " + user.age + "<br>"
})
//将拼接好的数据回显在div中
$("#userDiv").html(data)
}
}
)
}
</script>
</body>
</html>
  • 新增控制器AjaxRequestAction,action方法上添加注解@ResponseBody,该注解专门用来解析ajax请求
package com.example.controller;

import com.example.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import java.util.ArrayList;
import java.util.List; @Controller
public class AjaxRequestAction { //只有当请求路径和ajax请求类型都对上时,目标方法才会被调用
@RequestMapping("/list")
@ResponseBody
public List<User> ajaxRequest(){//User类含有属性:name(String), age(int),无参和全参构造方法,全属性的getter,setter,toString方法
List<User> users = new ArrayList<>();
User u1 = new User("荷包蛋", 20);
User u2 = new User("饺子", 21);
User u3 = new User("橘子", 22);
users.add(u1);
users.add(u2);
users.add(u3);
return users;//SpringMVC框架会自动将对象数组转化为json数据格式返回给前端ajax请求
}
}
  • 在springmvc.xml文件需要新增注解驱动< mvc:annotationdriven /> ,为了用他来解析@ResponseBody注解,响应ajax请求
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 添加包扫描-->
<context:component-scan base-package="com.example.controller"/> <!-- 这里暂时不需要配置视图解析器,因为这里响应ajax请求,直接将请求到的数据以json格式返回给前端--> <!-- 对专门响应ajax请求的注解进行注解的驱动注册-->
<mvc:annotation-driven/>
</beans>
  • 部署并启动tomcat测试
  • 网站首页(left),发送ajax请求后的首页(right)如下:从后端获取到的数据经前端ajax拼接处理并填充在div中显示

最新文章

  1. DB2 错误信息码
  2. [Beautifulzzzz的博客目录] 快速索引点这儿O(∩_∩)O~~,红色标记的是不错的(⊙o⊙)哦~
  3. 使用TabBarController(代码实现)
  4. 【Win10】使用 ValidationAttribute 实现数据验证
  5. IOS 图片轮播实现原理 (三图)
  6. 浏览器的中的 XMLHttpRequest 对象的使用
  7. Ubuntu 13.10下Hadoop 2.2 安装、配置、编译(伪分布式)
  8. Golang学习 - unicode/utf16 包
  9. Perl脚本学习经验(三)--Perl中ftp的使用
  10. junit参数化测试的使用方法
  11. webpack 引入 bootstrap
  12. mysql flush操作
  13. 201521123110 java课程设计
  14. AttributeError: &#39;int&#39; object has no attribute &#39;log&#39;
  15. 计蒜客NOIP模拟赛(3) D1T2 信息传递
  16. 【JVM】7、深入理解Java G1垃圾收集器
  17. day25 Python四个可以实现自省的函数,反射
  18. 删除iis日志(deliislogs.vbs)
  19. fedora更新
  20. c++のurlmon实现下载文件并进度回调

热门文章

  1. SQL server设置连接数
  2. HDLBits-&gt;Circuits-&gt;Arithmetic Circuitd-&gt;3-bit binary adder
  3. 聊聊 RPA 方向的规划:简单有价值的事情长期坚持做
  4. zabbix监控apache80端口
  5. 校验日期格式为yyyy-MM-dd
  6. go-zero微服务实战系列(九、极致优化秒杀性能)
  7. IDEA的项目结构和IDEA的HelloWord
  8. 绿色安装MySQL5.7版本----配置my.ini文件注意事项
  9. 【我的面试-01】Web前端开发实习岗-面试题总结
  10. 【每天学一点-03】 使用Html5+Less实现简单的静态登录界面(入门Less)