在 Spring MVC 应用程序中,我有一个带有 Ajax 的控制器和 JSP 文件。当我将数据从 Ajax 发送到 Spring 控制器时,我有正确的字符串,字符集为 UTF-8,但是当控制器向 Ajax 发送响应时,该字符串的编码是错误的。我需要控制器以俄语发送响应并遇到此问题,当我对 Ajax 做出响应并将其插入 JSP 页面时,我只有:?????? ??????.这是我的代码:
@Controller
public class GroupsController {
@RequestMapping(value = "/addData.html", method = RequestMethod.GET)
public ModelAndView getPage() {
return new ModelAndView("addData");
}
@RequestMapping(value = "/addData.html", method = RequestMethod.POST)
public @ResponseBody String addNewGroup(@ModelAttribute(value = "group") GroupStudent group,
if(group.getGroupStudentNumber() != null) {
return "Группа " + group.getGroupStudentNumber() + " добавлена";
// return "Group " + group.getGroupStudentNumber() + " has been added";
} else
return null;
}
}
<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>Add data</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" charset="UTF-8">
<script type="text/javascript"
src="<c:url value="resources/jquery.js"/>"></script>
<script type="text/javascript">
function addGroupAjax() {
var groupStudentNumber = $('#groupStudentNumber').val();
$.ajax({
type: "POST",
url: "/IRSystem/addData.html",
data: "groupStudentNumber=" + groupStudentNumber,
success: function(response) {
$('#group').html(response);
},
error: function(e) {
alert("Error" + e);
}
});
}
</script>
</head>
<body>
<div align="left">
<label>Group</label>
<input id="groupStudentNumber"/>
<input type="submit" value="Add" onclick="addGroupAjax()" />
<div id="group" style="color:green"></div>
</div>
</body>
</html>