在 JSP 文件中,我有输入学生组的字段,然后将此数据传递给实体类 GroupStudent(没有 getter 和 setter 方法)
@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;
}
// table GroupStudent fields
private Long groupStudentId;
private String groupStudentNumber;
}
JSP
<label>Group</label>
<input id="groupStudentNumber"/>
<input type="submit" value="Add" onclick="addGroupAjax()" />
和 ajax 函数将数据传递给 Spring Controller
function addGroupAjax() {
var groupStudentNumber = $('#groupStudentNumber').val();
$.ajax({
type: "POST",
url: "/IRSystem/addData.html",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: "{groupStudentNumber:" + groupStudentNumber + "}",
success: function(response) {
},
error: function(e) {
alert("Error" + e);
}
});
}
但它不会将数据传递给控制器。实体的字段为空。
@RequestMapping(value = "/addData.html", method = RequestMethod.POST)
public @ResponseBody Student addNewGroup(@ModelAttribute(value = "group") GroupStudent group) {
System.out.println("Entered group: " + group.getGroupStudentNumber());
return new Student();
}
还有一件事我也不能将实体 Student 传递给 ajax。我添加到 Spring jars
jackson-core-asl-1.7.1 和 jackson-mapper-asl-1.7.1 能够将实体对象传递给 ajax。但它没有给出结果。当我尝试在 Google Chrome 中传递数据(到 ajax)时,我的窗口出现错误 Error[object Object]。我不知道为什么会这样。我将不胜感激任何信息,谢谢。