エンティティ クラス GroupStudent、Spring Controller、および ajax 関数を使用した JSP ページがあります。コントローラーでは、@ResponseBody を使用してエンティティ GroupStudent オブジェクトを JSP ページに渡そうとします。しかし、私は常にブラウザからエラーを受け取ります: Error[object Object]. プロジェクトjackson-core-aslおよびjackson-mapper-asl jarのlibフォルダーに追加する必要があることがわかりました。それらを追加し(バージョン1.9.7)、spring-servlet.xmlに配置して、自動的にGroupStudentオブジェクトをjson形式に変換してajax関数に戻すことができるようにしました。しかし、それは役に立ちませんでした.ブラウザには常に同じエラーダイアログが表示されます. @ResponseBody を使用してエンティティ オブジェクトを ajax に渡す方法を誰かが知っている場合は、その助けに非常に感謝します。
ありがとうございました。
GroupStudent クラス
@Entity
@Table(name = "GroupStudent")
@NamedQueries({
@NamedQuery(name = "GroupStudent.getAllGroups", // get all groups
query = "select g from GroupStudent g"),
@NamedQuery(name = "GroupStudent.getGroupByName", // get group by name
query = "select g from GroupStudent g where g.groupStudentNumber = :name")
})
public class GroupStudent implements Serializable {
public GroupStudent() {}
public GroupStudent(String groupStudentNumber) {
this.groupStudentNumber = groupStudentNumber;
}
// create connectivity with table Student
private Set<Student> students = new HashSet<Student>();
@OneToMany(mappedBy = "groupStudent", cascade = CascadeType.ALL, orphanRemoval = true)
public Set<Student> getStudents() {
return this.students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "group_id_seq")
@SequenceGenerator(name = "group_id_seq", sequenceName = "GroupStudent_seq", allocationSize = 1)
@Column(name = "GroupStudentId")
public Long getGroupStudentId() {
return this.groupStudentId;
}
public void setGroupStudentId(Long groupStudentId) {
this.groupStudentId = groupStudentId;
}
@Column(name = "GroupStudentNumber")
public String getGroupStudentNumber() {
return this.groupStudentNumber;
}
public void setGroupStudentNumber(String groupStudentNumber) {
this.groupStudentNumber = groupStudentNumber;
}
// table GroupStudent fields
private Long groupStudentId;
private String groupStudentNumber;
}
コントローラ
@RequestMapping(value = "/addData.html", method = RequestMethod.POST)
public @ResponseBody GroupStudent addNewGroup(@RequestBody GroupStudent group) {
return group;
}
}
Ajax 関数
function addGroupAjax() {
var groupStudentNumber = $('#groupStudentNumber').val();
$.ajax({
type: "POST",
url: "/IRSystem/addData.html",
contentType: "application/json; charset=utf-8",
dataType: "json",
mimeType: "application/json",
data: "{\"groupStudentNumber\":" + "\"" + groupStudentNumber + "\"}",
success: function(response) {
},
error: function(e) {
alert("Error" + e);
}
});
}