Spring MVC アプリケーションには、Ajax を使用したコントローラーと JSP ファイルがあります。Ajax から Spring Controller にデータを送信すると、文字セット 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>