这个错误发生的情景如下:
(我们使用的是jsonplugin-0.32.jar)
struts.xml
<action name="student_*" class="StudentAction" method="{1}">
<result name="json" type="json" >
</result>
StudentAction 执行return "json"; 语句之后即报此错。
问题原因:
可能是 序列化 action中所有的属性时发生了错误。
解决方案:
在struts.xml中action的result属性中设置 序列化的根对象:
<action name="student_*" class="StudentAction" method="{1}">
<result name="json" type="json" >
<param name="root">William</param>
</result>
注意到,根对象设置为 一个不存在的值也是不会报错的,但是这里还是建议设置成action中某个需要序列化的属性。
对JSON类型的 result 中参数root 的说明:
root参数用于指定要序列化的根对象,如果省去这一配置,表示要序列化action中的所有属性
(问题的原因还是不甚清楚,以下粘贴的是action的部分代码,还望达人指点指点)
StudentAction.java:
public String list(){
int end = start+limit;
System.out.println(" StudentAction_list executes !");
try {
students = studentService.getAllStudents();
} catch (SQLException e) {
e.printStackTrace();
}
int length = students.size();
if(end > length){
end = length;
}
List<Student> listStudents = students.subList(start, end);
super.setContentType(super.JSON);
String json = "{count:" + students.size() + ",items:" + super.getJSONArrayString(listStudents) + "}";
super.outPrint(json);
System.out.println(json);
return "json";
}